UNPKG

radashi-helper

Version:
351 lines (327 loc) 10.3 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // ../../node_modules/.pnpm/radashi@12.2.0-beta.af825f4/node_modules/radashi/dist/radashi.js function flat(lists) { return lists.reduce((acc, list2) => { acc.push(...list2); return acc; }, []); } function last(array, defaultValue) { return (array == null ? void 0 : array.length) > 0 ? array[array.length - 1] : defaultValue; } function objectify(array, getKey, getValue = (item) => item) { return array.reduce( (acc, item) => { acc[getKey(item)] = getValue(item); return acc; }, {} ); } function select(array, mapper, condition) { if (!array) { return []; } let mapped; return array.reduce((acc, item, index) => { if (condition) { condition(item, index) && acc.push(mapper(item, index)); } else if ((mapped = mapper(item, index)) != null) { acc.push(mapped); } return acc; }, []); } function sift(array) { return _nullishCoalesce((array == null ? void 0 : array.filter((x) => !!x)), () => ( [])); } function unique(array, toKey) { if (toKey) { const keys2 = /* @__PURE__ */ new Set(); return array.reduce((acc, item) => { const key = toKey(item); if (!keys2.has(key)) { keys2.add(key); acc.push(item); } return acc; }, []); } return [...new Set(array)]; } async function defer(func) { const callbacks = []; const register = (fn, options) => callbacks.push({ fn, rethrow: _nullishCoalesce((options == null ? void 0 : options.rethrow), () => ( false)) }); const [err, response] = await tryit(func)(register); for (const { fn, rethrow } of callbacks) { const [rethrown] = await tryit(fn)(err); if (rethrown && rethrow) { throw rethrown; } } if (err) { throw err; } return response; } async function reduce(array, asyncReducer, initValue) { const initProvided = initValue !== void 0; if (!initProvided && (array == null ? void 0 : array.length) < 1) { throw new Error("Cannot reduce empty array with no init value"); } const iter = initProvided ? array : array.slice(1); let value = initProvided ? initValue : array[0]; for (const [i, item] of iter.entries()) { value = await asyncReducer(value, item, i); } return value; } function tryit(func) { return (...args) => { try { const result = func(...args); return isPromise(result) ? result.then( (value) => [void 0, value], (err) => [err, void 0] ) : [void 0, result]; } catch (err) { return [err, void 0]; } }; } function memoize(cache, func, keyFunc, ttl) { return function callWithMemo(...args) { const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args }); const existing = cache[key]; if (existing !== void 0) { if (!existing.exp) { return existing.value; } if (existing.exp > (/* @__PURE__ */ new Date()).getTime()) { return existing.value; } } const result = func(...args); cache[key] = { exp: ttl ? (/* @__PURE__ */ new Date()).getTime() + ttl : null, value: result }; return result; }; } function memo(func, options = {}) { return memoize({}, func, _nullishCoalesce(options.key, () => ( null)), _nullishCoalesce(options.ttl, () => ( null))); } function proxied(handler) { return new Proxy( {}, { get: (target, propertyName) => handler(propertyName) } ); } function noop() { } function traverse(root, visitor, options, outerContext) { const context = _nullishCoalesce(outerContext, () => ( { value: null, key: null, parent: null, parents: [], path: [], skipped: /* @__PURE__ */ new Set(), skip(obj) { context.skipped.add(_nullishCoalesce(obj, () => ( context.value))); } })); const { rootNeedsVisit } = options ??= {}; const ownKeys = _nullishCoalesce(options.ownKeys, () => ( Object.keys)); const nestedOptions = { ...options, rootNeedsVisit: null }; let ok = true; const visit = (value, key) => { if (context.parent.constructor === Map) { [key, value] = value; } context.path.push(key); const result = visitor( context.value = value, context.key = key, context.parent, context, nestedOptions ); if (result === false) { return ok = false; } if (value !== null && typeof value === "object" && (isArray(value) || isPlainObject(value)) && !context.skipped.has(value) && !context.parents.includes(value)) { traverse2(value, result); } context.path.pop(); return ok; }; const traverse2 = (parent, parentResult) => { context.parents.push(parent); context.parent = parent; if (rootNeedsVisit && parent === root) { parentResult = visitor( context.value = parent, context.key = null, context.parent, context, nestedOptions ); if (parentResult === false) { return ok; } } if (isArray(parent)) { parent.slice().forEach((value, index, values) => { if (visit(value, index) === false) { values.length = 0; } }); } else if (parent === root && isIterable(parent)) { let index = 0; for (const value of parent) { if (visit(value, index) === false) { return ok; } index++; } } else { for (const key of ownKeys(parent)) { if (visit(parent[key], key) === false) { return ok; } } } context.parents.pop(); context.parent = last(context.parents); if (ok && isFunction(parentResult)) { ok = parentResult() !== false; } return ok; }; if (outerContext) { if (outerContext.skipped.has(root)) { return true; } const { value, key } = context; traverse2(root); context.value = value; context.key = key; return ok; } return traverse2(root); } function similarity(str1, str2) { if (str1 === str2) { return 0; } let start = 0; let end1 = str1.length - 1; let end2 = str2.length - 1; while (start <= end1 && start <= end2 && str1[start] === str2[start]) { start++; } while (end1 >= start && end2 >= start && str1[end1] === str2[end2]) { end1--; end2--; } const length1 = end1 - start + 1; const length2 = end2 - start + 1; if (length1 === 0) { return length2; } if (length2 === 0) { return length1; } const numRows = length1 + 1; const numColumns = length2 + 1; const distances = new Array(numRows * numColumns).fill(0); for (let x = 1; x < numColumns; x++) { distances[x] = x; } for (let y = 1; y < numRows; y++) { distances[y * numColumns] = y; } for (let x = 1; x < numColumns; x++) { for (let y = 1; y < numRows; y++) { const i = y * numColumns + x; distances[i] = Math.min( // Cost of a deletion. distances[i - numColumns] + 1, // Cost of an insertion. distances[i - 1] + 1, // Cost of a substitution. distances[i - numColumns - 1] + (str1[start + y - 1] === str2[start + x - 1] ? 0 : 1) ); } } return distances[length1 * numColumns + length2]; } var isArray = /* @__PURE__ */ (() => Array.isArray)(); function isError(value) { return isTagged(value, "[object Error]"); } function isFunction(value) { return typeof value === "function"; } function isIterable(value) { return typeof value === "object" && value !== null && Symbol.iterator in value; } function isObject(value) { return isTagged(value, "[object Object]"); } function isPlainObject(value) { if (typeof value !== "object" || value === null) { return false; } const prototype = Object.getPrototypeOf(value); return ( // Fast path for most common objects. prototype === Object.prototype || // Support objects created without a prototype. prototype === null || // Support plain objects from other realms. Object.getPrototypeOf(prototype) === null ); } function isPromise(value) { return !!value && isFunction(value.then); } function isString(value) { return typeof value === "string"; } function isTagged(value, tag) { return Object.prototype.toString.call(value) === tag; } exports.__commonJS = __commonJS; exports.__toESM = __toESM; exports.flat = flat; exports.objectify = objectify; exports.select = select; exports.sift = sift; exports.unique = unique; exports.defer = defer; exports.reduce = reduce; exports.tryit = tryit; exports.memo = memo; exports.proxied = proxied; exports.noop = noop; exports.traverse = traverse; exports.similarity = similarity; exports.isArray = isArray; exports.isError = isError; exports.isFunction = isFunction; exports.isObject = isObject; exports.isString = isString;