Skip to content

Commit f936c78

Browse files
committed
feat(array-utils): added coalesce as well as test suite
1 parent 62e9b8c commit f936c78

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { RecursiveArray } from './recursiveArray.d.ts'
2+
3+
export function coalesce<T>(arr: RecursiveArray<T>): Array<T>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Flattens and filters out nullish values from the given list of arguments.
3+
*
4+
* The arguments can be composed of multiple depths of objects and arrays.
5+
* The output is a single flat array with no missing values.
6+
*
7+
* @param {Array} arr - list of arguments
8+
* @returns {Array} a flat list of arguments
9+
*
10+
* @alias module:modeling/utils.coalesce
11+
* @function
12+
*/
13+
export const coalesce = (arr) => flattenHelper(arr, [])
14+
15+
// Helper to recursively append to a given list.
16+
// This is MUCH faster than other flatten methods.
17+
const flattenHelper = (arr, out) => {
18+
if (Array.isArray(arr)) {
19+
arr.forEach((child) => flattenHelper(child, out))
20+
} else if (arr != null && arr !== undefined) {
21+
out.push(arr)
22+
}
23+
return out
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava'
2+
3+
import { coalesce } from './index.js'
4+
5+
test('array-utils: coalesce() should coalesce arrays into an array', (t) => {
6+
let obs = coalesce([1, 2, 3, 4])
7+
let exp = [1, 2, 3, 4]
8+
t.deepEqual(obs, exp)
9+
10+
obs = coalesce([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
11+
exp = [1, 2, 3, 4, 5, 6, 7, 8, 9]
12+
t.deepEqual(obs, exp)
13+
})
14+
15+
test('array-utils: coalesce() should coalesce arrays with arrays and values into an array', (t) => {
16+
let obs = coalesce([[1], 2, 3, [4]])
17+
let exp = [1, 2, 3, 4]
18+
t.deepEqual(obs, exp)
19+
20+
obs = coalesce([1, [2, 3], 4])
21+
exp = [1, 2, 3, 4]
22+
t.deepEqual(obs, exp)
23+
})
24+
25+
test('array-utils: coalesce() should coalesce heiarchy of arrays and values into an array', (t) => {
26+
const obs = coalesce([[1], [2, 3, [4, 5]], 6])
27+
const exp = [1, 2, 3, 4, 5, 6]
28+
t.deepEqual(obs, exp)
29+
})
30+
31+
test('array-utils: coalesce() should coalesce arrays with nullish values into an array', (t) => {
32+
const obs = coalesce([[1], [2, null, [4, undefined]], undefined])
33+
const exp = [1, 2, 4]
34+
t.deepEqual(obs, exp)
35+
})

packages/array-utils/src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/**
22
* JSCAD Utility functions for arrays.
3+
*
34
* @module array-utils
5+
*
46
* @example
5-
* const { flatten, head } = require('@jscad/array-utils')
7+
* import { flatten, head } from '@jscad/array-utils'
68
*/
79

10+
export { coalesce } from './coalesce.js'
811
export { flatten } from './flatten.js'
912
export { fnNumberSort } from './fnNumberSort.js'
1013
export { head } from './head.js'

0 commit comments

Comments
 (0)