class CommonCodes {
	constructor(codeList, asObject) {
		codeList.forEach(item => this[item.code] = !asObject ? item.value : item);
		this.codes = () => codeList.map(item => item.code);
		this.list = () => codeList;
		this.format = (code) => {
			return this.value(code, "");
		}
	}
	
	_value(code, field) {
		let found = this[code];
		if (!found)
			return null;
		
		return "string" == typeof found ? found : found[field] 
	}
	
	value(code, nt) {
		return ifEmpty(this._value(code, "value"), nt);
	}
	
	format(code) {
		return this.value(code, "");
	}
	
	parse(value) {
		for (let code in this) {
			let val = this.value(code);
			if (value == val)
				return code;
		}
		return "";
	}

	etc1(code) {
		return this._value(code, "etc1");
	}
	
	etc2(code) {
		return this._value(code, "etc2");
	}
	
	etc3(code) {
		return this._value(code, "etc3");
	}
}