UNPKG

@idiosync/react-observable

Version:

State management control layer for React projects

56 lines (55 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shallowEqualArrays = exports.isPlainObject = exports.isObject = exports.isFunction = exports.identity = exports.tryCatchSync = exports.tryCatch = void 0; exports.uuid = uuid; const tryCatch = async (fn, errorMessage) => { try { const result = await fn(); return [result, undefined]; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); if (errorMessage) { err.message = `${errorMessage}\n${err.message}`; } return [undefined, err]; } }; exports.tryCatch = tryCatch; const tryCatchSync = (fn, errorMessage) => { try { const result = fn(); return [result, undefined]; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); if (errorMessage) { err.message = `${errorMessage}\n${err.message}`; } return [undefined, err]; } }; exports.tryCatchSync = tryCatchSync; const identity = (value) => value; exports.identity = identity; const isFunction = (value) => typeof value === 'function'; exports.isFunction = isFunction; const isObject = (value) => value !== null && typeof value === 'object'; exports.isObject = isObject; const isPlainObject = (value) => (0, exports.isObject)(value) && value.constructor === Object; exports.isPlainObject = isPlainObject; const shallowEqualArrays = (a, b) => { if (!Array.isArray(a) || !Array.isArray(b)) return false; if (a.length !== b.length) return false; return a.every((item, index) => item === b[index]); }; exports.shallowEqualArrays = shallowEqualArrays; let uuidCounter = 0; function uuid() { if (uuidCounter >= Number.MAX_SAFE_INTEGER) { uuidCounter = 0; } return `${Date.now()}-${++uuidCounter}`; }