@stacksjs/arrays 0.65.0 → 0.68.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/src/arr.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './contains'
2
- export * from './helpers'
3
- export * from './math'
package/src/contains.ts DELETED
@@ -1,87 +0,0 @@
1
- /**
2
- * Returns true if the needle is contained in the haystack.
3
- *
4
- * @param needle
5
- * @param haystack
6
- * @example
7
- * ```ts
8
- * contains('foo', ['foo', 'bar']) // true
9
- * contains('foo', ['bar']) // false
10
- * ```
11
- */
12
- export function contains(needle: string, haystack: string[]): boolean {
13
- return haystack.some(hay => needle.includes(hay))
14
- }
15
-
16
- /**
17
- * Returns true if all needles are contained in the haystack.
18
- * @param needles
19
- * @param haystack
20
- * @example
21
- * ```ts
22
- * containsAll(['foo', 'bar'], ['foo', 'bar', 'baz']) // true
23
- * containsAll(['foo', 'bar'], ['foo', 'baz']) // false
24
- * ```
25
- */
26
- export function containsAll(needles: string[], haystack: string[]): boolean {
27
- return needles.every(needle => contains(needle, haystack))
28
- }
29
-
30
- /**
31
- * Returns true if any needle is contained in the haystack.
32
- * @param needles
33
- * @param haystack
34
- * @example
35
- * ```ts
36
- * containsAny(['foo', 'bar'], ['foo', 'bar', 'baz']) // true
37
- * containsAny(['foo', 'bar'], ['foo', 'baz']) // true
38
- * containsAny(['foo', 'bar'], ['baz']) // false
39
- * ```
40
- */
41
- export function containsAny(needles: string[], haystack: string[]): boolean {
42
- return needles.some(needle => contains(needle, haystack))
43
- }
44
-
45
- /**
46
- * Returns true if none of the needles are contained in the haystack.
47
- * @param needles
48
- * @param haystack
49
- * @example
50
- * ```ts
51
- * containsNone(['foo', 'bar'], ['foo', 'bar', 'baz']) // false
52
- * containsNone(['foo', 'bar'], ['foo', 'baz']) // false
53
- * containsNone(['foo', 'bar'], ['baz']) // true
54
- * ```
55
- */
56
- export function containsNone(needles: string[], haystack: string[]): boolean {
57
- return !containsAny(needles, haystack)
58
- }
59
-
60
- /**
61
- * Returns true if all needles are contained in the haystack.
62
- * @param needles
63
- * @param haystack
64
- * @example
65
- * ```ts
66
- * containsOnly(['foo', 'bar'], ['foo', 'bar', 'baz']) // false
67
- * containsOnly(['foo', 'bar'], ['foo', 'baz']) // false
68
- * containsOnly(['foo', 'bar'], ['foo', 'bar']) // true
69
- * ```
70
- */
71
- export function containsOnly(needles: string[], haystack: string[]): boolean {
72
- return containsAll(haystack, needles)
73
- }
74
-
75
- /**
76
- * Returns true if the needle is not contained in the haystack.
77
- * @param needle
78
- * @param haystack
79
- * @example
80
- * ```ts
81
- * doesNotContain('foo', ['foo', 'bar']) // false
82
- * doesNotContain('foo', ['bar']) // true
83
- * ```
84
- */
85
- export function doesNotContain(needle: string, haystack: string[]): boolean {
86
- return !contains(needle, haystack)
87
- }
package/src/helpers.ts DELETED
@@ -1,300 +0,0 @@
1
- import type { Arrayable, Nullable } from '@stacksjs/types'
2
- import { clamp } from '@stacksjs/utils'
3
-
4
- /**
5
- * Convert `Arrayable<T>` to `Array<T>`
6
- *
7
- * @category Array
8
- * @example
9
- * ```ts
10
- * toArray('foo') // ['foo']
11
- * toArray(['foo']) // ['foo']
12
- * toArray(null) // []
13
- * toArray(undefined) // []
14
- * toArray(1) // [1]
15
- * toArray([1]) // [1]
16
- * toArray({ foo: 'bar' }) // [{ foo: 'bar' }]
17
- * toArray([{ foo: 'bar' }]) // [{ foo: 'bar' }]
18
- * ```
19
- */
20
- export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
21
- array = array ?? []
22
- return Array.isArray(array) ? array : [array]
23
- }
24
-
25
- /**
26
- * Flatten `Arrayable<T>` to `Array<T>`
27
- *
28
- * @category Array
29
- * @example
30
- * ```ts
31
- * flatten([1, [2, [3, [4, [5]]]]]) // [1, 2, 3, 4, 5]
32
- * ```
33
- */
34
- export function flatten<T>(array?: Nullable<Arrayable<T | T[]>>): T[] {
35
- return toArray(array).reduce((acc: T[], val) => acc.concat(Array.isArray(val) ? flatten(val) : (val as T)), [])
36
- }
37
-
38
- /**
39
- * Use rest arguments to merge arrays
40
- *
41
- * @category Array
42
- * @example
43
- * ```ts
44
- * mergeArrayable([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]
45
- * ```
46
- */
47
- export function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
48
- return args.flatMap(i => toArray(i))
49
- }
50
-
51
- export type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any
52
-
53
- /**
54
- * Divide an array into two parts by a filter function
55
- *
56
- * @category Array
57
- * @example
58
- * ```ts
59
- * const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
60
- * console.log(odd) // [1, 3]
61
- * console.log(even) // [2, 4]
62
- * ```
63
- */
64
- export function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]]
65
- export function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]]
66
- export function partition<T>(
67
- array: readonly T[],
68
- f1: PartitionFilter<T>,
69
- f2: PartitionFilter<T>,
70
- f3: PartitionFilter<T>,
71
- ): [T[], T[], T[], T[]]
72
- export function partition<T>(
73
- array: readonly T[],
74
- f1: PartitionFilter<T>,
75
- f2: PartitionFilter<T>,
76
- f3: PartitionFilter<T>,
77
- f4: PartitionFilter<T>,
78
- ): [T[], T[], T[], T[], T[]]
79
- export function partition<T>(
80
- array: readonly T[],
81
- f1: PartitionFilter<T>,
82
- f2: PartitionFilter<T>,
83
- f3: PartitionFilter<T>,
84
- f4: PartitionFilter<T>,
85
- f5: PartitionFilter<T>,
86
- ): [T[], T[], T[], T[], T[], T[]]
87
- export function partition<T>(
88
- array: readonly T[],
89
- f1: PartitionFilter<T>,
90
- f2: PartitionFilter<T>,
91
- f3: PartitionFilter<T>,
92
- f4: PartitionFilter<T>,
93
- f5: PartitionFilter<T>,
94
- f6: PartitionFilter<T>,
95
- ): [T[], T[], T[], T[], T[], T[], T[]]
96
- export function partition<T>(array: readonly T[], ...filters: PartitionFilter<T>[]): any {
97
- const result: T[][] = Array.from({ length: filters.length + 1 })
98
- .fill(null)
99
- .map(() => [])
100
-
101
- array.forEach((e, idx, arr) => {
102
- let i = 0
103
- for (const filter of filters) {
104
- if (filter(e, idx, arr)) {
105
- ;(result[i] as T[]).push(e)
106
- return
107
- }
108
- i += 1
109
- }
110
- ;(result[i] as T[]).push(e)
111
- })
112
- return result
113
- }
114
-
115
- /**
116
- * Unique an Array
117
- *
118
- * @category Array
119
- * @example
120
- * ```ts
121
- * uniq([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
122
- * ```
123
- */
124
- export function uniq<T>(array: readonly T[]): T[] {
125
- return Array.from(new Set(array))
126
- }
127
-
128
- /**
129
- * Unique an Array
130
- *
131
- * @param array
132
- * @example
133
- * ```ts
134
- * unique([1, 2, 3, 3, 2, 1]) // [1, 2, 3]
135
- * ```
136
- */
137
- export function unique<T>(array: readonly T[]): T[] {
138
- return uniq(array)
139
- }
140
-
141
- /**
142
- * Unique an Array by a custom equality function
143
- *
144
- * @category Array
145
- * @example
146
- * ```ts
147
- * uniqueBy([1, 2, 3, 3, 2, 1], (a, b) => a === b) // [1, 2, 3]
148
- * ```
149
- */
150
- export function uniqueBy<T>(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[] {
151
- return array.reduce((acc: T[], cur: any) => {
152
- const index = acc.findIndex((item: any) => equalFn(cur, item))
153
- if (index === -1)
154
- acc.push(cur)
155
- return acc
156
- }, [])
157
- }
158
-
159
- /**
160
- * Get last item
161
- *
162
- * @category Array
163
- * @example
164
- * ```ts
165
- * last([1, 2, 3]) // 3
166
- * ```
167
- */
168
- export function last(array: readonly []): undefined
169
- export function last<T>(array: readonly T[]): T
170
- export function last<T>(array: readonly T[]): T | undefined {
171
- return at(array, -1)
172
- }
173
-
174
- /**
175
- * Remove an item from Array
176
- *
177
- * @category Array
178
- * @example
179
- * ```ts
180
- * const arr = [1, 2, 3]
181
- * remove(arr, 2) // true
182
- * Arr.remove(arr, 4) // false
183
- * console.log(arr) // [1, 3]
184
- */
185
- export function remove<T>(array: T[], value: T): boolean {
186
- if (!array)
187
- return false
188
-
189
- const index = array.indexOf(value)
190
- if (index >= 0) {
191
- array.splice(index, 1)
192
- return true
193
- }
194
-
195
- return false
196
- }
197
-
198
- /**
199
- * Get nth item of Array. Negative for backward
200
- *
201
- * @category Array
202
- * @example
203
- * ```ts
204
- * at([1, 2, 3], 1) // 2
205
- * at([1, 2, 3], -1) // 3
206
- * at([1, 2, 3], 3) // undefined
207
- * at([1, 2, 3], -4) // undefined
208
- * ```
209
- */
210
- export function at(array: readonly [], index: number): undefined
211
- export function at<T>(array: readonly T[], index: number): T
212
- export function at<T>(array: readonly T[] | [], index: number): T | undefined {
213
- const len = array.length
214
- if (!len)
215
- return undefined
216
-
217
- if (index < 0)
218
- index += len
219
-
220
- return array[index]
221
- }
222
-
223
- /**
224
- * Move an item from one index to another
225
- * @param array
226
- * @param from
227
- * @param to
228
- *
229
- * @category Array
230
- * @example
231
- * ```ts
232
- * move([1, 2, 3, 4], 0, 2) // [2, 3, 1, 4]
233
- * move([1, 2, 3, 4], 0, -1) // [2, 3, 4, 1]
234
- * move([1, 2, 3, 4], -1, 0) // [4, 1, 2, 3]
235
- * move([1, 2, 3, 4], -1, -2) // [1, 4, 2, 3]
236
- * move([1, 2, 3, 4], 1, 1) // [1, 2, 3, 4]
237
- * ```
238
- */
239
- export function move<T>(array: T[], from: number, to: number): T[] {
240
- const len = array.length
241
- if (!len)
242
- return []
243
-
244
- if (from < 0)
245
- from += len
246
-
247
- if (to < 0)
248
- to += len
249
-
250
- const item = array.splice(from, 1)[0]
251
- array.splice(to, 0, item as T)
252
- return array
253
- }
254
-
255
- /**
256
- * Clamp a number to the index range of an array.
257
- *
258
- * @category Array
259
- * @example
260
- * ```ts
261
- * clampArrayRange([1, 2, 3], 0) // 0
262
- * clampArrayRange([1, 2, 3], 1) // 1
263
- * clampArrayRange([1, 2, 3], 2) // 2
264
- * clampArrayRange([1, 2, 3], 3) // 2
265
- * clampArrayRange([1, 2, 3], 4) // 2
266
- * clampArrayRange([1, 2, 3], -1) // 0
267
- */
268
- export function clampArrayRange(arr: readonly unknown[], n: number): number {
269
- return clamp(n, 0, arr.length - 1)
270
- }
271
-
272
- /**
273
- * Get random items from an array
274
- *
275
- * @category Array
276
- * @example
277
- * ```ts
278
- * sample([1, 2, 3, 4], 2) // [2, 3]
279
- * ```
280
- */
281
- export function sample<T>(arr: T[], count: number): T[] {
282
- return Array.from({ length: count }, () => arr[Math.floor(Math.random() * arr.length)]!)
283
- }
284
-
285
- /**
286
- * Shuffle an array. This function mutates the array.
287
- *
288
- * @category Array
289
- * @example
290
- * ```ts
291
- * shuffle([1, 2, 3, 4]) // [2, 4, 1, 3]
292
- * ```
293
- */
294
- export function shuffle<T>(array: T[]): T[] {
295
- for (let i = array.length - 1; i > 0; i--) {
296
- const j = Math.floor(Math.random() * (i + 1))
297
- ;[array[i] as T, array[j] as T] = [array[j] as T, array[i] as T]
298
- }
299
- return array
300
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './arr'
2
- export * as arr from './arr'
3
- export * from './macro'
package/src/macro.ts DELETED
@@ -1,145 +0,0 @@
1
- import type { Arrayable, Nullable } from '@stacksjs/types'
2
- import type { PartitionFilter } from './helpers'
3
- import { contains, containsAll, containsAny, containsNone, containsOnly, doesNotContain } from './contains'
4
- import {
5
- at,
6
- clampArrayRange,
7
- flatten,
8
- last,
9
- mergeArrayable,
10
- move,
11
- partition,
12
- remove,
13
- sample,
14
- shuffle,
15
- toArray,
16
- uniq,
17
- uniqueBy,
18
- } from './helpers'
19
- import { average, median, mode, range, sum } from './math'
20
-
21
- export const Arr = {
22
- contains(needle: string, haystack: string[]): boolean {
23
- return contains(needle, haystack)
24
- },
25
-
26
- containsAll(needles: string[], haystack: string[]): boolean {
27
- return containsAll(needles, haystack)
28
- },
29
-
30
- containsAny(needles: string[], haystack: string[]): boolean {
31
- return containsAny(needles, haystack)
32
- },
33
-
34
- containsNone(needles: string[], haystack: string[]): boolean {
35
- return containsNone(needles, haystack)
36
- },
37
-
38
- containsOnly(needles: string[], haystack: string[]): boolean {
39
- return containsOnly(needles, haystack)
40
- },
41
-
42
- doesNotContain(needle: string, haystack: string[]): boolean {
43
- return doesNotContain(needle, haystack)
44
- },
45
-
46
- toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
47
- return toArray(array)
48
- },
49
-
50
- flatten<T>(array?: Nullable<Arrayable<T | T[]>>): T[] {
51
- return flatten(array)
52
- },
53
-
54
- mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
55
- return mergeArrayable(...args)
56
- },
57
-
58
- partition<T>(array: readonly T[], filter: PartitionFilter<T>): [T[], T[]] {
59
- return partition(array, filter)
60
- },
61
-
62
- /**
63
- * Returns a random item/s from the array
64
- */
65
- random<T>(arr: T[], count = 1): T[] {
66
- return sample(arr, count).filter((item): item is T => item != null)
67
- },
68
-
69
- /**
70
- * Returns random item/s from the array
71
- */
72
- sample<T>(arr: T[], count = 1): T[] {
73
- return sample(arr, count)
74
- },
75
-
76
- unique<T>(arr: T[]): T[] {
77
- return uniq(arr)
78
- },
79
-
80
- uniqueBy<T>(arr: readonly T[], equalFn: (a: any, b: any) => boolean): T[] {
81
- return uniqueBy(arr, equalFn)
82
- },
83
-
84
- last<T>(arr: T[]): T | undefined {
85
- return last(arr)
86
- },
87
-
88
- remove<T>(arr: T[], value: T): boolean {
89
- return remove(arr, value)
90
- },
91
-
92
- at(arr: any[], index: number): any {
93
- return at(arr, index)
94
- },
95
-
96
- range(arr: readonly number[]): number {
97
- return range(arr)
98
- },
99
-
100
- move<T>(arr: T[], from: number, to: number): T[] {
101
- return move(arr, from, to)
102
- },
103
-
104
- clampArrayRange(arr: readonly unknown[], n: number): number {
105
- return clampArrayRange(arr, n)
106
- },
107
-
108
- shuffle<T>(arr: T[]): T[] {
109
- return shuffle(arr)
110
- },
111
-
112
- /**
113
- * Returns the sum of all items in the array
114
- */
115
- sum(arr: number[]): number {
116
- return sum(arr)
117
- },
118
-
119
- /**
120
- * Returns the average of all items in the array
121
- */
122
- average(arr: number[]): number {
123
- return average(arr)
124
- },
125
-
126
- avg(arr: number[]): number {
127
- return average(arr)
128
- },
129
-
130
- /**
131
- * Returns the median of all items in the array
132
- */
133
- median(arr: number[]): number {
134
- return median(arr)
135
- },
136
-
137
- /**
138
- * Returns the mode of all items in the array
139
- */
140
- mode(arr: number[]): number {
141
- return mode(arr)
142
- },
143
- }
144
-
145
- export const arr: typeof Arr = Arr