@oscarpalmer/atoms 0.156.0 → 0.157.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/index.js +2 -1
- package/dist/array/slice.js +51 -0
- package/dist/atoms.full.js +50 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
- package/src/array/index.ts +1 -0
- package/src/array/slice.ts +245 -0
- package/types/array/index.d.ts +1 -0
- package/types/array/slice.d.ts +82 -0
package/dist/array/index.js
CHANGED
|
@@ -14,10 +14,11 @@ import { intersection } from "./intersection.js";
|
|
|
14
14
|
import { partition } from "./partition.js";
|
|
15
15
|
import { push } from "./push.js";
|
|
16
16
|
import { select } from "./select.js";
|
|
17
|
+
import { drop, slice, take } from "./slice.js";
|
|
17
18
|
import { sort } from "./sort.js";
|
|
18
19
|
import { splice } from "./splice.js";
|
|
19
20
|
import { toSet } from "./to-set.js";
|
|
20
21
|
import { toggle } from "./toggle.js";
|
|
21
22
|
import { union } from "./union.js";
|
|
22
23
|
import { update } from "./update.js";
|
|
23
|
-
export { chunk, compact, difference, exists, filter, find, flatten, getArray, indexOf, insert, intersection, partition, push, range, select, shuffle, sort, splice, times, toSet, toggle, union, update };
|
|
24
|
+
export { chunk, compact, difference, drop, exists, filter, find, flatten, getArray, indexOf, insert, intersection, partition, push, range, select, shuffle, slice, sort, splice, take, times, toSet, toggle, union, update };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getArrayCallbacks } from "../internal/array/callbacks.js";
|
|
2
|
+
function drop(array, first, second) {
|
|
3
|
+
return extract(EXTRACT_DROP, array, first, second);
|
|
4
|
+
}
|
|
5
|
+
function extract(type, array, first, second) {
|
|
6
|
+
if (!Array.isArray(array)) return [];
|
|
7
|
+
const { length } = array;
|
|
8
|
+
if (length === 0) return [];
|
|
9
|
+
const isTake = type === EXTRACT_TAKE;
|
|
10
|
+
if (typeof first === "number") {
|
|
11
|
+
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
12
|
+
if (first === 0) return isTake ? [] : array.slice();
|
|
13
|
+
if (isTake) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
14
|
+
return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
|
|
15
|
+
}
|
|
16
|
+
const callbacks = second == null ? getArrayCallbacks(first) : getArrayCallbacks(void 0, void 0, first);
|
|
17
|
+
const isBoolean = callbacks?.bool != null;
|
|
18
|
+
if (callbacks?.bool == null && callbacks?.value == null) return isTake ? array.slice() : [];
|
|
19
|
+
const extracted = [];
|
|
20
|
+
let push = false;
|
|
21
|
+
for (let index = 0; index < length; index += 1) {
|
|
22
|
+
const item = array[index];
|
|
23
|
+
const matches = isBoolean ? callbacks.bool(item, index, array) : Object.is(callbacks.value(item, index, array), second);
|
|
24
|
+
if (isTake) {
|
|
25
|
+
if (isBoolean ? !matches : matches) break;
|
|
26
|
+
extracted.push(item);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (push) extracted.push(item);
|
|
30
|
+
else if (isBoolean ? !matches : matches) {
|
|
31
|
+
push = true;
|
|
32
|
+
if (isBoolean) extracted.push(item);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return extracted;
|
|
36
|
+
}
|
|
37
|
+
function slice(array, first, second) {
|
|
38
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
39
|
+
const firstIsNumber = typeof first === "number";
|
|
40
|
+
const secondIsNumber = typeof second === "number";
|
|
41
|
+
if (!firstIsNumber && !secondIsNumber) return array.slice();
|
|
42
|
+
if (!secondIsNumber) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
43
|
+
if (!firstIsNumber) return array.slice();
|
|
44
|
+
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
45
|
+
}
|
|
46
|
+
function take(array, first, second) {
|
|
47
|
+
return extract(EXTRACT_TAKE, array, first, second);
|
|
48
|
+
}
|
|
49
|
+
var EXTRACT_DROP = "drop";
|
|
50
|
+
var EXTRACT_TAKE = "take";
|
|
51
|
+
export { drop, slice, take };
|
package/dist/atoms.full.js
CHANGED
|
@@ -381,6 +381,55 @@ function push(array, pushed) {
|
|
|
381
381
|
function select(array, ...parameters) {
|
|
382
382
|
return findValues(FIND_VALUES_ALL, array, parameters, parameters.pop()).matched;
|
|
383
383
|
}
|
|
384
|
+
function drop(array, first, second) {
|
|
385
|
+
return extract(EXTRACT_DROP, array, first, second);
|
|
386
|
+
}
|
|
387
|
+
function extract(type, array, first, second) {
|
|
388
|
+
if (!Array.isArray(array)) return [];
|
|
389
|
+
const { length } = array;
|
|
390
|
+
if (length === 0) return [];
|
|
391
|
+
const isTake = type === EXTRACT_TAKE;
|
|
392
|
+
if (typeof first === "number") {
|
|
393
|
+
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
394
|
+
if (first === 0) return isTake ? [] : array.slice();
|
|
395
|
+
if (isTake) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
396
|
+
return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
|
|
397
|
+
}
|
|
398
|
+
const callbacks = second == null ? getArrayCallbacks(first) : getArrayCallbacks(void 0, void 0, first);
|
|
399
|
+
const isBoolean = callbacks?.bool != null;
|
|
400
|
+
if (callbacks?.bool == null && callbacks?.value == null) return isTake ? array.slice() : [];
|
|
401
|
+
const extracted = [];
|
|
402
|
+
let push = false;
|
|
403
|
+
for (let index = 0; index < length; index += 1) {
|
|
404
|
+
const item = array[index];
|
|
405
|
+
const matches = isBoolean ? callbacks.bool(item, index, array) : Object.is(callbacks.value(item, index, array), second);
|
|
406
|
+
if (isTake) {
|
|
407
|
+
if (isBoolean ? !matches : matches) break;
|
|
408
|
+
extracted.push(item);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (push) extracted.push(item);
|
|
412
|
+
else if (isBoolean ? !matches : matches) {
|
|
413
|
+
push = true;
|
|
414
|
+
if (isBoolean) extracted.push(item);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return extracted;
|
|
418
|
+
}
|
|
419
|
+
function slice(array, first, second) {
|
|
420
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
421
|
+
const firstIsNumber = typeof first === "number";
|
|
422
|
+
const secondIsNumber = typeof second === "number";
|
|
423
|
+
if (!firstIsNumber && !secondIsNumber) return array.slice();
|
|
424
|
+
if (!secondIsNumber) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
425
|
+
if (!firstIsNumber) return array.slice();
|
|
426
|
+
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
427
|
+
}
|
|
428
|
+
function take(array, first, second) {
|
|
429
|
+
return extract(EXTRACT_TAKE, array, first, second);
|
|
430
|
+
}
|
|
431
|
+
const EXTRACT_DROP = "drop";
|
|
432
|
+
const EXTRACT_TAKE = "take";
|
|
384
433
|
function aggregate(type, array, key) {
|
|
385
434
|
const length = Array.isArray(array) ? array.length : 0;
|
|
386
435
|
if (length === 0) return {
|
|
@@ -3874,4 +3923,4 @@ var SizedSet = class extends Set {
|
|
|
3874
3923
|
}
|
|
3875
3924
|
}
|
|
3876
3925
|
};
|
|
3877
|
-
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
|
3926
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, drop, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, slice, smush, snakeCase, sort, splice, startsWith, sum, take, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { intersection } from "./array/intersection.js";
|
|
|
17
17
|
import { partition } from "./array/partition.js";
|
|
18
18
|
import { push } from "./array/push.js";
|
|
19
19
|
import { select } from "./array/select.js";
|
|
20
|
+
import { drop, slice, take } from "./array/slice.js";
|
|
20
21
|
import { max } from "./internal/math/aggregate.js";
|
|
21
22
|
import { getString, ignoreKey, join, tryDecode, tryEncode, words } from "./internal/string.js";
|
|
22
23
|
import { compare } from "./internal/value/compare.js";
|
|
@@ -75,4 +76,4 @@ import { QueueError, queue } from "./queue.js";
|
|
|
75
76
|
import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
|
|
76
77
|
import { attempt } from "./result/index.js";
|
|
77
78
|
import { SizedSet } from "./sized/set.js";
|
|
78
|
-
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
|
79
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, drop, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, slice, smush, snakeCase, sort, splice, startsWith, sum, take, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
package/package.json
CHANGED
package/src/array/index.ts
CHANGED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {getArrayCallbacks} from '../internal/array/callbacks';
|
|
2
|
+
import type {PlainObject} from '../models';
|
|
3
|
+
|
|
4
|
+
// #region Types
|
|
5
|
+
|
|
6
|
+
type ExtractType = 'drop' | 'take';
|
|
7
|
+
|
|
8
|
+
// #endregion
|
|
9
|
+
|
|
10
|
+
// #region Functions
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Drop items from the start of an array until they match a value
|
|
14
|
+
* @param array Original array
|
|
15
|
+
* @param key Key to get an item's value for matching
|
|
16
|
+
* @param value Value to match against
|
|
17
|
+
* @returns New array with items dropped
|
|
18
|
+
*/
|
|
19
|
+
export function drop<Item extends PlainObject, Key extends keyof Item>(
|
|
20
|
+
array: Item[],
|
|
21
|
+
key: Key,
|
|
22
|
+
value: Item[Key],
|
|
23
|
+
): Item[];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Drop items from the start of an array until they match a value
|
|
27
|
+
* @param array Original array
|
|
28
|
+
* @param callback Callback to get an item's value for matching
|
|
29
|
+
* @param value Value to match against
|
|
30
|
+
* @return New array with items dropped
|
|
31
|
+
*/
|
|
32
|
+
export function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
|
|
33
|
+
array: Item[],
|
|
34
|
+
callback: Callback,
|
|
35
|
+
value: ReturnType<Callback>,
|
|
36
|
+
): Item[];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Drop items from the start of an array while they match a filter
|
|
40
|
+
* @param array Original array
|
|
41
|
+
* @param callback Filter callback to match items
|
|
42
|
+
* @return New array with items dropped
|
|
43
|
+
*/
|
|
44
|
+
export function drop<Item extends PlainObject>(
|
|
45
|
+
array: Item[],
|
|
46
|
+
callback: (item: Item, index: number, array: Item[]) => boolean,
|
|
47
|
+
): Item[];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Drop a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
51
|
+
* @param array Original array
|
|
52
|
+
* @param count Number of items to drop
|
|
53
|
+
* @returns New array with items dropped
|
|
54
|
+
*/
|
|
55
|
+
export function drop(array: unknown[], count: number): unknown[];
|
|
56
|
+
|
|
57
|
+
export function drop(array: unknown[], first?: unknown, second?: unknown): unknown[] {
|
|
58
|
+
return extract(EXTRACT_DROP, array, first, second);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function extract(
|
|
62
|
+
type: ExtractType,
|
|
63
|
+
array: unknown[],
|
|
64
|
+
first?: unknown,
|
|
65
|
+
second?: unknown,
|
|
66
|
+
): unknown[] {
|
|
67
|
+
if (!Array.isArray(array)) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const {length} = array;
|
|
72
|
+
|
|
73
|
+
if (length === 0) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const isTake = type === EXTRACT_TAKE;
|
|
78
|
+
|
|
79
|
+
if (typeof first === 'number') {
|
|
80
|
+
if (Math.abs(first) >= length) {
|
|
81
|
+
return isTake ? array.slice() : [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (first === 0) {
|
|
85
|
+
return isTake ? [] : array.slice();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (isTake) {
|
|
89
|
+
return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const callbacks =
|
|
96
|
+
second == null ? getArrayCallbacks(first) : getArrayCallbacks(undefined, undefined, first);
|
|
97
|
+
|
|
98
|
+
const isBoolean = callbacks?.bool != null;
|
|
99
|
+
|
|
100
|
+
if (callbacks?.bool == null && callbacks?.value == null) {
|
|
101
|
+
return isTake ? array.slice() : [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const extracted: unknown[] = [];
|
|
105
|
+
|
|
106
|
+
let push = false;
|
|
107
|
+
|
|
108
|
+
for (let index = 0; index < length; index += 1) {
|
|
109
|
+
const item = array[index];
|
|
110
|
+
|
|
111
|
+
const matches = isBoolean
|
|
112
|
+
? callbacks.bool!(item, index, array)
|
|
113
|
+
: Object.is(callbacks!.value!(item, index, array), second);
|
|
114
|
+
|
|
115
|
+
if (isTake) {
|
|
116
|
+
if (isBoolean ? !matches : matches) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
extracted.push(item);
|
|
121
|
+
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (push) {
|
|
126
|
+
extracted.push(item);
|
|
127
|
+
} else if (isBoolean ? !matches : matches) {
|
|
128
|
+
push = true;
|
|
129
|
+
|
|
130
|
+
if (isBoolean) {
|
|
131
|
+
extracted.push(item);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return extracted;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Slice an array, returning a new array with a specified range of items
|
|
141
|
+
* @param array Original array
|
|
142
|
+
* @param start Start index _(inclusive)_
|
|
143
|
+
* @param end End index _(exclusive)_
|
|
144
|
+
* @return New array with sliced items
|
|
145
|
+
*/
|
|
146
|
+
export function slice<Item>(array: Item[], start: number, end: number): Item[];
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Slice an array, returning a new array with a specified number of items
|
|
150
|
+
* @param array Original array
|
|
151
|
+
* @param count Maximum sixe of the new array
|
|
152
|
+
* @return New array with sliced items
|
|
153
|
+
*/
|
|
154
|
+
export function slice<Item>(array: Item[], count: number): Item[];
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Slice an array
|
|
158
|
+
* @param array Array to slice
|
|
159
|
+
* @returns Sliced array
|
|
160
|
+
*/
|
|
161
|
+
export function slice<Item>(array: Item[]): Item[];
|
|
162
|
+
|
|
163
|
+
export function slice(array: unknown[], first?: number, second?: number): unknown[] {
|
|
164
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const firstIsNumber = typeof first === 'number';
|
|
169
|
+
const secondIsNumber = typeof second === 'number';
|
|
170
|
+
|
|
171
|
+
if (!firstIsNumber && !secondIsNumber) {
|
|
172
|
+
return array.slice();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!secondIsNumber) {
|
|
176
|
+
return first! >= 0 ? array.slice(0, first) : array.slice(array.length + first!);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!firstIsNumber) {
|
|
180
|
+
return array.slice();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return first! >= 0
|
|
184
|
+
? array.slice(first!, second!)
|
|
185
|
+
: array.slice(array.length + first!, array.length + second!);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Take items from the start of an array until they match a value
|
|
190
|
+
* @param array Original array
|
|
191
|
+
* @param key Key to get an item's value for matching
|
|
192
|
+
* @param value Value to match against
|
|
193
|
+
* @returns New array with taken items
|
|
194
|
+
*/
|
|
195
|
+
export function take<Item extends PlainObject, Key extends keyof Item>(
|
|
196
|
+
array: Item[],
|
|
197
|
+
key: Key,
|
|
198
|
+
value: Item[Key],
|
|
199
|
+
): Item[];
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Take items from the start of an array until they match a value
|
|
203
|
+
* @param array Original array
|
|
204
|
+
* @param callback Callback to get an item's value for matching
|
|
205
|
+
* @param value Value to match against
|
|
206
|
+
* @return New array with taken items
|
|
207
|
+
*/
|
|
208
|
+
export function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
|
|
209
|
+
array: Item[],
|
|
210
|
+
callback: Callback,
|
|
211
|
+
value: ReturnType<Callback>,
|
|
212
|
+
): Item[];
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Take items from the start of an array while they match a filter
|
|
216
|
+
* @param array Original array
|
|
217
|
+
* @param callback Filter callback to match items
|
|
218
|
+
* @return New array with taken items
|
|
219
|
+
*/
|
|
220
|
+
export function take<Item extends PlainObject>(
|
|
221
|
+
array: Item[],
|
|
222
|
+
callback: (item: Item, index: number, array: Item[]) => boolean,
|
|
223
|
+
): Item[];
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Take a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
227
|
+
* @param array Original array
|
|
228
|
+
* @param count Number of items to take
|
|
229
|
+
* @returns New array with taken items
|
|
230
|
+
*/
|
|
231
|
+
export function take(array: unknown[], count: number): unknown[];
|
|
232
|
+
|
|
233
|
+
export function take(array: unknown[], first?: unknown, second?: unknown): unknown[] {
|
|
234
|
+
return extract(EXTRACT_TAKE, array, first, second);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// #endregion
|
|
238
|
+
|
|
239
|
+
// #region Variables
|
|
240
|
+
|
|
241
|
+
const EXTRACT_DROP: ExtractType = 'drop';
|
|
242
|
+
|
|
243
|
+
const EXTRACT_TAKE: ExtractType = 'take';
|
|
244
|
+
|
|
245
|
+
// #endregion
|
package/types/array/index.d.ts
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { PlainObject } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* Drop items from the start of an array until they match a value
|
|
4
|
+
* @param array Original array
|
|
5
|
+
* @param key Key to get an item's value for matching
|
|
6
|
+
* @param value Value to match against
|
|
7
|
+
* @returns New array with items dropped
|
|
8
|
+
*/
|
|
9
|
+
export declare function drop<Item extends PlainObject, Key extends keyof Item>(array: Item[], key: Key, value: Item[Key]): Item[];
|
|
10
|
+
/**
|
|
11
|
+
* Drop items from the start of an array until they match a value
|
|
12
|
+
* @param array Original array
|
|
13
|
+
* @param callback Callback to get an item's value for matching
|
|
14
|
+
* @param value Value to match against
|
|
15
|
+
* @return New array with items dropped
|
|
16
|
+
*/
|
|
17
|
+
export declare function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
|
|
18
|
+
/**
|
|
19
|
+
* Drop items from the start of an array while they match a filter
|
|
20
|
+
* @param array Original array
|
|
21
|
+
* @param callback Filter callback to match items
|
|
22
|
+
* @return New array with items dropped
|
|
23
|
+
*/
|
|
24
|
+
export declare function drop<Item extends PlainObject>(array: Item[], callback: (item: Item, index: number, array: Item[]) => boolean): Item[];
|
|
25
|
+
/**
|
|
26
|
+
* Drop a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
27
|
+
* @param array Original array
|
|
28
|
+
* @param count Number of items to drop
|
|
29
|
+
* @returns New array with items dropped
|
|
30
|
+
*/
|
|
31
|
+
export declare function drop(array: unknown[], count: number): unknown[];
|
|
32
|
+
/**
|
|
33
|
+
* Slice an array, returning a new array with a specified range of items
|
|
34
|
+
* @param array Original array
|
|
35
|
+
* @param start Start index _(inclusive)_
|
|
36
|
+
* @param end End index _(exclusive)_
|
|
37
|
+
* @return New array with sliced items
|
|
38
|
+
*/
|
|
39
|
+
export declare function slice<Item>(array: Item[], start: number, end: number): Item[];
|
|
40
|
+
/**
|
|
41
|
+
* Slice an array, returning a new array with a specified number of items
|
|
42
|
+
* @param array Original array
|
|
43
|
+
* @param count Maximum sixe of the new array
|
|
44
|
+
* @return New array with sliced items
|
|
45
|
+
*/
|
|
46
|
+
export declare function slice<Item>(array: Item[], count: number): Item[];
|
|
47
|
+
/**
|
|
48
|
+
* Slice an array
|
|
49
|
+
* @param array Array to slice
|
|
50
|
+
* @returns Sliced array
|
|
51
|
+
*/
|
|
52
|
+
export declare function slice<Item>(array: Item[]): Item[];
|
|
53
|
+
/**
|
|
54
|
+
* Take items from the start of an array until they match a value
|
|
55
|
+
* @param array Original array
|
|
56
|
+
* @param key Key to get an item's value for matching
|
|
57
|
+
* @param value Value to match against
|
|
58
|
+
* @returns New array with taken items
|
|
59
|
+
*/
|
|
60
|
+
export declare function take<Item extends PlainObject, Key extends keyof Item>(array: Item[], key: Key, value: Item[Key]): Item[];
|
|
61
|
+
/**
|
|
62
|
+
* Take items from the start of an array until they match a value
|
|
63
|
+
* @param array Original array
|
|
64
|
+
* @param callback Callback to get an item's value for matching
|
|
65
|
+
* @param value Value to match against
|
|
66
|
+
* @return New array with taken items
|
|
67
|
+
*/
|
|
68
|
+
export declare function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
|
|
69
|
+
/**
|
|
70
|
+
* Take items from the start of an array while they match a filter
|
|
71
|
+
* @param array Original array
|
|
72
|
+
* @param callback Filter callback to match items
|
|
73
|
+
* @return New array with taken items
|
|
74
|
+
*/
|
|
75
|
+
export declare function take<Item extends PlainObject>(array: Item[], callback: (item: Item, index: number, array: Item[]) => boolean): Item[];
|
|
76
|
+
/**
|
|
77
|
+
* Take a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
78
|
+
* @param array Original array
|
|
79
|
+
* @param count Number of items to take
|
|
80
|
+
* @returns New array with taken items
|
|
81
|
+
*/
|
|
82
|
+
export declare function take(array: unknown[], count: number): unknown[];
|