Jump to content

User:Dv103/Converters for Wikidata enumerations

From Wikifunctions

JS

Basic class definition

function WikidataEnum(type, value){
	this.type=type;
	this.value=value;
	if (this.type.Z4K1){
		this.type=this.type.Z4K1.Z9K1;
	}
	if (this.value.Z1K1){
		let typeOfValue=this.value.Z1K1;
		while(typeOfValue.Z9K1){
			typeOfValue=typeOfValue.Z9K1;
		}
		this.value=this.value[typeOfValue+'K1'];
	}
}
WikidataEnum.prototype.compare=function(v){
	if (v instanceof  WikidataEnum){
		return this.type===v.type && this.value===v.value;
	}
	if (typeof(v) === 'string'){
		return this.value===v;
	}
	return false;
};
WikidataEnum.prototype.typeOfValue=function(){
	switch(this.value[0]){
		case 'Q': return 'Z6091';
		case 'P': return 'Z6092';
		case 'L': return 'Z6095';
	}
};

To code

function $fromEnum (v){
	let type=v.Z1K1;
	while (type.Z9K1){
		type=type.Z9K1;
	}
	let value=v[type+'K1'];
	while (value.Z9K1){
		value=value.Z9K1;
	}
	let typeOfValue=v[type+'K1'].Z1K1;
	while(typeOfValue.Z9K1){
		typeOfValue=typeOfValue.Z9K1;
	}
	value=value[typeOfValue+'K1'];
	return new WikidataEnum(type,value);
}

From code

WikidataEnum.prototype.$toEnum=function(){
	let r={};
	r.Z1K1={Z1K1:"Z9", Z9K1:this.type};
	r[this.type+'K1']={Z1K1:{
			Z1K1:"Z9",
			Z9K1:this.typeOfValue()
		}
	};
	r[this.type+'K1'][this.typeOfValue()+'K1']=this.value;
	return r;
};