@gesslar/toolkit 0.2.5 → 0.2.6
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 +37 -0
- package/src/types/Collection.d.ts +60 -0
package/package.json
CHANGED
package/src/lib/Collection.js
CHANGED
|
@@ -522,4 +522,41 @@ export default class Collection {
|
|
|
522
522
|
return acc
|
|
523
523
|
}, {})
|
|
524
524
|
}
|
|
525
|
+
|
|
526
|
+
static trimArray(arr, except=[]) {
|
|
527
|
+
Valid.type(arr, "Array")
|
|
528
|
+
Valid.type(except, "Array")
|
|
529
|
+
|
|
530
|
+
Collection.trimArrayLeft(arr, except)
|
|
531
|
+
Collection.trimArrayRight(arr, except)
|
|
532
|
+
|
|
533
|
+
return arr
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
static trimArrayRight(arr, except=[]) {
|
|
537
|
+
Valid.type(arr, "Array")
|
|
538
|
+
Valid.type(except, "Array")
|
|
539
|
+
|
|
540
|
+
arr.reverse()
|
|
541
|
+
Collection.trimArrayLeft(arr, except)
|
|
542
|
+
arr.reverse()
|
|
543
|
+
|
|
544
|
+
return arr
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
static trimArrayLeft(arr, except=[]) {
|
|
548
|
+
Valid.type(arr, "Array")
|
|
549
|
+
Valid.type(except, "Array")
|
|
550
|
+
|
|
551
|
+
while(arr.length > 0) {
|
|
552
|
+
const value = arr[0]
|
|
553
|
+
|
|
554
|
+
if(value || except.includes(value))
|
|
555
|
+
break
|
|
556
|
+
|
|
557
|
+
arr.shift()
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return arr
|
|
561
|
+
}
|
|
525
562
|
}
|
|
@@ -237,4 +237,64 @@ export default class Collection {
|
|
|
237
237
|
* ```
|
|
238
238
|
*/
|
|
239
239
|
static flattenObjectArray(objects: Array<Record<string, unknown>>): Record<string, Array<unknown>>
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Trims falsy values from both ends of an array.
|
|
243
|
+
*
|
|
244
|
+
* @param arr - The array to trim
|
|
245
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
246
|
+
* @returns The trimmed array (modified in place)
|
|
247
|
+
*
|
|
248
|
+
* @throws {Sass} If arr or except is not an Array
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
253
|
+
*
|
|
254
|
+
* const arr = [null, 0, 1, 2, "", undefined]
|
|
255
|
+
* Collection.trimArray(arr)
|
|
256
|
+
* console.log(arr) // [1, 2]
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
static trimArray<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Trims falsy values from the right end of an array.
|
|
263
|
+
*
|
|
264
|
+
* @param arr - The array to trim
|
|
265
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
266
|
+
* @returns The trimmed array (modified in place)
|
|
267
|
+
*
|
|
268
|
+
* @throws {Sass} If arr or except is not an Array
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
273
|
+
*
|
|
274
|
+
* const arr = [1, "", undefined]
|
|
275
|
+
* Collection.trimArrayRight(arr)
|
|
276
|
+
* console.log(arr) // [1]
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
static trimArrayRight<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Trims falsy values from the left end of an array.
|
|
283
|
+
*
|
|
284
|
+
* @param arr - The array to trim
|
|
285
|
+
* @param except - Array of values to exclude from trimming (default: [])
|
|
286
|
+
* @returns The trimmed array (modified in place)
|
|
287
|
+
*
|
|
288
|
+
* @throws {Sass} If arr or except is not an Array
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* import { Collection } from '@gesslar/toolkit'
|
|
293
|
+
*
|
|
294
|
+
* const arr = [null, undefined, "value"]
|
|
295
|
+
* Collection.trimArrayLeft(arr)
|
|
296
|
+
* console.log(arr) // ["value"]
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
static trimArrayLeft<T>(arr: Array<T>, except?: Array<T>): Array<T>
|
|
240
300
|
}
|