UNPKG

expression-evaluation

Version:
98 lines (97 loc) 3.96 kB
const VALUE_TYPE_NAMES = ['void', 'boolean', 'number', 'buffer', 'string', 'array', 'object', 'function']; const VALUE_TYPE_COUNT = VALUE_TYPE_NAMES.length; export class Type { _vtypes; constructor(...args) { this._vtypes = new Set(args.length ? args : VALUE_TYPE_NAMES); } get isSpecific() { return this._vtypes.size === 1; } get isBoolean() { return this.isSpecific && this._vtypes.has('boolean'); } get isNumber() { return this.isSpecific && this._vtypes.has('number'); } get isBuffer() { return this.isSpecific && this._vtypes.has('buffer'); } get isString() { return this.isSpecific && this._vtypes.has('string'); } get isArray() { return this.isSpecific && this._vtypes.has('array'); } get isObject() { return this.isSpecific && this._vtypes.has('object'); } get isFunction() { return this.isSpecific && this._vtypes.has('function'); } get isVoid() { return this.isSpecific && this._vtypes.has('void'); } reduce(mask) { if (mask.isVoid) { return this; } const vtypes = Array.from(this._vtypes.values()).filter((t) => mask._vtypes.has(t)); return vtypes.length === 0 ? undefined : vtypes.length === this._vtypes.size ? this : new Type(...vtypes); } toOptional() { return new Type('void', ...this._vtypes); } toString() { return this._vtypes.size < VALUE_TYPE_COUNT ? Array.from(this._vtypes.values()).join('|') : '??'; } static of(value) { const vtype = value == null ? 'void' : typeof value === 'boolean' ? 'boolean' : typeof value === 'number' ? 'number' : value instanceof ArrayBuffer ? 'buffer' : typeof value === 'string' ? 'string' : Array.isArray(value) ? 'array' : typeof value === 'object' ? 'object' : 'function'; return new Type(vtype); } } export const typeUnknown = new Type(); export const typeVoid = new Type('void'); export const typeBoolean = new Type('boolean'); export const typeNumber = new Type('number'); export const typeBuffer = new Type('buffer'); export const typeString = new Type('string'); export const typeArray = new Type('array'); export const typeObject = new Type('object'); export const typeFunction = new Type('function'); export const typeOptionalBoolean = new Type('void', 'boolean'); export const typeOptionalNumber = new Type('void', 'number'); export const typeOptionalBuffer = new Type('void', 'buffer'); export const typeOptionalString = new Type('void', 'string'); export const typeOptionalArray = new Type('void', 'array'); export const typeOptionalObject = new Type('void', 'object'); export const typeOptionalFunction = new Type('void', 'function'); export const typeBooleanOrArray = new Type('boolean', 'array'); export const typeNumberOrArray = new Type('number', 'array'); export const typeBufferOrArray = new Type('buffer', 'array'); export const typeStringOrArray = new Type('string', 'array'); export const typeNumberOrString = new Type('number', 'string'); export const typeArrayOrObject = new Type('array', 'object'); export const typeOptionalArrayOrObject = new Type('void', 'array', 'object'); export const typeEnumerable = new Type('buffer', 'string', 'array'); export const typeIterable = new Type('buffer', 'string', 'array', 'object'); export const typeJson = new Type('void', 'boolean', 'number', 'string', 'array', 'object'); export const typeValid = new Type('boolean', 'number', 'buffer', 'string', 'array', 'object', 'function');