UNPKG

feats

Version:

A comprehensive TypeScript utility library featuring fluent text building, type-safe switching, duration utilities, React hooks, and extended array/object prototypes for modern JavaScript development.

250 lines (245 loc) 7.08 kB
export { s as switcher, t as text } from './internals-CeU06BAg.js'; /** * Maps normalized units to their equivalent in milliseconds. */ const unitToMs = { milliseconds: 1, seconds: 1000, minutes: 60000, hours: 3600000, days: 86400000, }; /** * Maps various input unit aliases to a normalized internal unit. */ const aliasMap = { ms: "milliseconds", millisecond: "milliseconds", milliseconds: "milliseconds", s: "seconds", second: "seconds", seconds: "seconds", m: "minutes", minute: "minutes", minutes: "minutes", h: "hours", hour: "hours", hours: "hours", d: "days", day: "days", days: "days", }; /** * Creates an immutable duration object with fluent utilities. * * Example: * ```ts * const d = duration(2, "minutes"); * d.as("seconds"); // 120 * d.add(30, "seconds").milliseconds; // 150000 * ``` * * @param value - The numeric value of the duration * @param unit - The unit of the value (e.g. "minutes", "s", etc.) * @returns A duration instance with utility methods and accessors */ function duration(value, unit) { const ms = value * unitToMs[aliasMap[unit]]; const methods = { /** * Converts the current duration to another unit. * @param targetUnit - The unit to convert to * @returns The converted value */ as: (targetUnit) => { const target = aliasMap[targetUnit]; return ms / unitToMs[target]; }, /** * Adds another duration and returns a new immutable duration. * @param addValue - The value to add * @param addUnit - The unit of the value to add */ add: (addValue, addUnit) => { return duration(ms + duration(addValue, addUnit).milliseconds, "milliseconds"); }, /** * Subtracts another duration and returns a new immutable duration. * @param subValue - The value to subtract * @param subUnit - The unit of the value to subtract */ subtract: (subValue, subUnit) => { return duration(ms - duration(subValue, subUnit).milliseconds, "milliseconds"); }, /** * Returns the internal value in milliseconds. */ valueOf: () => ms, /** * Converts the duration to a string in milliseconds. * @returns A string like `"120000ms"` */ toString: () => `${ms}ms`, /** * Returns a new duration instance with the same value. */ clone: () => duration(ms, "milliseconds"), /** * Converts the duration to a primitive value (number or string). */ [Symbol.toPrimitive]: (hint) => { if (hint === "string") return `${ms}ms`; return ms; }, }; const proxy = new Proxy({}, { get(_, prop) { if (prop in methods) return methods[prop]; if (prop in aliasMap) { const normalized = aliasMap[prop]; return ms / unitToMs[normalized]; } throw new Error(`Invalid property access: ${prop}`); }, }); return proxy; } /** * Converts a duration to milliseconds. * * @param value - The numeric value of the duration * @param unit - The unit of the value (e.g. "seconds", "m", etc.) * @returns The equivalent duration in milliseconds */ function millis(value, unit) { return duration(value, unit).milliseconds; } if (!Array.prototype.filterAndMap) { Array.prototype.filterAndMap = function (handler) { const result = []; for (let i = 0; i < this.length; i++) { const mapped = handler(this[i], i, this); if (isDefined(mapped)) { result.push(mapped); } } return result; }; } if (!Array.prototype.partition) { Array.prototype.partition = function (handler) { const trueResults = []; const falseResults = []; for (let i = 0; i < this.length; i++) { if (handler(this[i], i, this)) { trueResults.push(this[i]); } else { falseResults.push(this[i]); } } return [trueResults, falseResults]; }; } if (!Array.prototype.groupBy) { Array.prototype.groupBy = function (keyFn) { var _a; const groups = {}; for (let i = 0; i < this.length; i++) { const key = keyFn(this[i], i, this); ((_a = groups[key]) !== null && _a !== void 0 ? _a : (groups[key] = [])).push(this[i]); } return new Proxy(groups, { get(target, prop) { if (prop in target) { return target[prop]; } return []; }, }); }; } if (!Array.prototype.unique) { Array.prototype.unique = function () { return Array.from(new Set(this)); }; } if (!Array.prototype.uniqBy) { Array.prototype.uniqBy = function (keyFn) { const seen = new Set(); const result = []; for (const item of this) { const key = keyFn(item); if (!seen.has(key)) { seen.add(key); result.push(item); } } return result; }; } if (!Array.prototype.chunk) { Array.prototype.chunk = function (size) { const chunks = []; for (let i = 0; i < this.length; i += size) { chunks.push(this.slice(i, i + size)); } return chunks; }; } if (!Array.prototype.intersect) { Array.prototype.intersect = function (other) { const setOther = new Set(other); return this.filter((item) => setOther.has(item)); }; } if (!Array.prototype.difference) { Array.prototype.difference = function (other) { const setOther = new Set(other); return this.filter((item) => !setOther.has(item)); }; } if (!Array.prototype.union) { Array.prototype.union = function (other) { const set = new Set([...this, ...other]); return Array.from(set); }; } if (!Array.prototype.pluck) { Array.prototype.pluck = function (key) { return this.map((item) => item[key]); }; } function isDefined(value) { return value !== undefined; } if (!Object.typedKeys) { Object.typedKeys = Object.keys; } if (!Object.pick) { Object.pick = function (obj, keys) { const result = {}; for (const key of keys) { if (key in obj) { result[key] = obj[key]; } } return result; }; } if (!Object.omit) { Object.omit = function (obj, keys) { const result = {}; const keySet = new Set(keys.map((v) => v.toString())); for (const key in obj) { if (!keySet.has(key)) { result[key] = obj[key]; } } return result; }; } export { duration, millis }; //# sourceMappingURL=index.mjs.map