Skip to content

Commit

Permalink
Changes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-m-dev committed Sep 18, 2024
1 parent 32a6769 commit f6caca5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 6 additions & 1 deletion @xen-orchestra/defined/.USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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
Expand All @@ -14,7 +19,7 @@ defined([undefined, null, 10])
// Returns null

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

defined(() => 'bar', 42)
// Returns 'bar'
Expand Down
13 changes: 10 additions & 3 deletions @xen-orchestra/defined/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ 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.
### `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'
Expand All @@ -29,7 +36,7 @@ defined([undefined, null, 10])
// Returns null

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

defined(() => 'bar', 42)
// Returns 'bar'
Expand Down
2 changes: 1 addition & 1 deletion @xen-orchestra/defined/defined.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('defined()', () => {
assert.deepStrictEqual(defined([undefined, null, 10]), expected)
})
it('should return first non undefined value in arrays', () => {
const expected = 10
const expected = [undefined, undefined, undefined]
assert.deepStrictEqual(defined([undefined, undefined, undefined], [undefined, undefined, 10]), expected)
})
it('should return first non undefined value in function', () => {
Expand Down
8 changes: 6 additions & 2 deletions @xen-orchestra/defined/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
// ])
// ```
function defined() {
const args = [].concat.apply([], arguments)
const n = args.length
let args = arguments
let n = args.length
if (n === 1) {
args = arguments[0]
n = args.length
}

for (let i = 0; i < n; ++i) {
let arg = args[i]
Expand Down

0 comments on commit f6caca5

Please sign in to comment.