source: node_modules/es-toolkit/dist/compat/array/pullAt.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { flattenDepth } from './flattenDepth.mjs';
2import { isIndex } from '../_internal/isIndex.mjs';
3import { isKey } from '../_internal/isKey.mjs';
4import { toKey } from '../_internal/toKey.mjs';
5import { at } from '../object/at.mjs';
6import { unset } from '../object/unset.mjs';
7import { isArray } from '../predicate/isArray.mjs';
8import { toPath } from '../util/toPath.mjs';
9
10function pullAt(array, ..._indices) {
11 const indices = flattenDepth(_indices, 1);
12 if (!array) {
13 return Array(indices.length);
14 }
15 const result = at(array, indices);
16 const indicesToPull = indices
17 .map(index => (isIndex(index, array.length) ? Number(index) : index))
18 .sort((a, b) => b - a);
19 for (const index of new Set(indicesToPull)) {
20 if (isIndex(index, array.length)) {
21 Array.prototype.splice.call(array, index, 1);
22 continue;
23 }
24 if (isKey(index, array)) {
25 delete array[toKey(index)];
26 continue;
27 }
28 const path = isArray(index) ? index : toPath(index);
29 unset(array, path);
30 }
31 return result;
32}
33
34export { pullAt };
Note: See TracBrowser for help on using the repository browser.