@cloudcome/utils-core 1.23.0 → 1.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array.cjs +47 -0
- package/dist/array.cjs.map +1 -1
- package/dist/array.d.ts +46 -0
- package/dist/array.mjs +47 -1
- package/dist/array.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/array.cjs
CHANGED
|
@@ -193,6 +193,52 @@ function arrayDiff(refArray, curArray, options) {
|
|
|
193
193
|
function arrayRemove(array, indexes) {
|
|
194
194
|
return array.filter((_item, index) => !indexes.includes(index));
|
|
195
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* 从数组中随机取样指定数量的元素。
|
|
198
|
+
*
|
|
199
|
+
* @template T - 数组元素的类型
|
|
200
|
+
* @param array - 要取样的数组。
|
|
201
|
+
* @param options - 取样选项。
|
|
202
|
+
* @returns 包含取样结果的新数组。
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const arr = [1, 2, 3, 4, 5];
|
|
207
|
+
* arraySample(arr);
|
|
208
|
+
* // => [3](随机取 1 个)
|
|
209
|
+
*
|
|
210
|
+
* arraySample(arr, { count: 3 });
|
|
211
|
+
* // => [5, 1, 3](无序、无放回取 3 个)
|
|
212
|
+
*
|
|
213
|
+
* arraySample(arr, { count: 3, ordered: true });
|
|
214
|
+
* // => [1, 3, 4](保持顺序、无放回)
|
|
215
|
+
*
|
|
216
|
+
* arraySample(arr, { count: 3, replacement: true });
|
|
217
|
+
* // => [4, 2, 4](无序、有放回,可能重复)
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
function arraySample(array, options) {
|
|
221
|
+
const { count = 1, ordered = false, replacement = false } = options || {};
|
|
222
|
+
let _count = Math.floor(count);
|
|
223
|
+
if (_count < 0) _count = 0;
|
|
224
|
+
const length = array.length;
|
|
225
|
+
if (length === 0 || _count === 0) return [];
|
|
226
|
+
if (replacement) {
|
|
227
|
+
const indices = [];
|
|
228
|
+
for (let i = 0; i < _count; i++) indices.push(Math.floor(Math.random() * length));
|
|
229
|
+
if (ordered) indices.sort((a, b) => a - b);
|
|
230
|
+
return indices.map((i) => array[i]);
|
|
231
|
+
}
|
|
232
|
+
const n = Math.min(_count, length);
|
|
233
|
+
const indices = Array.from({ length }, (_, i) => i);
|
|
234
|
+
for (let i = 0; i < n; i++) {
|
|
235
|
+
const j = i + Math.floor(Math.random() * (length - i));
|
|
236
|
+
[indices[i], indices[j]] = [indices[j], indices[i]];
|
|
237
|
+
}
|
|
238
|
+
const selected = indices.slice(0, n);
|
|
239
|
+
if (ordered) selected.sort((a, b) => a - b);
|
|
240
|
+
return selected.map((i) => array[i]);
|
|
241
|
+
}
|
|
196
242
|
//#endregion
|
|
197
243
|
exports.arrayDiff = arrayDiff;
|
|
198
244
|
exports.arrayEach = arrayEach;
|
|
@@ -201,6 +247,7 @@ exports.arrayMove = arrayMove;
|
|
|
201
247
|
exports.arrayOmit = arrayOmit;
|
|
202
248
|
exports.arrayPick = arrayPick;
|
|
203
249
|
exports.arrayRemove = arrayRemove;
|
|
250
|
+
exports.arraySample = arraySample;
|
|
204
251
|
exports.isArrayLike = isArrayLike;
|
|
205
252
|
|
|
206
253
|
//# sourceMappingURL=array.cjs.map
|
package/dist/array.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.cjs","names":[],"sources":["../src/array.ts"],"sourcesContent":["import { isArray, isObject } from './type';\nimport type { MaybePromise } from './types';\n\n/**\n * 检查给定的值是否为类数组对象。\n *\n * 类数组对象是指具有 `length` 属性且 `length` 属性为非负数的对象。\n *\n * @param unknown - 要检查的值。\n * @returns 如果值是类数组对象,则返回 `true`,否则返回 `false`。\n */\nexport function isArrayLike(unknown: unknown) {\n if (isArray(unknown)) return true;\n\n if (isObject(unknown)) {\n const arrayLike = unknown as { length: unknown };\n return typeof arrayLike.length === 'number' && arrayLike.length >= 0;\n }\n\n return false;\n}\n\n/**\n * 从数组中选择指定索引的元素。\n *\n * @param array - 要从中选择元素的数组。\n * @param indexes - 要选择的元素的索引数组。\n * @returns 包含指定索引元素的新数组。\n */\nexport function arrayPick<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return false;\n indexes2.splice(index, 1);\n return true;\n });\n}\n\n/**\n * 从数组中排除指定索引的元素。\n *\n * @param array - 要从中排除元素的数组。\n * @param indexes - 要排除的元素的索引数组。\n * @returns 包含排除指定索引元素后的新数组。\n */\nexport function arrayOmit<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return true;\n indexes2.splice(index, 1);\n return false;\n });\n}\n\n/**\n * 遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * arrayEach(arr, (item, index) => {\n * console.log(item, index);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport function arrayEach<T>(array: T[], iterator: (item: T, index: number) => false | unknown, reverse = false) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 异步遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的异步回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * await arrayEachAsync(arr, async (item, index) => {\n * await someAsyncOperation(item);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport async function arrayEachAsync<T>(\n array: T[],\n iterator: (item: T, index: number) => MaybePromise<false | unknown>,\n reverse = false,\n) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 将数组中的元素移动到指定位置。\n *\n * @param array - 要移动元素的数组。\n * @param from - 要移动的元素的起始位置。\n * @param to - 要移动的元素的目标位置。\n * @returns 新的数组,其中包含移动后的元素。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayMove(arr, 1, 3);\n * // 返回 [1, 3, 4, 2, 5]\n * ```\n */\nexport function arrayMove<T>(array: T[], from: number, to: number) {\n const array2 = [...array];\n\n if (from < 0 || from >= array2.length || to < 0 || to >= array2.length) {\n return array2;\n }\n\n const item = array2[from];\n\n array2.splice(from, 1);\n array2.splice(to, 0, item);\n\n return array2;\n}\n\n/**\n * 比较两个数组的差异,返回包含删除、新增和相同元素信息的对象\n *\n * @template T - 数组元素的类型\n * @param {T[]} refArray - 参考数组(原始数组)\n * @param {T[]} curArray - 当前数组(比较数组)\n * @returns {ArrayDiffs<T>} 包含差异信息的对象\n *\n * @example\n * ```typescript\n * const ref = [1, 2, 3];\n * const cur = [2, 3, 4];\n * const diff = arrayDiff(ref, cur);\n * // 返回结果:\n * // {\n * // deletes: [{refIndexes: [0], refValue: 1}],\n * // adds: [{curIndexes: [2], curValue: 4}],\n * // equals: [\n * // {refIndexes: [1], curIndexes: [0], refValue: 2, curValue: 2},\n * // {refIndexes: [2], curIndexes: [1], refValue: 3, curValue: 3}\n * // ]\n * // }\n * ```\n */\nexport type ArrayDiffs<T> = {\n /**\n * 被删除的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {T} refValue - 被删除的元素值\n */\n deletes: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 被删除的元素值\n * @type {T[]}\n */\n refValues: T[];\n }[];\n\n /**\n * 新增的元素列表\n * @type {Array}\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} curValue - 新增的元素值\n */\n adds: {\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 新增的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n\n /**\n * 相同的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} refValue - 参考数组中的元素值\n * @property {T} curValue - 当前数组中的元素值\n */\n equals: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 参考数组中的元素值\n * @type {T[]}\n */\n refValues: T[];\n /**\n * 当前数组中的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n};\n\nexport type ArrayDiffOptions<T> = {\n getItemKey: (item: T) => unknown;\n};\n\nexport function arrayDiff<T>(refArray: T[], curArray: T[], options?: ArrayDiffOptions<T>): ArrayDiffs<T> {\n const { getItemKey = (item: T) => item } = options || {};\n\n // biome-ignore lint/suspicious/noExplicitAny: 内部使用\n type Key = any;\n\n const toKeyIndexes = (map: Map<Key, number[]>, item: T) => {\n const key = getItemKey(item);\n return {\n key,\n indexes: map.get(key) || [],\n };\n };\n\n const buildMap = (arr: T[]) => {\n const map = new Map<Key, number[]>();\n\n arr.forEach((item, index) => {\n const { key, indexes } = toKeyIndexes(map, item);\n indexes.push(index);\n map.set(key, indexes);\n });\n\n return map;\n };\n\n const toIndexesArr = (arr: T[], indexes: number[]) => {\n return indexes.map((index) => arr[index]);\n };\n\n const refMap = buildMap(refArray);\n const curMap = buildMap(curArray);\n const deleteSet = new Set<Key>();\n const addSet = new Set<Key>();\n const equalSet = new Set<Key>();\n\n for (const key of refMap.keys()) {\n if (curMap.has(key)) {\n equalSet.add(key);\n } else {\n deleteSet.add(key);\n }\n }\n\n for (const key of curMap.keys()) {\n if (!refMap.has(key)) {\n addSet.add(key);\n }\n }\n\n return {\n deletes: [...deleteSet].map((key) => {\n const indexes = refMap.get(key) || [];\n return {\n refIndexes: indexes,\n refValues: toIndexesArr(refArray, indexes),\n };\n }),\n\n adds: [...addSet].map((key) => {\n const indexes = curMap.get(key) || [];\n return {\n curIndexes: indexes,\n curValues: toIndexesArr(curArray, indexes),\n };\n }),\n\n equals: [...equalSet].map((key) => {\n const refIndexes = refMap.get(key) || [];\n const curIndexes = curMap.get(key) || [];\n return {\n refIndexes,\n curIndexes,\n refValues: toIndexesArr(refArray, refIndexes),\n curValues: toIndexesArr(curArray, curIndexes),\n };\n }),\n };\n}\n\n/**\n * 从数组中移除指定索引的元素\n * @template T - 数组元素的类型\n * @param {T[]} array - 原始数组\n * @param {number[]} indexes - 要移除的元素索引数组\n * @returns {T[]} 移除指定索引元素后的新数组\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayRemove(arr, [1, 3]);\n * // 返回结果: [1, 3, 5]\n * ```\n */\nexport function arrayRemove<T>(array: T[], indexes: number[]) {\n return array.filter((_item, index) => !indexes.includes(index));\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAgB,YAAY,SAAkB;CAC5C,IAAI,aAAA,QAAQ,OAAO,GAAG,OAAO;CAE7B,IAAI,aAAA,SAAS,OAAO,GAAG;EACrB,MAAM,YAAY;EAClB,OAAO,OAAO,UAAU,WAAW,YAAY,UAAU,UAAU;CACrE;CAEA,OAAO;AACT;;;;;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OAAO;IACL,YAAY;IACZ,WAAW,aAAa,UAAU,OAAO;GAC3C;EACF,CAAC;EAED,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,QAAQ;GAC7B,MAAM,UAAU,OAAO,IAAI,GAAG,KAAK,CAAC;GACpC,OAAO;IACL,YAAY;IACZ,WAAW,aAAa,UAAU,OAAO;GAC3C;EACF,CAAC;EAED,QAAQ,CAAC,GAAG,QAAQ,EAAE,KAAK,QAAQ;GACjC,MAAM,aAAa,OAAO,IAAI,GAAG,KAAK,CAAC;GACvC,MAAM,aAAa,OAAO,IAAI,GAAG,KAAK,CAAC;GACvC,OAAO;IACL;IACA;IACA,WAAW,aAAa,UAAU,UAAU;IAC5C,WAAW,aAAa,UAAU,UAAU;GAC9C;EACF,CAAC;CACH;AACF;;;;;;;;;;;;;;;AAgBA,SAAgB,YAAe,OAAY,SAAmB;CAC5D,OAAO,MAAM,QAAQ,OAAO,UAAU,CAAC,QAAQ,SAAS,KAAK,CAAC;AAChE"}
|
|
1
|
+
{"version":3,"file":"array.cjs","names":[],"sources":["../src/array.ts"],"sourcesContent":["import { isArray, isObject } from './type';\nimport type { MaybePromise } from './types';\n\n/**\n * 检查给定的值是否为类数组对象。\n *\n * 类数组对象是指具有 `length` 属性且 `length` 属性为非负数的对象。\n *\n * @param unknown - 要检查的值。\n * @returns 如果值是类数组对象,则返回 `true`,否则返回 `false`。\n */\nexport function isArrayLike(unknown: unknown) {\n if (isArray(unknown)) return true;\n\n if (isObject(unknown)) {\n const arrayLike = unknown as { length: unknown };\n return typeof arrayLike.length === 'number' && arrayLike.length >= 0;\n }\n\n return false;\n}\n\n/**\n * 从数组中选择指定索引的元素。\n *\n * @param array - 要从中选择元素的数组。\n * @param indexes - 要选择的元素的索引数组。\n * @returns 包含指定索引元素的新数组。\n */\nexport function arrayPick<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return false;\n indexes2.splice(index, 1);\n return true;\n });\n}\n\n/**\n * 从数组中排除指定索引的元素。\n *\n * @param array - 要从中排除元素的数组。\n * @param indexes - 要排除的元素的索引数组。\n * @returns 包含排除指定索引元素后的新数组。\n */\nexport function arrayOmit<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return true;\n indexes2.splice(index, 1);\n return false;\n });\n}\n\n/**\n * 遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * arrayEach(arr, (item, index) => {\n * console.log(item, index);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport function arrayEach<T>(array: T[], iterator: (item: T, index: number) => false | unknown, reverse = false) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 异步遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的异步回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * await arrayEachAsync(arr, async (item, index) => {\n * await someAsyncOperation(item);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport async function arrayEachAsync<T>(\n array: T[],\n iterator: (item: T, index: number) => MaybePromise<false | unknown>,\n reverse = false,\n) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 将数组中的元素移动到指定位置。\n *\n * @param array - 要移动元素的数组。\n * @param from - 要移动的元素的起始位置。\n * @param to - 要移动的元素的目标位置。\n * @returns 新的数组,其中包含移动后的元素。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayMove(arr, 1, 3);\n * // 返回 [1, 3, 4, 2, 5]\n * ```\n */\nexport function arrayMove<T>(array: T[], from: number, to: number) {\n const array2 = [...array];\n\n if (from < 0 || from >= array2.length || to < 0 || to >= array2.length) {\n return array2;\n }\n\n const item = array2[from];\n\n array2.splice(from, 1);\n array2.splice(to, 0, item);\n\n return array2;\n}\n\n/**\n * 比较两个数组的差异,返回包含删除、新增和相同元素信息的对象\n *\n * @template T - 数组元素的类型\n * @param {T[]} refArray - 参考数组(原始数组)\n * @param {T[]} curArray - 当前数组(比较数组)\n * @returns {ArrayDiffs<T>} 包含差异信息的对象\n *\n * @example\n * ```typescript\n * const ref = [1, 2, 3];\n * const cur = [2, 3, 4];\n * const diff = arrayDiff(ref, cur);\n * // 返回结果:\n * // {\n * // deletes: [{refIndexes: [0], refValue: 1}],\n * // adds: [{curIndexes: [2], curValue: 4}],\n * // equals: [\n * // {refIndexes: [1], curIndexes: [0], refValue: 2, curValue: 2},\n * // {refIndexes: [2], curIndexes: [1], refValue: 3, curValue: 3}\n * // ]\n * // }\n * ```\n */\nexport type ArrayDiffs<T> = {\n /**\n * 被删除的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {T} refValue - 被删除的元素值\n */\n deletes: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 被删除的元素值\n * @type {T[]}\n */\n refValues: T[];\n }[];\n\n /**\n * 新增的元素列表\n * @type {Array}\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} curValue - 新增的元素值\n */\n adds: {\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 新增的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n\n /**\n * 相同的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} refValue - 参考数组中的元素值\n * @property {T} curValue - 当前数组中的元素值\n */\n equals: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 参考数组中的元素值\n * @type {T[]}\n */\n refValues: T[];\n /**\n * 当前数组中的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n};\n\nexport type ArrayDiffOptions<T> = {\n getItemKey: (item: T) => unknown;\n};\n\nexport function arrayDiff<T>(refArray: T[], curArray: T[], options?: ArrayDiffOptions<T>): ArrayDiffs<T> {\n const { getItemKey = (item: T) => item } = options || {};\n\n // biome-ignore lint/suspicious/noExplicitAny: 内部使用\n type Key = any;\n\n const toKeyIndexes = (map: Map<Key, number[]>, item: T) => {\n const key = getItemKey(item);\n return {\n key,\n indexes: map.get(key) || [],\n };\n };\n\n const buildMap = (arr: T[]) => {\n const map = new Map<Key, number[]>();\n\n arr.forEach((item, index) => {\n const { key, indexes } = toKeyIndexes(map, item);\n indexes.push(index);\n map.set(key, indexes);\n });\n\n return map;\n };\n\n const toIndexesArr = (arr: T[], indexes: number[]) => {\n return indexes.map((index) => arr[index]);\n };\n\n const refMap = buildMap(refArray);\n const curMap = buildMap(curArray);\n const deleteSet = new Set<Key>();\n const addSet = new Set<Key>();\n const equalSet = new Set<Key>();\n\n for (const key of refMap.keys()) {\n if (curMap.has(key)) {\n equalSet.add(key);\n } else {\n deleteSet.add(key);\n }\n }\n\n for (const key of curMap.keys()) {\n if (!refMap.has(key)) {\n addSet.add(key);\n }\n }\n\n return {\n deletes: [...deleteSet].map((key) => {\n const indexes = refMap.get(key) || [];\n return {\n refIndexes: indexes,\n refValues: toIndexesArr(refArray, indexes),\n };\n }),\n\n adds: [...addSet].map((key) => {\n const indexes = curMap.get(key) || [];\n return {\n curIndexes: indexes,\n curValues: toIndexesArr(curArray, indexes),\n };\n }),\n\n equals: [...equalSet].map((key) => {\n const refIndexes = refMap.get(key) || [];\n const curIndexes = curMap.get(key) || [];\n return {\n refIndexes,\n curIndexes,\n refValues: toIndexesArr(refArray, refIndexes),\n curValues: toIndexesArr(curArray, curIndexes),\n };\n }),\n };\n}\n\n/**\n * 从数组中移除指定索引的元素\n * @template T - 数组元素的类型\n * @param {T[]} array - 原始数组\n * @param {number[]} indexes - 要移除的元素索引数组\n * @returns {T[]} 移除指定索引元素后的新数组\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayRemove(arr, [1, 3]);\n * // 返回结果: [1, 3, 5]\n * ```\n */\nexport function arrayRemove<T>(array: T[], indexes: number[]) {\n return array.filter((_item, index) => !indexes.includes(index));\n}\n\nexport type ArraySampleOptions = {\n /**\n * 取样数量,默认 1。\n * 小数向下取整,负数归零。\n */\n count?: number;\n /**\n * 是否保持原数组顺序。\n * 为 `true` 时,结果元素按原数组中的相对顺序排列。\n * 为 `false` 时,结果顺序随机。\n * @default false\n */\n ordered?: boolean;\n /**\n * 是否有放回取样。\n * 为 `true` 时,同一元素可能被多次选中。\n * 为 `false` 时,每个元素至多被选中一次。\n * @default false\n */\n replacement?: boolean;\n};\n\n/**\n * 从数组中随机取样指定数量的元素。\n *\n * @template T - 数组元素的类型\n * @param array - 要取样的数组。\n * @param options - 取样选项。\n * @returns 包含取样结果的新数组。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * arraySample(arr);\n * // => [3](随机取 1 个)\n *\n * arraySample(arr, { count: 3 });\n * // => [5, 1, 3](无序、无放回取 3 个)\n *\n * arraySample(arr, { count: 3, ordered: true });\n * // => [1, 3, 4](保持顺序、无放回)\n *\n * arraySample(arr, { count: 3, replacement: true });\n * // => [4, 2, 4](无序、有放回,可能重复)\n * ```\n */\nexport function arraySample<T>(array: T[], options?: ArraySampleOptions): T[] {\n const { count = 1, ordered = false, replacement = false } = options || {};\n\n let _count = Math.floor(count);\n if (_count < 0) _count = 0;\n\n const length = array.length;\n\n if (length === 0 || _count === 0) return [];\n\n // 有放回取样\n if (replacement) {\n const indices: number[] = [];\n\n for (let i = 0; i < _count; i++) {\n indices.push(Math.floor(Math.random() * length));\n }\n\n if (ordered) {\n indices.sort((a, b) => a - b);\n }\n\n return indices.map((i) => array[i]);\n }\n\n // 无放回取样:Fisher-Yates 部分洗牌,取前 n 个\n const n = Math.min(_count, length);\n const indices = Array.from({ length }, (_, i) => i);\n\n for (let i = 0; i < n; i++) {\n const j = i + Math.floor(Math.random() * (length - i));\n [indices[i], indices[j]] = [indices[j], indices[i]];\n }\n\n const selected = indices.slice(0, n);\n\n if (ordered) {\n selected.sort((a, b) => a - b);\n }\n\n return selected.map((i) => array[i]);\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAgB,YAAY,SAAkB;CAC5C,IAAI,aAAA,QAAQ,OAAO,GAAG,OAAO;CAE7B,IAAI,aAAA,SAAS,OAAO,GAAG;EACrB,MAAM,YAAY;EAClB,OAAO,OAAO,UAAU,WAAW,YAAY,UAAU,UAAU;CACrE;CAEA,OAAO;AACT;;;;;;;;AASA,SAAgB,UAAa,OAAY,SAAmB;CAC1D,MAAM,WAAW,CAAC,GAAG,OAAO;CAC5B,OAAO,MAAM,QAAQ,GAAG,MAAM;EAC5B,MAAM,QAAQ,SAAS,QAAQ,CAAC;EAChC,IAAI,UAAU,IAAI,OAAO;EACzB,SAAS,OAAO,OAAO,CAAC;EACxB,OAAO;CACT,CAAC;AACH;;;;;;;;AASA,SAAgB,UAAa,OAAY,SAAm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SAAS,MAAM,GAAG,MAAM,IAAI,CAAC;CAG/B,OAAO,SAAS,KAAK,MAAM,MAAM,EAAE;AACrC"}
|
package/dist/array.d.ts
CHANGED
|
@@ -187,3 +187,49 @@ export declare function arrayDiff<T>(refArray: T[], curArray: T[], options?: Arr
|
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
189
|
export declare function arrayRemove<T>(array: T[], indexes: number[]): T[];
|
|
190
|
+
export type ArraySampleOptions = {
|
|
191
|
+
/**
|
|
192
|
+
* 取样数量,默认 1。
|
|
193
|
+
* 小数向下取整,负数归零。
|
|
194
|
+
*/
|
|
195
|
+
count?: number;
|
|
196
|
+
/**
|
|
197
|
+
* 是否保持原数组顺序。
|
|
198
|
+
* 为 `true` 时,结果元素按原数组中的相对顺序排列。
|
|
199
|
+
* 为 `false` 时,结果顺序随机。
|
|
200
|
+
* @default false
|
|
201
|
+
*/
|
|
202
|
+
ordered?: boolean;
|
|
203
|
+
/**
|
|
204
|
+
* 是否有放回取样。
|
|
205
|
+
* 为 `true` 时,同一元素可能被多次选中。
|
|
206
|
+
* 为 `false` 时,每个元素至多被选中一次。
|
|
207
|
+
* @default false
|
|
208
|
+
*/
|
|
209
|
+
replacement?: boolean;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* 从数组中随机取样指定数量的元素。
|
|
213
|
+
*
|
|
214
|
+
* @template T - 数组元素的类型
|
|
215
|
+
* @param array - 要取样的数组。
|
|
216
|
+
* @param options - 取样选项。
|
|
217
|
+
* @returns 包含取样结果的新数组。
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* const arr = [1, 2, 3, 4, 5];
|
|
222
|
+
* arraySample(arr);
|
|
223
|
+
* // => [3](随机取 1 个)
|
|
224
|
+
*
|
|
225
|
+
* arraySample(arr, { count: 3 });
|
|
226
|
+
* // => [5, 1, 3](无序、无放回取 3 个)
|
|
227
|
+
*
|
|
228
|
+
* arraySample(arr, { count: 3, ordered: true });
|
|
229
|
+
* // => [1, 3, 4](保持顺序、无放回)
|
|
230
|
+
*
|
|
231
|
+
* arraySample(arr, { count: 3, replacement: true });
|
|
232
|
+
* // => [4, 2, 4](无序、有放回,可能重复)
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export declare function arraySample<T>(array: T[], options?: ArraySampleOptions): T[];
|
package/dist/array.mjs
CHANGED
|
@@ -192,7 +192,53 @@ function arrayDiff(refArray, curArray, options) {
|
|
|
192
192
|
function arrayRemove(array, indexes) {
|
|
193
193
|
return array.filter((_item, index) => !indexes.includes(index));
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* 从数组中随机取样指定数量的元素。
|
|
197
|
+
*
|
|
198
|
+
* @template T - 数组元素的类型
|
|
199
|
+
* @param array - 要取样的数组。
|
|
200
|
+
* @param options - 取样选项。
|
|
201
|
+
* @returns 包含取样结果的新数组。
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const arr = [1, 2, 3, 4, 5];
|
|
206
|
+
* arraySample(arr);
|
|
207
|
+
* // => [3](随机取 1 个)
|
|
208
|
+
*
|
|
209
|
+
* arraySample(arr, { count: 3 });
|
|
210
|
+
* // => [5, 1, 3](无序、无放回取 3 个)
|
|
211
|
+
*
|
|
212
|
+
* arraySample(arr, { count: 3, ordered: true });
|
|
213
|
+
* // => [1, 3, 4](保持顺序、无放回)
|
|
214
|
+
*
|
|
215
|
+
* arraySample(arr, { count: 3, replacement: true });
|
|
216
|
+
* // => [4, 2, 4](无序、有放回,可能重复)
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
function arraySample(array, options) {
|
|
220
|
+
const { count = 1, ordered = false, replacement = false } = options || {};
|
|
221
|
+
let _count = Math.floor(count);
|
|
222
|
+
if (_count < 0) _count = 0;
|
|
223
|
+
const length = array.length;
|
|
224
|
+
if (length === 0 || _count === 0) return [];
|
|
225
|
+
if (replacement) {
|
|
226
|
+
const indices = [];
|
|
227
|
+
for (let i = 0; i < _count; i++) indices.push(Math.floor(Math.random() * length));
|
|
228
|
+
if (ordered) indices.sort((a, b) => a - b);
|
|
229
|
+
return indices.map((i) => array[i]);
|
|
230
|
+
}
|
|
231
|
+
const n = Math.min(_count, length);
|
|
232
|
+
const indices = Array.from({ length }, (_, i) => i);
|
|
233
|
+
for (let i = 0; i < n; i++) {
|
|
234
|
+
const j = i + Math.floor(Math.random() * (length - i));
|
|
235
|
+
[indices[i], indices[j]] = [indices[j], indices[i]];
|
|
236
|
+
}
|
|
237
|
+
const selected = indices.slice(0, n);
|
|
238
|
+
if (ordered) selected.sort((a, b) => a - b);
|
|
239
|
+
return selected.map((i) => array[i]);
|
|
240
|
+
}
|
|
195
241
|
//#endregion
|
|
196
|
-
export { arrayDiff, arrayEach, arrayEachAsync, arrayMove, arrayOmit, arrayPick, arrayRemove, isArrayLike };
|
|
242
|
+
export { arrayDiff, arrayEach, arrayEachAsync, arrayMove, arrayOmit, arrayPick, arrayRemove, arraySample, isArrayLike };
|
|
197
243
|
|
|
198
244
|
//# sourceMappingURL=array.mjs.map
|
package/dist/array.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.mjs","names":[],"sources":["../src/array.ts"],"sourcesContent":["import { isArray, isObject } from './type';\nimport type { MaybePromise } from './types';\n\n/**\n * 检查给定的值是否为类数组对象。\n *\n * 类数组对象是指具有 `length` 属性且 `length` 属性为非负数的对象。\n *\n * @param unknown - 要检查的值。\n * @returns 如果值是类数组对象,则返回 `true`,否则返回 `false`。\n */\nexport function isArrayLike(unknown: unknown) {\n if (isArray(unknown)) return true;\n\n if (isObject(unknown)) {\n const arrayLike = unknown as { length: unknown };\n return typeof arrayLike.length === 'number' && arrayLike.length >= 0;\n }\n\n return false;\n}\n\n/**\n * 从数组中选择指定索引的元素。\n *\n * @param array - 要从中选择元素的数组。\n * @param indexes - 要选择的元素的索引数组。\n * @returns 包含指定索引元素的新数组。\n */\nexport function arrayPick<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return false;\n indexes2.splice(index, 1);\n return true;\n });\n}\n\n/**\n * 从数组中排除指定索引的元素。\n *\n * @param array - 要从中排除元素的数组。\n * @param indexes - 要排除的元素的索引数组。\n * @returns 包含排除指定索引元素后的新数组。\n */\nexport function arrayOmit<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return true;\n indexes2.splice(index, 1);\n return false;\n });\n}\n\n/**\n * 遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * arrayEach(arr, (item, index) => {\n * console.log(item, index);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport function arrayEach<T>(array: T[], iterator: (item: T, index: number) => false | unknown, reverse = false) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 异步遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的异步回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * await arrayEachAsync(arr, async (item, index) => {\n * await someAsyncOperation(item);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport async function arrayEachAsync<T>(\n array: T[],\n iterator: (item: T, index: number) => MaybePromise<false | unknown>,\n reverse = false,\n) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 将数组中的元素移动到指定位置。\n *\n * @param array - 要移动元素的数组。\n * @param from - 要移动的元素的起始位置。\n * @param to - 要移动的元素的目标位置。\n * @returns 新的数组,其中包含移动后的元素。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayMove(arr, 1, 3);\n * // 返回 [1, 3, 4, 2, 5]\n * ```\n */\nexport function arrayMove<T>(array: T[], from: number, to: number) {\n const array2 = [...array];\n\n if (from < 0 || from >= array2.length || to < 0 || to >= array2.length) {\n return array2;\n }\n\n const item = array2[from];\n\n array2.splice(from, 1);\n array2.splice(to, 0, item);\n\n return array2;\n}\n\n/**\n * 比较两个数组的差异,返回包含删除、新增和相同元素信息的对象\n *\n * @template T - 数组元素的类型\n * @param {T[]} refArray - 参考数组(原始数组)\n * @param {T[]} curArray - 当前数组(比较数组)\n * @returns {ArrayDiffs<T>} 包含差异信息的对象\n *\n * @example\n * ```typescript\n * const ref = [1, 2, 3];\n * const cur = [2, 3, 4];\n * const diff = arrayDiff(ref, cur);\n * // 返回结果:\n * // {\n * // deletes: [{refIndexes: [0], refValue: 1}],\n * // adds: [{curIndexes: [2], curValue: 4}],\n * // equals: [\n * // {refIndexes: [1], curIndexes: [0], refValue: 2, curValue: 2},\n * // {refIndexes: [2], curIndexes: [1], refValue: 3, curValue: 3}\n * // ]\n * // }\n * ```\n */\nexport type ArrayDiffs<T> = {\n /**\n * 被删除的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {T} refValue - 被删除的元素值\n */\n deletes: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 被删除的元素值\n * @type {T[]}\n */\n refValues: T[];\n }[];\n\n /**\n * 新增的元素列表\n * @type {Array}\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} curValue - 新增的元素值\n */\n adds: {\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 新增的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n\n /**\n * 相同的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} refValue - 参考数组中的元素值\n * @property {T} curValue - 当前数组中的元素值\n */\n equals: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 参考数组中的元素值\n * @type {T[]}\n */\n refValues: T[];\n /**\n * 当前数组中的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n};\n\nexport type ArrayDiffOptions<T> = {\n getItemKey: (item: T) => unknown;\n};\n\nexport function arrayDiff<T>(refArray: T[], curArray: T[], options?: ArrayDiffOptions<T>): ArrayDiffs<T> {\n const { getItemKey = (item: T) => item } = options || {};\n\n // biome-ignore lint/suspicious/noExplicitAny: 内部使用\n type Key = any;\n\n const toKeyIndexes = (map: Map<Key, number[]>, item: T) => {\n const key = getItemKey(item);\n return {\n key,\n indexes: map.get(key) || [],\n };\n };\n\n const buildMap = (arr: T[]) => {\n const map = new Map<Key, number[]>();\n\n arr.forEach((item, index) => {\n const { key, indexes } = toKeyIndexes(map, item);\n indexes.push(index);\n map.set(key, indexes);\n });\n\n return map;\n };\n\n const toIndexesArr = (arr: T[], indexes: number[]) => {\n return indexes.map((index) => arr[index]);\n };\n\n const refMap = buildMap(refArray);\n const curMap = buildMap(curArray);\n const deleteSet = new Set<Key>();\n const addSet = new Set<Key>();\n const equalSet = new Set<Key>();\n\n for (const key of refMap.keys()) {\n if (curMap.has(key)) {\n equalSet.add(key);\n } else {\n deleteSet.add(key);\n }\n }\n\n for (const key of curMap.keys()) {\n if (!refMap.has(key)) {\n addSet.add(key);\n }\n }\n\n return {\n deletes: [...deleteSet].map((key) => {\n const indexes = refMap.get(key) || [];\n return {\n refIndexes: indexes,\n refValues: toIndexesArr(refArray, indexes),\n };\n }),\n\n adds: [...addSet].map((key) => {\n const indexes = curMap.get(key) || [];\n return {\n curIndexes: indexes,\n curValues: toIndexesArr(curArray, indexes),\n };\n }),\n\n equals: [...equalSet].map((key) => {\n const refIndexes = refMap.get(key) || [];\n const curIndexes = curMap.get(key) || [];\n return {\n refIndexes,\n curIndexes,\n refValues: toIndexesArr(refArray, refIndexes),\n curValues: toIndexesArr(curArray, curIndexes),\n };\n }),\n };\n}\n\n/**\n * 从数组中移除指定索引的元素\n * @template T - 数组元素的类型\n * @param {T[]} array - 原始数组\n * @param {number[]} indexes - 要移除的元素索引数组\n * @returns {T[]} 移除指定索引元素后的新数组\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayRemove(arr, [1, 3]);\n * // 返回结果: [1, 3, 5]\n * ```\n */\nexport function arrayRemove<T>(array: T[], indexes: number[]) {\n return array.filter((_item, index) => !indexes.includes(index));\n}\n"],"mappings":";;;;;;;;;;AAWA,SAAgB,YAAY,SAAkB;CAC5C,IAAI,QAAQ,OAAO,GAAG,OAAO;CAE7B,IAAI,SAAS,OAAO,GAAG;EACrB,MAAM,YAAY;EAClB,OAAO,OAAO,UAAU,WAAW,YAAY,UAAU,UAAU;CACrE;CAEA,OAAO;AACT;;;;;;;;AASA,SAAgB,UAAa,OAAY,SAAmB;CAC1D,MAAM,WAAW,CAAC,GAAG,OAAO;CAC5B,OAAO,MAAM,QAAQ,GAAG,MAAM;EAC5B,MAAM,QAAQ,SAAS,QAAQ,CAAC;EAChC,IAAI,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GACpC,OAAO;IACL,YAAY;IACZ,WAAW,aAAa,UAAU,OAAO;GAC3C;EACF,CAAC;EAED,QAAQ,CAAC,GAAG,QAAQ,EAAE,KAAK,QAAQ;GACjC,MAAM,aAAa,OAAO,IAAI,GAAG,KAAK,CAAC;GACvC,MAAM,aAAa,OAAO,IAAI,GAAG,KAAK,CAAC;GACvC,OAAO;IACL;IACA;IACA,WAAW,aAAa,UAAU,UAAU;IAC5C,WAAW,aAAa,UAAU,UAAU;GAC9C;EACF,CAAC;CACH;AACF;;;;;;;;;;;;;;;AAgBA,SAAgB,YAAe,OAAY,SAAmB;CAC5D,OAAO,MAAM,QAAQ,OAAO,UAAU,CAAC,QAAQ,SAAS,KAAK,CAAC;AAChE"}
|
|
1
|
+
{"version":3,"file":"array.mjs","names":[],"sources":["../src/array.ts"],"sourcesContent":["import { isArray, isObject } from './type';\nimport type { MaybePromise } from './types';\n\n/**\n * 检查给定的值是否为类数组对象。\n *\n * 类数组对象是指具有 `length` 属性且 `length` 属性为非负数的对象。\n *\n * @param unknown - 要检查的值。\n * @returns 如果值是类数组对象,则返回 `true`,否则返回 `false`。\n */\nexport function isArrayLike(unknown: unknown) {\n if (isArray(unknown)) return true;\n\n if (isObject(unknown)) {\n const arrayLike = unknown as { length: unknown };\n return typeof arrayLike.length === 'number' && arrayLike.length >= 0;\n }\n\n return false;\n}\n\n/**\n * 从数组中选择指定索引的元素。\n *\n * @param array - 要从中选择元素的数组。\n * @param indexes - 要选择的元素的索引数组。\n * @returns 包含指定索引元素的新数组。\n */\nexport function arrayPick<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return false;\n indexes2.splice(index, 1);\n return true;\n });\n}\n\n/**\n * 从数组中排除指定索引的元素。\n *\n * @param array - 要从中排除元素的数组。\n * @param indexes - 要排除的元素的索引数组。\n * @returns 包含排除指定索引元素后的新数组。\n */\nexport function arrayOmit<T>(array: T[], indexes: number[]) {\n const indexes2 = [...indexes];\n return array.filter((_, i) => {\n const index = indexes2.indexOf(i);\n if (index === -1) return true;\n indexes2.splice(index, 1);\n return false;\n });\n}\n\n/**\n * 遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * arrayEach(arr, (item, index) => {\n * console.log(item, index);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport function arrayEach<T>(array: T[], iterator: (item: T, index: number) => false | unknown, reverse = false) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if (iterator(_array[i], i) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 异步遍历数组中的每个元素,并对每个元素执行提供的回调函数。\n *\n * @param array - 要遍历的数组。\n * @param iterator - 对每个元素执行的异步回调函数。如果回调函数返回 `false`,则提前终止遍历。\n * @param reverse - 是否以相反的顺序遍历数组。默认为 `false`。\n * @returns 无返回值。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3];\n * await arrayEachAsync(arr, async (item, index) => {\n * await someAsyncOperation(item);\n * if (index === 1) return false; // 提前终止遍历\n * });\n * ```\n */\nexport async function arrayEachAsync<T>(\n array: T[],\n iterator: (item: T, index: number) => MaybePromise<false | unknown>,\n reverse = false,\n) {\n const _array = [...array];\n const length = array.length;\n\n if (reverse) {\n for (let i = length - 1; i >= 0; i--) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n } else {\n for (let i = 0; i < length; i++) {\n if ((await iterator(_array[i], i)) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * 将数组中的元素移动到指定位置。\n *\n * @param array - 要移动元素的数组。\n * @param from - 要移动的元素的起始位置。\n * @param to - 要移动的元素的目标位置。\n * @returns 新的数组,其中包含移动后的元素。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayMove(arr, 1, 3);\n * // 返回 [1, 3, 4, 2, 5]\n * ```\n */\nexport function arrayMove<T>(array: T[], from: number, to: number) {\n const array2 = [...array];\n\n if (from < 0 || from >= array2.length || to < 0 || to >= array2.length) {\n return array2;\n }\n\n const item = array2[from];\n\n array2.splice(from, 1);\n array2.splice(to, 0, item);\n\n return array2;\n}\n\n/**\n * 比较两个数组的差异,返回包含删除、新增和相同元素信息的对象\n *\n * @template T - 数组元素的类型\n * @param {T[]} refArray - 参考数组(原始数组)\n * @param {T[]} curArray - 当前数组(比较数组)\n * @returns {ArrayDiffs<T>} 包含差异信息的对象\n *\n * @example\n * ```typescript\n * const ref = [1, 2, 3];\n * const cur = [2, 3, 4];\n * const diff = arrayDiff(ref, cur);\n * // 返回结果:\n * // {\n * // deletes: [{refIndexes: [0], refValue: 1}],\n * // adds: [{curIndexes: [2], curValue: 4}],\n * // equals: [\n * // {refIndexes: [1], curIndexes: [0], refValue: 2, curValue: 2},\n * // {refIndexes: [2], curIndexes: [1], refValue: 3, curValue: 3}\n * // ]\n * // }\n * ```\n */\nexport type ArrayDiffs<T> = {\n /**\n * 被删除的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {T} refValue - 被删除的元素值\n */\n deletes: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 被删除的元素值\n * @type {T[]}\n */\n refValues: T[];\n }[];\n\n /**\n * 新增的元素列表\n * @type {Array}\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} curValue - 新增的元素值\n */\n adds: {\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 新增的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n\n /**\n * 相同的元素列表\n * @type {Array}\n * @property {number[]} refIndexes - 元素在参考数组中的所有索引位置\n * @property {number[]} curIndexes - 元素在当前数组中的所有索引位置\n * @property {T} refValue - 参考数组中的元素值\n * @property {T} curValue - 当前数组中的元素值\n */\n equals: {\n /**\n * 元素在参考数组中的所有索引位置\n * @type {number[]}\n */\n refIndexes: number[];\n /**\n * 元素在当前数组中的所有索引位置\n * @type {number[]}\n */\n curIndexes: number[];\n /**\n * 参考数组中的元素值\n * @type {T[]}\n */\n refValues: T[];\n /**\n * 当前数组中的元素值\n * @type {T[]}\n */\n curValues: T[];\n }[];\n};\n\nexport type ArrayDiffOptions<T> = {\n getItemKey: (item: T) => unknown;\n};\n\nexport function arrayDiff<T>(refArray: T[], curArray: T[], options?: ArrayDiffOptions<T>): ArrayDiffs<T> {\n const { getItemKey = (item: T) => item } = options || {};\n\n // biome-ignore lint/suspicious/noExplicitAny: 内部使用\n type Key = any;\n\n const toKeyIndexes = (map: Map<Key, number[]>, item: T) => {\n const key = getItemKey(item);\n return {\n key,\n indexes: map.get(key) || [],\n };\n };\n\n const buildMap = (arr: T[]) => {\n const map = new Map<Key, number[]>();\n\n arr.forEach((item, index) => {\n const { key, indexes } = toKeyIndexes(map, item);\n indexes.push(index);\n map.set(key, indexes);\n });\n\n return map;\n };\n\n const toIndexesArr = (arr: T[], indexes: number[]) => {\n return indexes.map((index) => arr[index]);\n };\n\n const refMap = buildMap(refArray);\n const curMap = buildMap(curArray);\n const deleteSet = new Set<Key>();\n const addSet = new Set<Key>();\n const equalSet = new Set<Key>();\n\n for (const key of refMap.keys()) {\n if (curMap.has(key)) {\n equalSet.add(key);\n } else {\n deleteSet.add(key);\n }\n }\n\n for (const key of curMap.keys()) {\n if (!refMap.has(key)) {\n addSet.add(key);\n }\n }\n\n return {\n deletes: [...deleteSet].map((key) => {\n const indexes = refMap.get(key) || [];\n return {\n refIndexes: indexes,\n refValues: toIndexesArr(refArray, indexes),\n };\n }),\n\n adds: [...addSet].map((key) => {\n const indexes = curMap.get(key) || [];\n return {\n curIndexes: indexes,\n curValues: toIndexesArr(curArray, indexes),\n };\n }),\n\n equals: [...equalSet].map((key) => {\n const refIndexes = refMap.get(key) || [];\n const curIndexes = curMap.get(key) || [];\n return {\n refIndexes,\n curIndexes,\n refValues: toIndexesArr(refArray, refIndexes),\n curValues: toIndexesArr(curArray, curIndexes),\n };\n }),\n };\n}\n\n/**\n * 从数组中移除指定索引的元素\n * @template T - 数组元素的类型\n * @param {T[]} array - 原始数组\n * @param {number[]} indexes - 要移除的元素索引数组\n * @returns {T[]} 移除指定索引元素后的新数组\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * const newArr = arrayRemove(arr, [1, 3]);\n * // 返回结果: [1, 3, 5]\n * ```\n */\nexport function arrayRemove<T>(array: T[], indexes: number[]) {\n return array.filter((_item, index) => !indexes.includes(index));\n}\n\nexport type ArraySampleOptions = {\n /**\n * 取样数量,默认 1。\n * 小数向下取整,负数归零。\n */\n count?: number;\n /**\n * 是否保持原数组顺序。\n * 为 `true` 时,结果元素按原数组中的相对顺序排列。\n * 为 `false` 时,结果顺序随机。\n * @default false\n */\n ordered?: boolean;\n /**\n * 是否有放回取样。\n * 为 `true` 时,同一元素可能被多次选中。\n * 为 `false` 时,每个元素至多被选中一次。\n * @default false\n */\n replacement?: boolean;\n};\n\n/**\n * 从数组中随机取样指定数量的元素。\n *\n * @template T - 数组元素的类型\n * @param array - 要取样的数组。\n * @param options - 取样选项。\n * @returns 包含取样结果的新数组。\n *\n * @example\n * ```typescript\n * const arr = [1, 2, 3, 4, 5];\n * arraySample(arr);\n * // => [3](随机取 1 个)\n *\n * arraySample(arr, { count: 3 });\n * // => [5, 1, 3](无序、无放回取 3 个)\n *\n * arraySample(arr, { count: 3, ordered: true });\n * // => [1, 3, 4](保持顺序、无放回)\n *\n * arraySample(arr, { count: 3, replacement: true });\n * // => [4, 2, 4](无序、有放回,可能重复)\n * ```\n */\nexport function arraySample<T>(array: T[], options?: ArraySampleOptions): T[] {\n const { count = 1, ordered = false, replacement = false } = options || {};\n\n let _count = Math.floor(count);\n if (_count < 0) _count = 0;\n\n const length = array.length;\n\n if (length === 0 || _count === 0) return [];\n\n // 有放回取样\n if (replacement) {\n const indices: number[] = [];\n\n for (let i = 0; i < _count; i++) {\n indices.push(Math.floor(Math.random() * length));\n }\n\n if (ordered) {\n indices.sort((a, b) => a - b);\n }\n\n return indices.map((i) => array[i]);\n }\n\n // 无放回取样:Fisher-Yates 部分洗牌,取前 n 个\n const n = Math.min(_count, length);\n const indices = Array.from({ length }, (_, i) => i);\n\n for (let i = 0; i < n; i++) {\n const j = i + Math.floor(Math.random() * (length - i));\n [indices[i], indices[j]] = [indices[j], indices[i]];\n }\n\n const selected = indices.slice(0, n);\n\n if (ordered) {\n selected.sort((a, b) => a - b);\n }\n\n return selected.map((i) => array[i]);\n}\n"],"mappings":";;;;;;;;;;AAWA,SAAgB,YAAY,SAAkB;CAC5C,IAAI,QAAQ,OAAO,GAAG,OAAO;CAE7B,IAAI,SAAS,OAAO,GAAG;EACrB,MAAM,YAAY;EAClB,OAAO,OAAO,UAAU,WAAW,YAAY,UAAU,UAAU;CACrE;CAEA,OAAO;AACT;;;;;;;;AASA,SAAgB,UAAa,OAAY,SAAmB;CAC1D,MAAM,WAAW,CAAC,GAAG,OAAO;CAC5B,OAAO,MAAM,QAAQ,GAAG,MAAM;EAC5B,MAAM,QAAQ,SAAS,QAAQ,CAAC;EAChC,IAAI,UAAU,IAAI,OAAO;EACzB,SAAS,OAAO,OAAO,CAAC;EACxB,OAAO;CACT,CAAC;AACH;;;;;;;;AASA,SAAgB,UAAa,OAAY,SAAmB;CAC1D,MAAM,WAAW,CAAC,GAAG,OAAO;CAC5B,OAAO,MAAM,QAAQ,GAAG,MAAM;EAC5B,MAAM,QAAQ,SAAS,QAAQ,CAAC;EAChC,IAAI,UAAU,IAAI,OAAO;EACzB,SAAS,OAAO,OAAO,CAA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