Skip to content

Commit f5dabe8

Browse files
authored
reorg(all): promote array utils package
* reorg(all): promoted array-utils package * feat(array-utils): added coalesce as well as test suite * docs(array-utils): tweaks to docs and annotations
1 parent 17ebf6a commit f5dabe8

38 files changed

+128
-32
lines changed

jsdoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"source" : {
33
"include": [
4-
"packages/utils/array-utils/src",
4+
"packages/array-utils/src",
55

66
"packages/modeling/src/colors",
77
"packages/modeling/src/curves",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"homepage": "https://openjscad.xyz/",
66
"repository": "https://github.com/jscad/OpenJSCAD.org",
7+
"type": "module",
78
"scripts": {
89
"docs": "jsdoc --configure jsdoc.json",
910
"coverage": "lerna run --concurrency 1 --stream coverage",
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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+
}

0 commit comments

Comments
 (0)