UNPKG

@bemedev/types

Version:

Type definitions for Bemedev projects

138 lines (134 loc) 4.09 kB
'use strict'; var utils = require('../utils.cjs'); var functions_deepclone = require('./deepclone.cjs'); // #region Helpers const _isPrimitive = (value) => { return (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null || value === undefined); }; const _isPrimitiveObject = (object) => { const isObject = utils.isPlainObject(object); if (isObject) { for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { const element = object[key]; const isPrimitiveMap = _isPrimitiveObject(element); if (!isPrimitiveMap) return false; } } return true; } const isArray = Array.isArray(object); if (isArray) { for (const item of object) { const isPrimitiveMap = _isPrimitiveObject(item); if (!isPrimitiveMap) return false; } return true; } return _isPrimitive(object); }; const _identity = (value) => value; const _partial = (value) => { return utils._unknown(value); }; const _required = (value) => { return utils._unknown(value); }; // eslint-disable-next-line @typescript-eslint/no-unused-vars const _function = (..._) => _identity; // #endregion const commons = utils.expandFn((value) => value, { partial: utils.expandFn(_partial, { deep: (value) => { return utils._unknown(value); }, }), const: (value) => value, clone: (object) => { return functions_deepclone(object); }, identity: _identity, isDefined: (value) => { return value !== undefined && value !== null; }, isUndefined: (value) => { return value === undefined; }, isNull: (value) => { return value === null; }, unknown: (value) => value, any: utils.castFn()(), neverify: (value) => { return utils._unknown(value); }, required: utils.expandFn(_required, { deep: (value) => { return utils._unknown(value); }, }), readonly: utils.expandFn((value) => value, { deep: utils.expandFn((value) => utils._unknown(value), { not: (value) => utils._unknown(value), }), not: (value) => utils._unknown(value), }), primitive: utils.castFn()({ is: _isPrimitive, }), primitiveObject: utils.castFn()({ is: _isPrimitiveObject, }), function: utils.expandFn(_function, { is: utils.expandFn((value) => { return typeof value === 'function'; }, { strict: (fn) => { return (value) => typeof value === 'function' && fn(value); }, }), forceCast: (value) => { return utils._unknown(value); }, dynamic: (value) => value, checker: utils.castFn()({ /** * Very low * Checks if value is a function with one argument * @param value value to check * @returns true if value is a function with one argument */ is: (value) => { return (typeof value === 'function' && value.length === 1 && !/^\s*class\s+/.test(value.toString())); }, byType: utils.expandFn((checker) => checker, { forceCast: (value) => utils._unknown(value), }), }), }), undefined: _identity(undefined), null: _identity(null), symbol: utils.castFn()({ is: (value) => typeof value === 'symbol', }), date: utils.castFn()({ is: (value) => { return value instanceof Date; }, }), undefiny: (value) => value, defaulted: (value, defaultValue) => { const out = value === undefined || value === null ? defaultValue : value; return utils._unknown(out); }, }); exports.commons = commons; //# sourceMappingURL=commons.cjs.map