@bemedev/types
Version:
Type definitions for Bemedev projects
136 lines (133 loc) • 3.92 kB
JavaScript
import { expandFn, castFn, _unknown, isPlainObject } from '../utils.js';
import deepClone from './deepclone.js';
// #region Helpers
const _isPrimitive = (value) => {
return (typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean' ||
value === null ||
value === undefined);
};
const _isPrimitiveObject = (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 = _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 _unknown(value);
};
const _required = (value) => {
return _unknown(value);
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _function = (..._) => _identity;
// #endregion
const commons = expandFn((value) => value, {
partial: expandFn(_partial, {
deep: (value) => {
return _unknown(value);
},
}),
const: (value) => value,
clone: (object) => {
return 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: castFn()(),
neverify: (value) => {
return _unknown(value);
},
required: expandFn(_required, {
deep: (value) => {
return _unknown(value);
},
}),
readonly: expandFn((value) => value, {
deep: expandFn((value) => _unknown(value), {
not: (value) => _unknown(value),
}),
not: (value) => _unknown(value),
}),
primitive: castFn()({
is: _isPrimitive,
}),
primitiveObject: castFn()({
is: _isPrimitiveObject,
}),
function: expandFn(_function, {
is: expandFn((value) => {
return typeof value === 'function';
}, {
strict: (fn) => {
return (value) => typeof value === 'function' && fn(value);
},
}),
forceCast: (value) => {
return _unknown(value);
},
dynamic: (value) => value,
checker: 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: expandFn((checker) => checker, {
forceCast: (value) => _unknown(value),
}),
}),
}),
undefined: _identity(undefined),
null: _identity(null),
symbol: castFn()({
is: (value) => typeof value === 'symbol',
}),
date: castFn()({
is: (value) => {
return value instanceof Date;
},
}),
undefiny: (value) => value,
defaulted: (value, defaultValue) => {
const out = value === undefined || value === null ? defaultValue : value;
return _unknown(out);
},
});
export { commons };
//# sourceMappingURL=commons.js.map