|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 1 | import { flattenDepth } from './flattenDepth.mjs';
|
|---|
| 2 | import { isIndex } from '../_internal/isIndex.mjs';
|
|---|
| 3 | import { isKey } from '../_internal/isKey.mjs';
|
|---|
| 4 | import { toKey } from '../_internal/toKey.mjs';
|
|---|
| 5 | import { at } from '../object/at.mjs';
|
|---|
| 6 | import { unset } from '../object/unset.mjs';
|
|---|
| 7 | import { isArray } from '../predicate/isArray.mjs';
|
|---|
| 8 | import { toPath } from '../util/toPath.mjs';
|
|---|
| 9 |
|
|---|
| 10 | function 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 |
|
|---|
| 34 | export { pullAt };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.