Skip to content

Commit

Permalink
test(defined): create tests for defined() (#7967)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephane MEYER <[email protected]>
Co-authored-by: Julien Fontanet <[email protected]>
  • Loading branch information
3 people authored Sep 24, 2024
1 parent 28709b6 commit b365e4a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
29 changes: 29 additions & 0 deletions @xen-orchestra/defined/.USAGE.md
Original file line number Diff line number Diff line change
@@ -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
```
31 changes: 31 additions & 0 deletions @xen-orchestra/defined/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion @xen-orchestra/defined/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions @xen-orchestra/defined/index.test.mjs
Original file line number Diff line number Diff line change
@@ -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'
)
})
})
}
3 changes: 2 additions & 1 deletion @xen-orchestra/defined/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"node": ">=6"
},
"scripts": {
"postversion": "npm publish"
"postversion": "npm publish",
"test": "node --test"
}
}
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<!--packages-start-->

- @xen-orchestra/defined patch
- @xen-orchestra/lite minor
- @xen-orchestra/web minor
- @xen-orchestra/web-core minor
Expand Down

0 comments on commit b365e4a

Please sign in to comment.