Skip to content

Commit

Permalink
chore: create unit tests for defined
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-m-dev committed Sep 9, 2024
1 parent 6c8d41d commit 2113e67
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions @xen-orchestra/defined/.USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The defined() function returns the first non-undefined value from a list of arguments, evaluating functions if needed.
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 [undefined, null, 10]

defined([undefined, undefined], [undefined, undefined, 10])
// Returns [undefined, undefined]

defined(() => 'bar', 42)
// Returns 'bar'

defined(undefined, undefined)
// Returns undefined
```
24 changes: 24 additions & 0 deletions @xen-orchestra/defined/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ Installation of the [npm package](https://npmjs.org/package/@xen-orchestra/defin
npm install --save @xen-orchestra/defined
```

## Usage

The defined() function returns the first non-undefined value from a list of arguments, evaluating functions if needed.
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 [undefined, null, 10]

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
29 changes: 29 additions & 0 deletions @xen-orchestra/defined/defined.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import defined from '@xen-orchestra/defined/index.js'

describe('Utilities to help handling (possibly) undefined values', () => {
it('should return first non undefined value in args', () => {
const expected = 'foo'
assert.deepStrictEqual(defined(undefined, 'foo', 42), expected)
})
it('should return first non undefined value in array', () => {
const expected = [undefined, null, 10]
assert.deepStrictEqual(defined([undefined, null, 10]), expected)
})
it('should return first non undefined value in arrays', () => {
const expected = [undefined, undefined, undefined]
assert.deepStrictEqual(defined([undefined, undefined, undefined], [undefined, undefined, 10]), expected)
})
it('should return first non undefined value in function', () => {
const expected = 'bar'
assert.deepStrictEqual(
defined(() => 'bar', 42),
expected
)
})
it('should return undefined if only undefined values', () => {
const expected = undefined
assert.equal(defined(undefined, undefined), expected)
})
})

0 comments on commit 2113e67

Please sign in to comment.