diff --git a/@xen-orchestra/defined/.USAGE.md b/@xen-orchestra/defined/.USAGE.md index e69de29bb2d..bc71fccad85 100644 --- a/@xen-orchestra/defined/.USAGE.md +++ b/@xen-orchestra/defined/.USAGE.md @@ -0,0 +1,29 @@ +### `defined()` + +Returns the first non-`undefined` value from a list of arguments, evaluating functions if needed. + +There is two ways to use this function: + +- either with a single array argument: it should return the first non-undefined item +- or with multiple arguments: it should return the first non-undefined argument + +If only `undefined` values, return `undefined`. + +```js +import defined from '@xen-orchestra/defined/index.js' + +defined(undefined, 'foo', 42) +// Returns 'foo' + +defined([undefined, null, 10]) +// Returns null + +defined([undefined, undefined], [undefined, undefined, 10]) +// Returns [undefined, undefined] + +defined(() => 'bar', 42) +// Returns 'bar' + +defined(undefined, undefined) +// Returns undefined +``` diff --git a/@xen-orchestra/defined/README.md b/@xen-orchestra/defined/README.md index 6176dcc500a..942553f41aa 100644 --- a/@xen-orchestra/defined/README.md +++ b/@xen-orchestra/defined/README.md @@ -14,6 +14,37 @@ Installation of the [npm package](https://npmjs.org/package/@xen-orchestra/defin npm install --save @xen-orchestra/defined ``` +## Usage + +### `defined()` + +Returns the first non-`undefined` value from a list of arguments, evaluating functions if needed. + +There is two ways to use this function: +- either with a single array argument: it should return the first non-undefined item +- or with multiple arguments: it should return the first non-undefined argument + +If only `undefined` values, return `undefined`. + +```js +import defined from '@xen-orchestra/defined/index.js' + +defined(undefined, 'foo', 42) +// Returns 'foo' + +defined([undefined, null, 10]) +// Returns null + +defined([undefined, undefined], [undefined, undefined, 10]) +// Returns [undefined, undefined] + +defined(() => 'bar', 42) +// Returns 'bar' + +defined(undefined, undefined) +// Returns undefined +``` + ## Contributions Contributions are _very_ welcomed, either on the documentation or on diff --git a/@xen-orchestra/defined/index.js b/@xen-orchestra/defined/index.js index 923bb886f02..c95d52c151c 100644 --- a/@xen-orchestra/defined/index.js +++ b/@xen-orchestra/defined/index.js @@ -22,7 +22,7 @@ function defined() { } for (let i = 0; i < n; ++i) { - let arg = arguments[i] + let arg = args[i] if (typeof arg === 'function') { arg = get(arg) } diff --git a/@xen-orchestra/defined/index.test.mjs b/@xen-orchestra/defined/index.test.mjs new file mode 100644 index 00000000000..50ed51071bd --- /dev/null +++ b/@xen-orchestra/defined/index.test.mjs @@ -0,0 +1,30 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert/strict' +import defined_ from '@xen-orchestra/defined/index.js' + +// for each test, use the flat syntax and the array syntax +for (const [title, defined] of Object.entries({ + 'defined(...values)': defined_, + 'defined([ ...values ])': (...args) => defined_(args), +})) { + describe(title, () => { + it('returns the first non undefined value', () => { + assert.deepEqual(defined(undefined, 'foo', 42), 'foo') + }) + + it('returns undefined if only undefined values', () => { + assert.equal(defined(undefined, undefined), undefined) + }) + + it('resolves functions with `get(fn)`', () => { + const o = { foo: undefined, baz: 'baz' } + assert.deepEqual( + defined( + () => o.foo.bar, + () => o.baz + ), + 'baz' + ) + }) + }) +} diff --git a/@xen-orchestra/defined/package.json b/@xen-orchestra/defined/package.json index 4f01831508e..b4496d20677 100644 --- a/@xen-orchestra/defined/package.json +++ b/@xen-orchestra/defined/package.json @@ -23,6 +23,7 @@ "node": ">=6" }, "scripts": { - "postversion": "npm publish" + "postversion": "npm publish", + "test": "node --test" } } diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index b975766a1c9..1f51d541d1d 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -41,6 +41,7 @@ +- @xen-orchestra/defined patch - @xen-orchestra/lite minor - @xen-orchestra/web minor - @xen-orchestra/web-core minor