@tempots/std 0.19.0 → 0.21.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/array.cjs +1 -1
- package/array.d.ts +124 -284
- package/array.js +108 -128
- package/date.cjs +1 -0
- package/date.d.ts +393 -0
- package/date.js +49 -0
- package/function.cjs +1 -1
- package/function.d.ts +145 -0
- package/function.js +35 -5
- package/index.cjs +1 -1
- package/index.d.ts +6 -0
- package/index.js +272 -199
- package/iterator.cjs +1 -0
- package/iterator.d.ts +219 -0
- package/iterator.js +69 -0
- package/json.cjs +1 -1
- package/json.d.ts +40 -2
- package/json.js +7 -6
- package/map.cjs +1 -0
- package/map.d.ts +202 -0
- package/map.js +41 -0
- package/number.cjs +1 -1
- package/number.d.ts +16 -25
- package/number.js +26 -27
- package/object.cjs +1 -1
- package/object.d.ts +146 -8
- package/object.js +42 -15
- package/package.json +43 -1
- package/random.cjs +1 -0
- package/random.d.ts +200 -0
- package/random.js +72 -0
- package/set.cjs +1 -0
- package/set.d.ts +227 -0
- package/set.js +52 -0
- package/string.cjs +4 -4
- package/string.d.ts +2 -60
- package/string.js +122 -129
- package/timer.cjs +1 -1
- package/timer.d.ts +14 -0
- package/timer.js +49 -27
- package/url.cjs +1 -0
- package/url.d.ts +232 -0
- package/url.js +79 -0
package/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./object.cjs"),B=r=>r.length>0?r[0]:void 0,k=r=>r.slice(1),E=(r,n,t)=>{if(r.length!==n.length)return!1;for(let e=0;e<r.length;e++)if(!t(r[e],n[e]))return!1;return!0},x=r=>r.length===0,F=r=>r.length>0,b=(r,n)=>{const t=[];for(let e=0;e<r.length;e++){const o=n(r[e],e);o!=null&&t.push(o)}return t},j=r=>r.filter(n=>n!=null),q=(r,n,t,e=!0)=>{if(r.length<n.length)return-1*(e?1:-1);if(r.length>n.length)return 1*(e?1:-1);for(let o=0;o<r.length;o++){const a=t(r[o],n[o]);if(a!==0)return a}return 0},g=(r,n)=>Array.from({length:r},(t,e)=>n(e)),P=(r,n=0)=>Array.from({length:r},(t,e)=>n+e),D=(r,n)=>g(r,()=>n),M=(r,n)=>{const t={};return r.forEach(e=>{t[n(e)]=e}),w.objectKeys(t).map(e=>t[e])},d=(r,n)=>{const t=r.indexOf(n);return t<0?!1:(r.splice(t,1),!0)},_=(r,n)=>{let t=!1;for(;d(r,n);)t=!0;return t},v=(r,n)=>{const t=r.findIndex(n);return t<0?!1:(r.splice(t,1),!0)},T=(r,n)=>{let t=!1;for(;v(r,n);)t=!0;return t},H=(r,n,t)=>{const e={removals:[],swaps:[],inserts:[]},{removals:o,inserts:a,swaps:f}=e,i=new Map;n.forEach((s,l)=>i.set(t(s),l));const c=r.map((s,l)=>[s,l]).filter(([s])=>!i.has(t(s))).map(([s,l])=>l);for(let s=c.length-1;s>=0;s--){const l=c[s],u=o.length>0?o[o.length-1]:void 0;u!=null&&u.at===l+1?(u.at--,u.qt++):o.push({at:l,qt:1})}const h=new Map;r.forEach((s,l)=>h.set(t(s),l));const m=n.map((s,l)=>[s,l]).filter(([s])=>!h.has(t(s))).map(([s,l])=>l);for(const s of m){const l=a.length>0?a[a.length-1]:void 0;l!=null&&l.at+l.values.length===s?l.values.push(n[s]):a.push({at:s,values:[n[s]]})}const p=r.filter((s,l)=>!c.includes(l)),y=new Map;for(let s=0;s<p.length;s++)y.set(t(p[s]),s);const A=n.filter((s,l)=>!m.includes(l));for(let s=0;s<A.length;s++){const l=t(A[s]),u=y.get(l);if(u==null||s===u)continue;const O=t(p[s]);y.delete(O),f.push({from:s,to:u})}return e},N=(r,n)=>{const t=[...n];for(const{at:e,qt:o}of r.removals)t.splice(e,o);for(const{from:e,to:o}of r.swaps){const a=t[o];t[o]=t[e],t[e]=a}for(const e of r.inserts)t.splice(e.at,0,...e.values);return t},S=(r,n=" and ",t=", ")=>r.length===0?"":r.length===1?String(r[0]):`${r.slice(0,-1).join(t)}${n}${String(r[r.length-1])}`,C=(r,n,t=!0)=>{const e=r.map((i,c)=>[i,c]);e.sort((i,c)=>n(i[0],c[0]));const o=new Array(e.length);let a=0,f=e[0][0];for(let i=0;i<e.length;i++){const[c,h]=e[i];n(c,f)!==0&&(a=i,f=c),o[h]=a,t&&a++}return o},I=(r,n)=>{if(n<=0)throw new Error("Chunk size must be positive");const t=[];for(let e=0;e<r.length;e+=n)t.push(r.slice(e,e+n));return t},$=(r,n)=>{const t=[],e=[];for(const o of r)n(o)?t.push(o):e.push(o);return[t,e]},V=(r,n)=>{const t={};for(const e of r){const o=n(e);t[o]?t[o].push(e):t[o]=[e]}return t};exports.applyArrayDiffOperations=N;exports.areArraysEqual=E;exports.arrayDiffOperations=H;exports.arrayHasValues=F;exports.arrayHead=B;exports.arrayTail=k;exports.buildArray=g;exports.chunk=I;exports.compareArrays=q;exports.fillArray=D;exports.filterMapArray=b;exports.filterNullsFromArray=j;exports.groupBy=V;exports.isArrayEmpty=x;exports.joinArrayWithConjunction=S;exports.partition=$;exports.range=P;exports.rankArray=C;exports.removeAllFromArray=_;exports.removeAllFromArrayByPredicate=T;exports.removeOneFromArray=d;exports.removeOneFromArrayByPredicate=v;exports.uniqueByPrimitive=M;
|
package/array.d.ts
CHANGED
|
@@ -1,95 +1,4 @@
|
|
|
1
|
-
import { Compare, Maybe, Nothing
|
|
2
|
-
/**
|
|
3
|
-
* Transforms each element of an array using a mapping function, returning a new array.
|
|
4
|
-
*
|
|
5
|
-
* This is a functional programming utility that creates a new array by applying a transformation
|
|
6
|
-
* function to each element of the input array. The original array is not modified.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* // Transform numbers to strings
|
|
11
|
-
* const numbers = [1, 2, 3, 4, 5]
|
|
12
|
-
* const strings = mapArray(numbers, n => `Number: ${n}`)
|
|
13
|
-
* // Result: ['Number: 1', 'Number: 2', 'Number: 3', 'Number: 4', 'Number: 5']
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* // Transform objects with index
|
|
19
|
-
* const users = [{ name: 'Alice' }, { name: 'Bob' }]
|
|
20
|
-
* const indexed = mapArray(users, (user, index) => ({
|
|
21
|
-
* ...user,
|
|
22
|
-
* id: index + 1
|
|
23
|
-
* }))
|
|
24
|
-
* // Result: [{ name: 'Alice', id: 1 }, { name: 'Bob', id: 2 }]
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* // Use with signals in Tempo
|
|
30
|
-
* const items = prop([1, 2, 3])
|
|
31
|
-
* const doubled = items.map(arr => mapArray(arr, n => n * 2))
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @typeParam A - The type of elements in the input array
|
|
35
|
-
* @typeParam B - The type of elements in the output array
|
|
36
|
-
* @param arr - The input array to transform
|
|
37
|
-
* @param f - Function that transforms each element (receives element and index)
|
|
38
|
-
* @returns A new array with the transformed elements
|
|
39
|
-
* @public
|
|
40
|
-
*/
|
|
41
|
-
export declare const mapArray: <A, B>(arr: A[], f: (a: A, index: number) => B) => B[];
|
|
42
|
-
/**
|
|
43
|
-
* Transforms each element of an array into an array, then flattens all results into a single array.
|
|
44
|
-
*
|
|
45
|
-
* This function combines mapping and flattening operations. It applies a transformation function
|
|
46
|
-
* to each element that returns an array, then concatenates all the resulting arrays into one.
|
|
47
|
-
* This is useful for operations that need to expand elements into multiple items.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* // Split strings and flatten
|
|
52
|
-
* const sentences = ['hello world', 'foo bar', 'baz qux']
|
|
53
|
-
* const words = flatMapArray(sentences, sentence => sentence.split(' '))
|
|
54
|
-
* // Result: ['hello', 'world', 'foo', 'bar', 'baz', 'qux']
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Expand objects into multiple items
|
|
60
|
-
* const users = [
|
|
61
|
-
* { name: 'Alice', skills: ['JS', 'TS'] },
|
|
62
|
-
* { name: 'Bob', skills: ['Python', 'Go'] }
|
|
63
|
-
* ]
|
|
64
|
-
* const allSkills = flatMapArray(users, user =>
|
|
65
|
-
* user.skills.map(skill => ({ user: user.name, skill }))
|
|
66
|
-
* )
|
|
67
|
-
* // Result: [
|
|
68
|
-
* // { user: 'Alice', skill: 'JS' },
|
|
69
|
-
* // { user: 'Alice', skill: 'TS' },
|
|
70
|
-
* // { user: 'Bob', skill: 'Python' },
|
|
71
|
-
* // { user: 'Bob', skill: 'Go' }
|
|
72
|
-
* // ]
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* // Generate ranges
|
|
78
|
-
* const ranges = [2, 3, 1]
|
|
79
|
-
* const numbers = flatMapArray(ranges, n =>
|
|
80
|
-
* Array.from({ length: n }, (_, i) => i)
|
|
81
|
-
* )
|
|
82
|
-
* // Result: [0, 1, 0, 1, 2, 0]
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* @typeParam A - The type of elements in the input array
|
|
86
|
-
* @typeParam B - The type of elements in the resulting flattened array
|
|
87
|
-
* @param arr - The input array to transform and flatten
|
|
88
|
-
* @param f - Function that transforms each element into an array
|
|
89
|
-
* @returns A new flattened array containing all elements from the transformation results
|
|
90
|
-
* @public
|
|
91
|
-
*/
|
|
92
|
-
export declare const flatMapArray: <A, B>(arr: A[], f: (a: A) => B[]) => B[];
|
|
1
|
+
import { Compare, Maybe, Nothing } from './domain';
|
|
93
2
|
/**
|
|
94
3
|
* Returns the first element of an array, or `undefined` if the array is empty.
|
|
95
4
|
*
|
|
@@ -134,47 +43,6 @@ export declare const isArrayEmpty: <T>(arr: T[]) => arr is [];
|
|
|
134
43
|
* @public
|
|
135
44
|
*/
|
|
136
45
|
export declare const arrayHasValues: <T>(arr: T[]) => arr is [T, ...T[]];
|
|
137
|
-
/**
|
|
138
|
-
* Creates a new array containing only elements that satisfy the predicate function.
|
|
139
|
-
*
|
|
140
|
-
* This function provides a functional approach to filtering arrays, creating a new array
|
|
141
|
-
* without modifying the original. Elements are included in the result only if the predicate
|
|
142
|
-
* function returns `true` for that element.
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* // Filter even numbers
|
|
147
|
-
* const numbers = [1, 2, 3, 4, 5, 6]
|
|
148
|
-
* const evenNumbers = filterArray(numbers, n => n % 2 === 0)
|
|
149
|
-
* // Result: [2, 4, 6]
|
|
150
|
-
* ```
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```typescript
|
|
154
|
-
* // Filter objects by property
|
|
155
|
-
* const users = [
|
|
156
|
-
* { name: 'Alice', active: true },
|
|
157
|
-
* { name: 'Bob', active: false },
|
|
158
|
-
* { name: 'Charlie', active: true }
|
|
159
|
-
* ]
|
|
160
|
-
* const activeUsers = filterArray(users, user => user.active)
|
|
161
|
-
* // Result: [{ name: 'Alice', active: true }, { name: 'Charlie', active: true }]
|
|
162
|
-
* ```
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```typescript
|
|
166
|
-
* // Use with signals in Tempo
|
|
167
|
-
* const allItems = prop([1, 2, 3, 4, 5])
|
|
168
|
-
* const evenItems = allItems.map(arr => filterArray(arr, n => n % 2 === 0))
|
|
169
|
-
* ```
|
|
170
|
-
*
|
|
171
|
-
* @typeParam T - The type of elements in the array
|
|
172
|
-
* @param arr - The array to filter
|
|
173
|
-
* @param predicate - Function that tests each element (returns true to include the element)
|
|
174
|
-
* @returns A new array containing only elements that satisfy the predicate
|
|
175
|
-
* @public
|
|
176
|
-
*/
|
|
177
|
-
export declare const filterArray: <T>(arr: T[], predicate: (v: T) => boolean) => T[];
|
|
178
46
|
/**
|
|
179
47
|
* Applies a mapping function to each element of an array and returns a new array
|
|
180
48
|
* containing the mapped values, excluding any `null` or `undefined` values.
|
|
@@ -196,101 +64,6 @@ export declare const filterMapArray: <A, B>(arr: A[], f: (a: A, index: number) =
|
|
|
196
64
|
* @public
|
|
197
65
|
*/
|
|
198
66
|
export declare const filterNullsFromArray: <T>(arr: Array<T | Nothing>) => T[];
|
|
199
|
-
/**
|
|
200
|
-
* Flattens a two-dimensional array into a one-dimensional array.
|
|
201
|
-
*
|
|
202
|
-
* @param arr - The two-dimensional array to flatten.
|
|
203
|
-
* @returns The flattened one-dimensional array.
|
|
204
|
-
* @typeParam T - The type of elements in the array.
|
|
205
|
-
* @public
|
|
206
|
-
*/
|
|
207
|
-
export declare const flattenArray: <T>(arr: T[][]) => T[];
|
|
208
|
-
/**
|
|
209
|
-
* Reduces an array to a single value by applying an accumulator function from left to right.
|
|
210
|
-
*
|
|
211
|
-
* This function processes each element of the array in order, passing the current accumulator
|
|
212
|
-
* value and the current element to the reducer function. The result becomes the new accumulator
|
|
213
|
-
* value for the next iteration.
|
|
214
|
-
*
|
|
215
|
-
* @example
|
|
216
|
-
* ```typescript
|
|
217
|
-
* // Sum all numbers
|
|
218
|
-
* const numbers = [1, 2, 3, 4, 5]
|
|
219
|
-
* const sum = foldLeftArray(numbers, (acc, curr) => acc + curr, 0)
|
|
220
|
-
* // Result: 15
|
|
221
|
-
* ```
|
|
222
|
-
*
|
|
223
|
-
* @example
|
|
224
|
-
* ```typescript
|
|
225
|
-
* // Build an object from array
|
|
226
|
-
* const items = ['apple', 'banana', 'cherry']
|
|
227
|
-
* const indexed = foldLeftArray(
|
|
228
|
-
* items,
|
|
229
|
-
* (acc, item, index) => ({ ...acc, [index]: item }),
|
|
230
|
-
* {} as Record<number, string>
|
|
231
|
-
* )
|
|
232
|
-
* // Result: { 0: 'apple', 1: 'banana', 2: 'cherry' }
|
|
233
|
-
* ```
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* ```typescript
|
|
237
|
-
* // Count occurrences
|
|
238
|
-
* const words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
|
|
239
|
-
* const counts = foldLeftArray(
|
|
240
|
-
* words,
|
|
241
|
-
* (acc, word) => ({ ...acc, [word]: (acc[word] || 0) + 1 }),
|
|
242
|
-
* {} as Record<string, number>
|
|
243
|
-
* )
|
|
244
|
-
* // Result: { apple: 3, banana: 2, cherry: 1 }
|
|
245
|
-
* ```
|
|
246
|
-
*
|
|
247
|
-
* @typeParam T - The type of elements in the array
|
|
248
|
-
* @typeParam B - The type of the accumulator value
|
|
249
|
-
* @param arr - The array to reduce
|
|
250
|
-
* @param f - Function that combines the accumulator with each element
|
|
251
|
-
* @param b - The initial accumulator value
|
|
252
|
-
* @returns The final accumulated result
|
|
253
|
-
* @public
|
|
254
|
-
*/
|
|
255
|
-
export declare const foldLeftArray: <T, B>(arr: T[], f: (acc: B, curr: T) => B, b: B) => B;
|
|
256
|
-
/**
|
|
257
|
-
* Checks if all elements in an array satisfy a given predicate.
|
|
258
|
-
*
|
|
259
|
-
* @param arr - The array to check.
|
|
260
|
-
* @param predicate - The predicate function to apply to each element.
|
|
261
|
-
* @returns `true` if all elements satisfy the predicate, `false` otherwise.
|
|
262
|
-
* @typeParam T - The type of elements in the array.
|
|
263
|
-
* @public
|
|
264
|
-
*/
|
|
265
|
-
export declare const allElements: <T>(arr: T[], predicate: (v: T) => boolean) => boolean;
|
|
266
|
-
/**
|
|
267
|
-
* Checks if any element in the array satisfies the given predicate.
|
|
268
|
-
*
|
|
269
|
-
* @param arr - The array to check.
|
|
270
|
-
* @param predicate - The predicate function to apply to each element.
|
|
271
|
-
* @returns `true` if any element satisfies the predicate, `false` otherwise.
|
|
272
|
-
* @typeParam T - The type of elements in the array.
|
|
273
|
-
* @public
|
|
274
|
-
*/
|
|
275
|
-
export declare const anyElement: <T>(arr: T[], predicate: (v: T) => boolean) => boolean;
|
|
276
|
-
/**
|
|
277
|
-
* Applies a function to each element in an array.
|
|
278
|
-
*
|
|
279
|
-
* @typeParam T - The type of elements in the array.
|
|
280
|
-
* @param arr - The array to iterate over.
|
|
281
|
-
* @param f - The function to apply to each element.
|
|
282
|
-
* @public
|
|
283
|
-
*/
|
|
284
|
-
export declare const forEachElement: <T>(arr: T[], f: (v: T) => void) => void;
|
|
285
|
-
/**
|
|
286
|
-
* Concatenates multiple arrays into a single array.
|
|
287
|
-
*
|
|
288
|
-
* @param arrs - The arrays to concatenate.
|
|
289
|
-
* @returns The concatenated array.
|
|
290
|
-
* @typeParam A - The type of elements in the arrays.
|
|
291
|
-
* @public
|
|
292
|
-
*/
|
|
293
|
-
export declare const concatArrays: <A>(...arrs: A[][]) => A[];
|
|
294
67
|
/**
|
|
295
68
|
* Compares two arrays based on their lengths and element values.
|
|
296
69
|
*
|
|
@@ -303,81 +76,87 @@ export declare const concatArrays: <A>(...arrs: A[][]) => A[];
|
|
|
303
76
|
* @public
|
|
304
77
|
*/
|
|
305
78
|
export declare const compareArrays: <A>(a: A[], b: A[], comparef: Compare<A>, shorterFirst?: boolean) => number;
|
|
306
|
-
/**
|
|
307
|
-
* Sorts an array in place using the provided compare function.
|
|
308
|
-
*
|
|
309
|
-
* @typeParam A - The type of elements in the array.
|
|
310
|
-
* @param arr - The array to be sorted.
|
|
311
|
-
* @param compare - The compare function used to determine the order of the elements.
|
|
312
|
-
* @returns The sorted array.
|
|
313
|
-
* @public
|
|
314
|
-
*/
|
|
315
|
-
export declare const sortArray: <A>(arr: A[], compare: Compare<A>) => A[];
|
|
316
79
|
/**
|
|
317
80
|
* Generates an array of values by applying a function to each index.
|
|
318
81
|
*
|
|
82
|
+
* This function provides a more semantic alternative to `Array.from({ length }, (_, i) => f(i))`
|
|
83
|
+
* with better readability and intent expression. It's particularly useful for creating
|
|
84
|
+
* arrays with computed values based on their position.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* // Create an array of squares
|
|
89
|
+
* const squares = buildArray(5, i => i * i)
|
|
90
|
+
* // Result: [0, 1, 4, 9, 16]
|
|
91
|
+
*
|
|
92
|
+
* // Create an array of objects with IDs
|
|
93
|
+
* const items = buildArray(3, i => ({ id: i, name: `Item ${i}` }))
|
|
94
|
+
* // Result: [{ id: 0, name: 'Item 0' }, { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
319
97
|
* @param length - The length of the resulting array.
|
|
320
98
|
* @param f - The function to apply to each index. It takes the index as a parameter and returns the corresponding value.
|
|
321
99
|
* @returns An array of values generated by applying the function to each index.
|
|
322
100
|
* @public
|
|
323
101
|
*/
|
|
324
|
-
export declare const
|
|
102
|
+
export declare const buildArray: <A>(length: number, f: (index: number) => A) => A[];
|
|
325
103
|
/**
|
|
326
|
-
* Generates an array of numbers in a specified range.
|
|
104
|
+
* Generates an array of consecutive numbers in a specified range.
|
|
105
|
+
*
|
|
106
|
+
* This function provides a more intuitive API than `Array.from({ length }, (_, i) => startAt + i)`
|
|
107
|
+
* for creating numeric sequences. It's commonly needed for iterations, indexing, and mathematical operations.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // Create a range from 0 to 4
|
|
112
|
+
* const zeroToFour = range(5)
|
|
113
|
+
* // Result: [0, 1, 2, 3, 4]
|
|
114
|
+
*
|
|
115
|
+
* // Create a range from 10 to 14
|
|
116
|
+
* const tenToFourteen = range(5, 10)
|
|
117
|
+
* // Result: [10, 11, 12, 13, 14]
|
|
118
|
+
*
|
|
119
|
+
* // Use for creating test data
|
|
120
|
+
* const testIds = range(100, 1000)
|
|
121
|
+
* // Result: [1000, 1001, 1002, ..., 1099]
|
|
122
|
+
* ```
|
|
327
123
|
*
|
|
328
124
|
* @param length - The length of the array to generate.
|
|
329
125
|
* @param startAt - The starting value of the range. Default is 0.
|
|
330
126
|
* @returns An array of numbers in the specified range.
|
|
331
127
|
* @public
|
|
332
128
|
*/
|
|
333
|
-
export declare const
|
|
129
|
+
export declare const range: (length: number, startAt?: number) => number[];
|
|
334
130
|
/**
|
|
335
131
|
* Creates a new array with the specified length and fills it with the provided value.
|
|
336
132
|
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
* @public
|
|
342
|
-
*/
|
|
343
|
-
export declare const createFilledArray: <A>(length: number, value: A) => A[];
|
|
344
|
-
/**
|
|
345
|
-
* Removes duplicate primitive values from an array, returning only unique values.
|
|
346
|
-
*
|
|
347
|
-
* This function uses JavaScript's Set to efficiently remove duplicates from arrays
|
|
348
|
-
* containing primitive values (strings, numbers, booleans, symbols). The order of
|
|
349
|
-
* first occurrence is preserved.
|
|
133
|
+
* This function provides a more semantic alternative to `Array(length).fill(value)` or
|
|
134
|
+
* `new Array(length).fill(value)` with better type inference and clearer intent.
|
|
135
|
+
* It's particularly useful when you need arrays of non-primitive values or when
|
|
136
|
+
* working with functional programming patterns.
|
|
350
137
|
*
|
|
351
138
|
* @example
|
|
352
139
|
* ```typescript
|
|
353
|
-
* //
|
|
354
|
-
* const
|
|
355
|
-
*
|
|
356
|
-
* // Result: [1, 2, 3, 4]
|
|
357
|
-
* ```
|
|
140
|
+
* // Create an array of default objects
|
|
141
|
+
* const defaultUsers = fillArray(3, { name: '', active: false })
|
|
142
|
+
* // Result: [{ name: '', active: false }, { name: '', active: false }, { name: '', active: false }]
|
|
358
143
|
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* //
|
|
362
|
-
* const tags = ['react', 'vue', 'react', 'angular', 'vue']
|
|
363
|
-
* const uniqueTags = uniquePrimitives(tags)
|
|
364
|
-
* // Result: ['react', 'vue', 'angular']
|
|
365
|
-
* ```
|
|
144
|
+
* // Create an array of empty arrays
|
|
145
|
+
* const matrix = fillArray(3, [] as number[])
|
|
146
|
+
* // Result: [[], [], []]
|
|
366
147
|
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* //
|
|
370
|
-
* const mixed = [1, '1', true, 1, 'hello', true, '1']
|
|
371
|
-
* const uniqueMixed = uniquePrimitives(mixed)
|
|
372
|
-
* // Result: [1, '1', true, 'hello']
|
|
148
|
+
* // Create an array of functions
|
|
149
|
+
* const handlers = fillArray(5, () => console.log('handler'))
|
|
150
|
+
* // Result: [function, function, function, function, function]
|
|
373
151
|
* ```
|
|
374
152
|
*
|
|
375
|
-
* @typeParam
|
|
376
|
-
* @param
|
|
377
|
-
* @
|
|
153
|
+
* @typeParam A - The type of the elements in the array.
|
|
154
|
+
* @param length - The length of the new array.
|
|
155
|
+
* @param value - The value to fill the array with.
|
|
156
|
+
* @returns A new array filled with the specified value.
|
|
378
157
|
* @public
|
|
379
158
|
*/
|
|
380
|
-
export declare const
|
|
159
|
+
export declare const fillArray: <A>(length: number, value: A) => A[];
|
|
381
160
|
/**
|
|
382
161
|
* Removes duplicate objects from an array based on a key extraction function.
|
|
383
162
|
*
|
|
@@ -483,14 +262,6 @@ export declare const removeOneFromArrayByPredicate: <A>(arr: A[], predicate: (a:
|
|
|
483
262
|
* @public
|
|
484
263
|
*/
|
|
485
264
|
export declare const removeAllFromArrayByPredicate: <A>(arr: A[], predicate: (a: A) => boolean) => boolean;
|
|
486
|
-
/**
|
|
487
|
-
* Converts an IterableIterator to an array.
|
|
488
|
-
*
|
|
489
|
-
* @param it - The IterableIterator to convert.
|
|
490
|
-
* @returns An array containing the values from the IterableIterator.
|
|
491
|
-
* @public
|
|
492
|
-
*/
|
|
493
|
-
export declare const arrayOfIterableIterator: <A>(it: IterableIterator<A>) => A[];
|
|
494
265
|
/**
|
|
495
266
|
* Represents the different operations that can be performed on an array.
|
|
496
267
|
*
|
|
@@ -555,3 +326,72 @@ export declare const joinArrayWithConjunction: <A>(arr: A[], conjunction?: strin
|
|
|
555
326
|
* @public
|
|
556
327
|
*/
|
|
557
328
|
export declare const rankArray: <T>(array: T[], compare: (a: T, b: T) => number, incrementDuplicates?: boolean) => number[];
|
|
329
|
+
/**
|
|
330
|
+
* Splits an array into chunks of the specified size.
|
|
331
|
+
*
|
|
332
|
+
* This function divides an array into smaller arrays of a fixed size.
|
|
333
|
+
* The last chunk may contain fewer elements if the array length is not
|
|
334
|
+
* evenly divisible by the chunk size.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```typescript
|
|
338
|
+
* const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
339
|
+
* const chunks = chunk(numbers, 3)
|
|
340
|
+
* // Result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
|
341
|
+
*
|
|
342
|
+
* const uneven = chunk([1, 2, 3, 4, 5], 2)
|
|
343
|
+
* // Result: [[1, 2], [3, 4], [5]]
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @param array - The array to chunk
|
|
347
|
+
* @param size - The size of each chunk (must be positive)
|
|
348
|
+
* @returns An array of chunks
|
|
349
|
+
* @public
|
|
350
|
+
*/
|
|
351
|
+
export declare const chunk: <T>(array: readonly T[], size: number) => T[][];
|
|
352
|
+
/**
|
|
353
|
+
* Partitions an array into two arrays based on a predicate.
|
|
354
|
+
*
|
|
355
|
+
* This function splits an array into two parts: elements that satisfy
|
|
356
|
+
* the predicate and elements that don't. The order of elements is preserved.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* const numbers = [1, 2, 3, 4, 5, 6]
|
|
361
|
+
* const [evens, odds] = partition(numbers, n => n % 2 === 0)
|
|
362
|
+
* // evens: [2, 4, 6]
|
|
363
|
+
* // odds: [1, 3, 5]
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @param array - The array to partition
|
|
367
|
+
* @param predicate - Function that tests each element
|
|
368
|
+
* @returns A tuple containing [matching elements, non-matching elements]
|
|
369
|
+
* @public
|
|
370
|
+
*/
|
|
371
|
+
export declare const partition: <T>(array: readonly T[], predicate: (item: T) => boolean) => [T[], T[]];
|
|
372
|
+
/**
|
|
373
|
+
* Groups array elements by a key function.
|
|
374
|
+
*
|
|
375
|
+
* This function creates an object where keys are the result of the key function
|
|
376
|
+
* and values are arrays of elements that produced that key.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```typescript
|
|
380
|
+
* const users = [
|
|
381
|
+
* { name: 'Alice', department: 'Engineering' },
|
|
382
|
+
* { name: 'Bob', department: 'Engineering' },
|
|
383
|
+
* { name: 'Carol', department: 'Marketing' }
|
|
384
|
+
* ]
|
|
385
|
+
* const byDepartment = groupBy(users, user => user.department)
|
|
386
|
+
* // Result: {
|
|
387
|
+
* // Engineering: [{ name: 'Alice', ... }, { name: 'Bob', ... }],
|
|
388
|
+
* // Marketing: [{ name: 'Carol', ... }]
|
|
389
|
+
* // }
|
|
390
|
+
* ```
|
|
391
|
+
*
|
|
392
|
+
* @param array - The array to group
|
|
393
|
+
* @param keyFn - Function that extracts the grouping key from each element
|
|
394
|
+
* @returns An object with grouped elements
|
|
395
|
+
* @public
|
|
396
|
+
*/
|
|
397
|
+
export declare const groupBy: <T, K extends string | number | symbol>(array: readonly T[], keyFn: (item: T) => K) => Record<K, T[]>;
|