@gesslar/toolkit 0.2.5 → 0.2.7
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/package.json +1 -1
- package/src/lib/Collection.js +44 -1
- package/src/types/Collection.d.ts +85 -9
package/package.json
CHANGED
package/src/lib/Collection.js
CHANGED
|
@@ -498,7 +498,44 @@ export default class Collection {
|
|
|
498
498
|
return result
|
|
499
499
|
}
|
|
500
500
|
|
|
501
|
-
static
|
|
501
|
+
static trimArray(arr, except=[]) {
|
|
502
|
+
Valid.type(arr, "Array")
|
|
503
|
+
Valid.type(except, "Array")
|
|
504
|
+
|
|
505
|
+
Collection.trimArrayLeft(arr, except)
|
|
506
|
+
Collection.trimArrayRight(arr, except)
|
|
507
|
+
|
|
508
|
+
return arr
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
static trimArrayRight(arr, except=[]) {
|
|
512
|
+
Valid.type(arr, "Array")
|
|
513
|
+
Valid.type(except, "Array")
|
|
514
|
+
|
|
515
|
+
arr.reverse()
|
|
516
|
+
Collection.trimArrayLeft(arr, except)
|
|
517
|
+
arr.reverse()
|
|
518
|
+
|
|
519
|
+
return arr
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
static trimArrayLeft(arr, except=[]) {
|
|
523
|
+
Valid.type(arr, "Array")
|
|
524
|
+
Valid.type(except, "Array")
|
|
525
|
+
|
|
526
|
+
while(arr.length > 0) {
|
|
527
|
+
const value = arr[0]
|
|
528
|
+
|
|
529
|
+
if(value || except.includes(value))
|
|
530
|
+
break
|
|
531
|
+
|
|
532
|
+
arr.shift()
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return arr
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
static transposeObjects(objects) {
|
|
502
539
|
const req = "Array"
|
|
503
540
|
const type = Data.typeOf(objects)
|
|
504
541
|
|
|
@@ -522,4 +559,10 @@ export default class Collection {
|
|
|
522
559
|
return acc
|
|
523
560
|
}, {})
|
|
524
561
|
}
|
|
562
|
+
|
|
563
|
+
static flattenObjectArray(input) {
|
|
564
|
+
const flattened = Array.isArray(input) ? input.flat() : input
|
|
565
|
+
|
|
566
|
+
return Collection.transposeObjects(flattened)
|
|
567
|
+
}
|
|
525
568
|
}
|
|
@@ -214,27 +214,103 @@ export default class Collection {
|
|
|
214
214
|
static allocateObject(source: Array<unknown>, spec: Array<unknown> | ((source: Array<unknown>) => Promise<Array<unknown>> | Array<unknown>)): Promise<Record<string, unknown>>
|
|
215
215
|
|
|
216
216
|
/**
|
|
217
|
-
* Flattens an array of plain objects
|
|
218
|
-
*
|
|
217
|
+
* Flattens one level of an array of plain objects, transposing values so each
|
|
218
|
+
* key maps to the collected values from every object.
|
|
219
219
|
*
|
|
220
|
-
*
|
|
220
|
+
* Accepts either a simple array of objects or an array that mixes objects and
|
|
221
|
+
* nested object arrays (one level deep). Nested arrays are flattened before
|
|
222
|
+
* transposition.
|
|
223
|
+
*
|
|
224
|
+
* @param input - Array of plain objects (optionally containing nested arrays)
|
|
221
225
|
* @returns Object with keys mapped to arrays of values from all input objects
|
|
222
226
|
*
|
|
223
|
-
* @throws {Sass} If
|
|
227
|
+
* @throws {Sass} If input is not an Array or if any element is not a plain object after flattening
|
|
224
228
|
*
|
|
225
229
|
* @example
|
|
226
230
|
* ```typescript
|
|
227
231
|
* import { Collection } from '@gesslar/toolkit'
|
|
228
232
|
*
|
|
229
233
|
* const objects = [
|
|
230
|
-
* { name: 'Alice', age: 25 },
|
|
231
|
-
* { name: 'Bob', age: 30 }
|
|
232
|
-
* { name: 'Charlie', age: 35 }
|
|
234
|
+
* [{ name: 'Alice', age: 25 }],
|
|
235
|
+
* { name: 'Bob', age: 30 }
|
|
233
236
|
* ]
|
|
234
237
|
*
|
|
235
238
|
* const result = Collection.flattenObjectArray(objects)
|
|
236
|
-
* // result: { name: ['Alice', 'Bob'
|
|
239
|
+
* // result: { name: ['Alice', 'Bob'], age: [25, 30] }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
static flattenObjectArray(
|
|
243
|
+
input: Array<Record<string, unknown> | Array<Record<string, unknown>>>
|
|
244
|
+
): Record<string, Array<unknown>>
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Transposes an array of plain objects into an object of arrays, keyed by the
|
|
248
|
+
* original object keys.
|
|
249
|
+
*
|
|
250
|
+
* @param objects - Array of plain objects to transpose
|
|
251
|
+
* @returns Object with keys mapped to arrays of values from the input objects
|
|
252
|
+
*
|
|
253
|
+
* @throws {Sass} If objects is not an Array or if any element is not a plain object
|
|
254
|
+
*/
|
|
255
|
+
static transposeObjects(objects: Array<Record<string, unknown>>): Record<string, Array<unknown>>
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Trims falsy values from both ends of an array.
|
|
259
|
+
*
|
|
260
|
+
* @param arr - The array to trim
|
|
261
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
262
|
+
* @returns The trimmed array (modified in place)
|
|
263
|
+
*
|
|
264
|
+
* @throws {Sass} If arr or except is not an Array
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
269
|
+
*
|
|
270
|
+
* const arr = [null, 0, 1, 2, "", undefined]
|
|
271
|
+
* Collection.trimArray(arr)
|
|
272
|
+
* console.log(arr) // [1, 2]
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
static trimArray<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Trims falsy values from the right end of an array.
|
|
279
|
+
*
|
|
280
|
+
* @param arr - The array to trim
|
|
281
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
282
|
+
* @returns The trimmed array (modified in place)
|
|
283
|
+
*
|
|
284
|
+
* @throws {Sass} If arr or except is not an Array
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```typescript
|
|
288
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
289
|
+
*
|
|
290
|
+
* const arr = [1, "", undefined]
|
|
291
|
+
* Collection.trimArrayRight(arr)
|
|
292
|
+
* console.log(arr) // [1]
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
static trimArrayRight<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Trims falsy values from the left end of an array.
|
|
299
|
+
*
|
|
300
|
+
* @param arr - The array to trim
|
|
301
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
302
|
+
* @returns The trimmed array (modified in place)
|
|
303
|
+
*
|
|
304
|
+
* @throws {Sass} If arr or except is not an Array
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
309
|
+
*
|
|
310
|
+
* const arr = [null, undefined, "value"]
|
|
311
|
+
* Collection.trimArrayLeft(arr)
|
|
312
|
+
* console.log(arr) // ["value"]
|
|
237
313
|
* ```
|
|
238
314
|
*/
|
|
239
|
-
static
|
|
315
|
+
static trimArrayLeft<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
240
316
|
}
|