UNPKG

@trail-ui/shared-utils

Version:
259 lines (251 loc) 6.78 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { arrayToObject: () => arrayToObject, callAll: () => callAll, callAllHandlers: () => callAllHandlers, clampValue: () => clampValue, cleanObject: () => cleanObject, cleanObjectKeys: () => cleanObjectKeys, clsx: () => import_clsx.clsx, cn: () => cn, compact: () => compact, copyObject: () => copyObject, countDecimalPlaces: () => countDecimalPlaces, dataAttr: () => dataAttr, getKeyValue: () => getKeyValue, getProp: () => getProp, isArray: () => isArray, isEmpty: () => isEmpty, isEmptyArray: () => isEmptyArray, isEmptyObject: () => isEmptyObject, isFunction: () => isFunction, isNumeric: () => isNumeric, isObject: () => isObject, omitObject: () => omitObject, renameProp: () => renameProp, toPrecision: () => toPrecision, twMerge: () => import_tailwind_merge.twMerge, warn: () => warn }); module.exports = __toCommonJS(src_exports); // src/assertion.ts function isArray(value) { return Array.isArray(value); } function isEmptyArray(value) { return isArray(value) && value.length === 0; } function isObject(value) { const type = typeof value; return value != null && (type === "object" || type === "function") && !isArray(value); } function isEmptyObject(value) { return isObject(value) && Object.keys(value).length === 0; } function isEmpty(value) { if (isArray(value)) return isEmptyArray(value); if (isObject(value)) return isEmptyObject(value); if (value == null || value === "") return true; return false; } function isFunction(value) { return typeof value === "function"; } var isNumeric = (value) => value != null && parseInt(value.toString(), 10) > 0; var dataAttr = (condition) => condition ? "true" : void 0; // src/console.ts var warningStack = {}; function warn(message, component, ...args) { var _a; const tag = component ? ` [${component}]` : " "; const log = `[Trail UI]${tag}: ${message}`; if (typeof console === "undefined") return; if (warningStack[log]) return; warningStack[log] = true; if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) !== "production") { return console.warn(log, args); } } // src/clsx.ts var import_clsx = require("clsx"); var import_tailwind_merge = require("tailwind-merge"); function cn(className, ...inputs) { return typeof className === "function" ? (val) => (0, import_clsx.clsx)(...inputs, className(val)) : (0, import_clsx.clsx)(...inputs, className); } // src/functions.ts function callAllHandlers(...fns) { return function func(event) { fns.some((fn) => { fn == null ? void 0 : fn(event); return event == null ? void 0 : event.defaultPrevented; }); }; } function callAll(...fns) { return function mergedFn(arg) { fns.forEach((fn) => { fn == null ? void 0 : fn(arg); }); }; } // src/numbers.ts function toNumber(value) { const num = parseFloat(value); return typeof num !== "number" || Number.isNaN(num) ? 0 : num; } function toPrecision(value, precision) { let nextValue = toNumber(value); const scaleFactor = 10 ** (precision != null ? precision : 10); nextValue = Math.round(nextValue * scaleFactor) / scaleFactor; return precision ? nextValue.toFixed(precision) : nextValue.toString(); } function countDecimalPlaces(value) { if (!Number.isFinite(value)) return 0; let e = 1; let p = 0; while (Math.round(value * e) / e !== value) { e *= 10; p += 1; } return p; } function clampValue(value, min, max) { if (value == null) return value; if (max < min) { console.warn("clamp: max cannot be less than min"); } return Math.min(Math.max(value, min), max); } // src/object.ts var renameProp = (oldProp, newProp, { [oldProp]: old, ...others }) => ({ [newProp]: old, ...others }); var copyObject = (obj) => { if (!isObject(obj)) return obj; if (obj instanceof Array) return [...obj]; return { ...obj }; }; var omitObject = (obj, omitKeys) => { if (!isObject(obj)) return obj; if (obj instanceof Array) return [...obj]; const newObj = { ...obj }; omitKeys.forEach((key) => newObj[key] && delete newObj[key]); return newObj; }; var cleanObject = (obj) => { if (!isObject(obj)) return obj; if (obj instanceof Array) return [...obj]; const newObj = { ...obj }; Object.keys(newObj).forEach((key) => { if (newObj[key] === void 0 || newObj[key] === null) { delete newObj[key]; } }); return newObj; }; var cleanObjectKeys = (obj, keys = []) => { if (!isObject(obj)) return obj; if (obj instanceof Array) return [...obj]; const newObj = { ...obj }; keys.forEach((key) => { if (newObj[key]) { delete newObj[key]; } }); return newObj; }; var getKeyValue = (obj, key) => { if (!isObject(obj)) return obj; if (obj instanceof Array) return [...obj]; return obj[key]; }; var getProp = (obj, path, fallback, index) => { const key = typeof path === "string" ? path.split(".") : [path]; for (index = 0; index < key.length; index += 1) { if (!obj) break; obj = obj[key[index]]; } return obj === void 0 ? fallback : obj; }; var arrayToObject = (arr) => { if (!arr.length || !Array.isArray(arr)) return {}; return arr.reduce((acc, item) => { return { ...acc, ...item }; }, {}); }; function compact(object) { const clone = Object.assign({}, object); for (let key in clone) { if (clone[key] === void 0) delete clone[key]; } return clone; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { arrayToObject, callAll, callAllHandlers, clampValue, cleanObject, cleanObjectKeys, clsx, cn, compact, copyObject, countDecimalPlaces, dataAttr, getKeyValue, getProp, isArray, isEmpty, isEmptyArray, isEmptyObject, isFunction, isNumeric, isObject, omitObject, renameProp, toPrecision, twMerge, warn });