@dark-engine/core
Version:
The lightweight and powerful UI rendering engine without dependencies and written in TypeScript (Browser, Node.js, Android, iOS, Windows, Linux, macOS)
74 lines (73 loc) • 2.37 kB
JavaScript
import { INDEX_KEY, LIB } from '../constants';
const detectIsFunction = o => typeof o === 'function';
const detectIsUndefined = o => typeof o === 'undefined';
const detectIsNumber = o => typeof o === 'number';
const detectIsString = o => typeof o === 'string';
const detectIsTextBased = o => typeof o === 'string' || typeof o === 'number';
const detectIsObject = o => typeof o === 'object';
const detectIsBoolean = o => typeof o === 'boolean';
const detectIsArray = Array.isArray;
const detectIsNull = o => o === null;
const detectIsEmpty = o => o === null || typeof o === 'undefined';
const detectIsFalsy = o => o === null || typeof o === 'undefined' || o === false;
const detectIsPromise = o => o instanceof Promise;
const detectIsEqual = Object.is;
const keys = Object.keys;
const hasKeys = o => keys(o).length > 0;
const getTime = () => Date.now();
const dummyFn = () => {};
const trueFn = () => true;
const falseFn = () => false;
const logError = (...args) => !detectIsUndefined(console) && console.error(...args);
const formatErrorMsg = (x, prefix = LIB) => `[${prefix}]: ${x}`;
function throwThis(x) {
throw x;
}
const illegal = (x, prefix = LIB) => throwThis(new Error(formatErrorMsg(x, prefix)));
const flatten = x => x.flat(Infinity);
function detectAreDepsDifferent(prevDeps, nextDeps) {
if (prevDeps === nextDeps || (prevDeps.length === 0 && nextDeps.length === 0)) return false;
const max = Math.max(prevDeps.length, nextDeps.length);
for (let i = 0; i < max; i++) {
if (!detectIsEqual(prevDeps[i], nextDeps[i])) return true;
}
return false;
}
const nextTick = callback => Promise.resolve().then(callback);
const createIndexKey = idx => `${INDEX_KEY}:${idx}`;
const mapRecord = record => keys(record).map(x => record[x]);
const createError = x => (x instanceof Error ? x : new Error(String(x)));
const stringify = JSON.stringify;
export {
detectIsFunction,
detectIsUndefined,
detectIsNumber,
detectIsString,
detectIsTextBased,
detectIsObject,
detectIsBoolean,
detectIsArray,
detectIsNull,
detectIsEmpty,
detectIsFalsy,
detectIsPromise,
detectIsEqual,
keys,
hasKeys,
getTime,
dummyFn,
trueFn,
falseFn,
logError,
formatErrorMsg,
throwThis,
illegal,
flatten,
detectAreDepsDifferent,
nextTick,
createIndexKey,
mapRecord,
createError,
stringify,
};
//# sourceMappingURL=utils.js.map