-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10e6e57
commit ebe3c90
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
|
||
|
||
## defaultTo variable | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Returns the second argument if it is not `null`<!-- -->, `undefined` or `NaN`<!-- -->; otherwise the first argument is returned. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
defaultTo: <T extends unknown>(a: T) => <U extends unknown>(b: U) => IsNill<U> extends true ? T : IsNumber<U> extends false ? U : T | U | ||
``` | ||
|
||
## Example | ||
|
||
|
||
```ts | ||
const defaultVal = defaultTo('anonymous') | ||
defaultVal(undefined) // 'anonymous' | ||
defaultVal(null) // 'anonymous' | ||
defaultVal(NaN) // 'anonymous' | ||
|
||
defaultVal('Tom') // 'Tom' | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isNaN } from './isNaN.ts' | ||
import { IsNill, isNill } from './isNill.ts' | ||
import { IsNumber } from './isNumber.ts' | ||
import { or } from './or.ts' | ||
|
||
/** | ||
* Returns the second argument if it is not `null`, `undefined` or `NaN`; otherwise the first argument is returned. | ||
* | ||
* @param a - `a` will be returned instead of `default` | ||
* @returns Returns a function that stores the default `a` value. The function accept `b` argument. | ||
* if `b` is `null`, `undefined` or `NaN`, return `a`; otherwise return `b` | ||
* | ||
* @example | ||
* ```ts | ||
* const defaultVal = defaultTo('anonymous') | ||
* defaultVal(undefined) // 'anonymous' | ||
* defaultVal(null) // 'anonymous' | ||
* defaultVal(NaN) // 'anonymous' | ||
* | ||
* defaultVal('Tom') // 'Tom' | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const defaultTo = <T extends unknown>(a: T) => <U extends unknown>( | ||
b: U | ||
): IsNill<U> extends true ? T : IsNumber<U> extends false ? U : T | U => | ||
(or(isNill(b), isNaN(b)) ? a : b) as IsNill<U> extends true | ||
? T | ||
: IsNumber<U> extends false | ||
? U | ||
: T | U | ||
|
||
export { defaultTo } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { defaultTo } from '@/defaultTo' | ||
|
||
describe('defaultTo', () => { | ||
const defaultValue = 'hello' | ||
const table: [unknown, unknown, unknown][] = [ | ||
[defaultValue, '', ''], | ||
[defaultValue, 'world', 'world'], | ||
[defaultValue, undefined, defaultValue], | ||
[defaultValue, null, defaultValue], | ||
[defaultValue, NaN, defaultValue], | ||
[defaultValue, NaN, defaultValue], | ||
[defaultValue, 0, 0], | ||
[defaultValue, {}, {}], | ||
[defaultValue, [], []] | ||
] | ||
|
||
it.each(table)('defaultTo(%s, %s) -> %s', (a, b, expected) => { | ||
expect(defaultTo(a)(b)).toEqual(expected) | ||
}) | ||
}) |