AnPv2/Public/ecma/Utils/Options.ecma.js

113 lines
2.5 KiB
JavaScript

"use strict";
/**
* @class Options
* @constructor
* @returns {void}
* @access public
* @static
*/
export const Options = (function(){
/**
* @constructs Options
* @returns {void}
* @access private
* @static
*/
const Options = function(options, _default = null){
/** @type {Options} */
const self = this;
/** @type {boolean|null} */
this.overwrite = null;
/** @type {boolean|null} */
this.allow_nulls = null;
/**
* @returns {void}
* @access private
*/
const constructor = () => {
_default || (_default = Options.DEFAULT);
[
["overwrite", Options.OVERWRITE],
["allow_nulls", Options.NULLISH]
].forEach(([key, option]) => {
/** @type {boolean|null} */
const value = Options.get_value(options, option);
self[key] = value !== null ? value : _default[key];
});
};
/**
* @returns {number}
* @access public
*/
this.get = () => {
/** @type {number} */
let code = 0;
[
[Options.OVERWRITING, self.overwrite],
[Options.NULLISH, self.allow_nulls],
].forEach(([option, value]) => {
code += (
value ? 1 :
value === false ? 2 :
0) * 3 ** option;
});
return code;
};
constructor();
};
/** @type {number} */
Options.OVERWRITING = 0;
/** @type {number} */
Options.OVERWRITE = 1 + 3 * Options.OVERWRITING;
/** @type {number} */
Options.NO_OVERWRITE = 2 + 3 * Options.OVERWRITING;
/** @type {number} */
Options.NULLISH = 1;
/** @type {number} */
Options.ALLOW_NULLS = 1 + 3 * Options.NULLISH;
/** @type {number} */
Options.NOT_NULLS = 2 + 3 * Options.NULLISH;
/** @type {Options} */
Options.DEFAULT = new Options(Options.NO_OVERWRITE | Options.ALLOW_NULLS);
/**
* @param {!number} value
* @param {!number} option
* @returns {boolean|null}
* @access public
* @static
*/
Options.get_value = (value, option) => {
/** @type {number} */
const ternary = (value / 3 ** option >> 0) % 3;
return (
ternary === 1 ? true :
ternary === 2 ? false :
null
);
}
return Options;
})();