Skip to content

Commit 3c4db2b

Browse files
feat: add array utility functions for enhanced array manipulation
- Introduced a new module `arrayUtility.js` containing various utility functions for array operations, including `indexOf`, `contains`, `replace`, `updateRange`, `clearEach`, `register`, `findIndex`, `forEach`, `applyTo`, `removeIndex`, `remove`, `repeat`, `range`, `rangeUntil`, `distinct`, and `flatten`. - Added source map file `arrayUtility.js.map` for better debugging and development experience. - Functions provide capabilities such as searching, replacing, and manipulating array elements, ensuring robust handling of edge cases and type checks.
1 parent b991278 commit 3c4db2b

File tree

7 files changed

+1119
-0
lines changed

7 files changed

+1119
-0
lines changed

dist/arrayUtility.d.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*!
2+
* @author electricessence / https://github.com/electricessence/
3+
* @license MIT
4+
*/
5+
import { ActionWithIndex, PredicateWithIndex, SelectorWithIndex } from '@tsdotnet/common-interfaces';
6+
import init from '@tsdotnet/array-init';
7+
import copy, { arrayCopyTo as copyTo } from '@tsdotnet/array-copy';
8+
import { EqualityComparison } from '@tsdotnet/compare';
9+
export { init, copy, copyTo };
10+
export interface ArrayLikeWritable<T> {
11+
length: number;
12+
[n: number]: T;
13+
}
14+
/**
15+
* Checks to see where the provided array contains an item/value.
16+
* If the array value is null, then -1 is returned.
17+
* @param array
18+
* @param item
19+
* @param {function?} equalityComparer
20+
* @returns {number}
21+
*/
22+
export declare function indexOf<T>(array: ArrayLike<T>, item: T, equalityComparer?: EqualityComparison<T>): number;
23+
/**
24+
* Checks to see if the provided array contains an item.
25+
* If the array value is null, then false is returned.
26+
* @param array
27+
* @param item
28+
* @param {function?} equalityComparer
29+
* @returns {boolean}
30+
*/
31+
export declare function contains<T>(array: ArrayLike<T>, item: T, equalityComparer?: EqualityComparison<T>): boolean;
32+
/**
33+
* Finds and replaces a value from an array. Will replaces all instances unless a maximum is specified.
34+
* @param array
35+
* @param old
36+
* @param newValue
37+
* @param max
38+
* @returns {number}
39+
*/
40+
export declare function replace<T>(array: ArrayLikeWritable<T>, old: T, newValue: T, max?: number): number;
41+
/**
42+
* Replaces values of an array across a range of indexes.
43+
* @param array
44+
* @param value
45+
* @param start
46+
* @param stop
47+
*/
48+
export declare function updateRange<T>(array: ArrayLike<T>, value: T, start?: number, stop?: number): void;
49+
/**
50+
* Clears (sets to null) values of an array across a range of indexes.
51+
* @param array
52+
* @param start
53+
* @param stop
54+
*/
55+
export declare function clearEach(array: ArrayLikeWritable<any>, start?: number, stop?: number): void;
56+
/**
57+
* Ensures a value exists within an array. If not found, adds to the end.
58+
* @param array
59+
* @param item
60+
* @param {function?} equalityComparer
61+
* @returns {boolean}
62+
*/
63+
export declare function register<T>(array: ArrayLikeWritable<T>, item: T, equalityComparer?: EqualityComparison<T>): boolean;
64+
/**
65+
* Returns the first index of which the provided predicate returns true.
66+
* Returns -1 if always false.
67+
* @param array
68+
* @param predicate
69+
* @returns {number}
70+
*/
71+
export declare function findIndex<T>(array: ArrayLike<T>, predicate: PredicateWithIndex<T>): number;
72+
/**
73+
* Allows for using "false" to cause forEach to break.
74+
* Can also be applied to a structure that indexes like an array, but may not be.
75+
* @param source
76+
* @param action
77+
*/
78+
export declare function forEach<T>(source: ArrayLike<T>, action: ActionWithIndex<T> | PredicateWithIndex<T>): void;
79+
/**
80+
* Is similar to Array.map() but instead of returning a new array, it updates the existing indexes.
81+
* Can also be applied to a structure that indexes like an array, but may not be.
82+
* @param target
83+
* @param fn
84+
*/
85+
export declare function applyTo<T>(target: ArrayLikeWritable<T>, fn: SelectorWithIndex<T, T>): void;
86+
/**
87+
* Removes an entry at a specified index.
88+
* @param array
89+
* @param index
90+
* @returns {boolean} True if the value was able to be removed.
91+
*/
92+
export declare function removeIndex<T>(array: T[], index: number): boolean;
93+
/**
94+
* Finds and removes a value from an array. Will remove all instances unless a maximum is specified.
95+
* @param array
96+
* @param value
97+
* @param max
98+
* @param {function?} equalityComparer
99+
* @returns {number} The number of times the value was found and removed.
100+
*/
101+
export declare function remove<T>(array: T[], value: T, max?: number, equalityComparer?: EqualityComparison<T>): number;
102+
/**
103+
* Simply repeats a value the number of times specified.
104+
* @param element
105+
* @param count
106+
* @returns {T[]}
107+
*/
108+
export declare function repeat<T>(element: T, count: number): T[];
109+
/**
110+
* Returns a range of numbers based upon the first value and the step value.
111+
* @param first
112+
* @param count
113+
* @param step
114+
* @returns {number[]}
115+
*/
116+
export declare function range(first: number, count: number, step?: number): number[];
117+
/**
118+
* Returns a range of numbers based upon the first value and the step value excluding any numbers at or beyond the until value.
119+
* @param first
120+
* @param until
121+
* @param step
122+
* @returns {number[]}
123+
*/
124+
export declare function rangeUntil(first: number, until: number, step?: number): number[];
125+
/**
126+
* Returns a unique reduced set of values.
127+
* @param source
128+
*/
129+
export declare function distinct(source: string[] | null): string[];
130+
export declare function distinct(source: number[] | null): number[];
131+
/**
132+
* Takes any arrays within an array and inserts the values contained within in place of that array.
133+
* For every count higher than 0 in recurseDepth it will attempt an additional pass. Passing Infinity will flatten all arrays contained.
134+
* @param a
135+
* @param recurseDepth
136+
* @returns {any[]}
137+
*/
138+
export declare function flatten(a: any[], recurseDepth?: number): any[];

0 commit comments

Comments
 (0)