UNPKG

@bemedev/types

Version:

Type definitions for Bemedev projects

267 lines (264 loc) 8.29 kB
import { expandFn, _unknown, castFn, isPlainObject, partialCall } from '../utils.js'; import { commons } from './commons.js'; const _isRequiredDeep = (object) => { const isObject = isPlainObject(object); if (isObject) { for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { const element = object[key]; const isPrimitiveMap = _isRequiredDeep(element); if (!isPrimitiveMap) return false; } } return true; } const isArray = Array.isArray(object); if (isArray) { for (const item of object) { const isPrimitiveMap = _isRequiredDeep(item); if (!isPrimitiveMap) return false; } return true; } return commons.isDefined(object); }; const _readonly = expandFn((object) => { return Object.freeze(object); }, { forceCast: (object) => { const out = Object.freeze(object); return _unknown(out); }, dynamic: (object) => { return Object.freeze(object); }, is: (object) => { return Object.isFrozen(object); }, not: (object) => { return _unknown(object); }, deep: expandFn((object) => { const out = Object.freeze(object); return _unknown(out); }, { not: (object) => { return _unknown(object); }, }), }); const _omitDeepIs = (by, object, ...valuesOrKeys) => { const entries = Object.entries(object); for (const [key, value] of entries) { const isObject = isPlainObject(value); if (isObject) { const isDeep = _omitDeepIs(by, value, ...valuesOrKeys); if (!isDeep) return false; } else { const shouldOmit = by === 'element' ? valuesOrKeys.includes(value) : valuesOrKeys.includes(key); if (shouldOmit) return false; } } return true; }; const _omitIs = (by, object, ...valuesOrKeys) => { const entries = Object.entries(object); for (const [key, value] of entries) { const checkKey = by === 'key' && valuesOrKeys.includes(key); const checkElement = by === 'element' && valuesOrKeys.includes(value); if (checkKey) { return false; } else if (checkElement) { return false; } } return true; }; const _omit = (by, object, ...valuesOrKeys) => { const result = {}; const entries = Object.entries(object); entries.forEach(([key, value]) => { const checkKey = by === 'key' && !valuesOrKeys.includes(key); const checkElement = by === 'element' && !valuesOrKeys.includes(value); if (checkKey) { result[key] = value; } else if (checkElement) { result[key] = value; } }); return result; }; const __omitDeep = (by, object, ...valuesOrKeys) => { const entries = Object.entries(object); const result = {}; entries.forEach(([key, value]) => { const isObject = isPlainObject(value); if (isObject) { const picked = __omitDeep(by, value, ...valuesOrKeys); result[key] = picked; } else if (by === 'key' && !valuesOrKeys.includes(key)) { result[key] = value; } else if (by === 'element' && !!value && !valuesOrKeys.includes(value)) { result[key] = value; } }); return result; }; const _omitDeep = (by, object, ...valuesOrKeys) => { const result = __omitDeep(by, object, ...valuesOrKeys); return result; }; const _pickDeep = (by, object, ...valuesOrKeys) => { const result = {}; for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { const element = object[key]; const isObject = isPlainObject(element); if (isObject) { const picked = _pickDeep(by, element, ...valuesOrKeys); if (Object.keys(picked).length > 0) { result[key] = picked; } } else { const shouldPick = by === 'element' ? valuesOrKeys.includes(element) : valuesOrKeys.includes(key); if (shouldPick) result[key] = element; } } } return result; }; const _pick = (by, object, ...keys) => { const result = {}; const entries = Object.entries(object); entries.forEach(([key, value]) => { const shouldPick = by === 'element' ? keys.includes(value) : keys.includes(key); if (shouldPick) { result[key] = value; } }); return result; }; const checkEntries = (keys, object) => { const entries = Object.entries(keys); return entries.every(([key, type]) => { const check1 = key in object; if (!check1) return false; const value = object[key]; if (typeof type === 'string') { return typeof value === type; } else if (typeof type === 'function') { return type(value); } return false; }); }; // #endregion const objects = castFn()({ trueObject: castFn()({ is: isPlainObject }), type: Object, keysOf: (object) => { return Object.keys(object); }, values: (object) => { return Object.values(object); }, entries: (object) => { return Object.entries(object); }, byKey: (object, key) => { return object[key]; }, hasKeys: expandFn((object, ...keys) => { return keys.every(key => key in object); }, { typings: (object, keys) => { return checkEntries(keys, object); }, all: expandFn((object, ...keys) => { return (Object.keys(object).every(key => keys.includes(key)) && keys.every(key => key in object)); }, { typings: (object, keys) => { const check0 = Object.keys(object).every(key => Object.keys(keys).includes(key)); if (!check0) return false; return checkEntries(keys, object); }, }), }), omit: expandFn(partialCall(_omit, 'key'), { strict: expandFn(partialCall(_omit, 'key'), { is: partialCall(_omitIs, 'key'), }), const: expandFn(partialCall(_omit, 'key'), { is: partialCall(_omitIs, 'key'), }), is: partialCall(_omitIs, 'key'), by: expandFn(partialCall(_omit, 'element'), { is: partialCall(_omitIs, 'element'), }), deep: expandFn(partialCall(_omitDeep, 'key'), { by: expandFn(partialCall(_omitDeep, 'element'), { is: partialCall(_omitDeepIs, 'element'), }), is: partialCall(_omitDeepIs, 'key'), }), }), reverse: (object) => { const result = {}; for (const key in object) { result[object[key]] = key; } return result; }, readonly: _readonly, freeze: _readonly, require: expandFn((object, requires) => { return Object.assign(object, requires); }, { strict: (object, requires) => objects.require(object, requires), const: (object, requires) => objects.require(object, requires), is: expandFn((object) => { const values = Object.values(object); return values.every(value => value !== undefined && value !== null); }, { deep: _isRequiredDeep, }), }), pick: expandFn(partialCall(_pick, 'key'), { deep: expandFn(partialCall(_pickDeep, 'key'), { by: partialCall(_pickDeep, 'element'), }), by: partialCall(_pick, 'element'), }), ru: castFn()(), ra: castFn()(), primitive: castFn()({ is: (object) => { if (!isPlainObject(object)) return false; return commons.primitiveObject.is(object); }, }), }); export { objects }; //# sourceMappingURL=objects.js.map