Skip to content

Commit

Permalink
✨ Add defaultTo function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 29, 2021
1 parent 10e6e57 commit ebe3c90
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/api/defaultto/index.md
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'

```

2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| [and](./and/) | Returns true if both arguments are true; otherwise false. |
| [append](./append/) | Returns a new list containing the contents of the given list, followed by the given value |
| [dec](./dec/) | Decrements its argument. |
| [defaultTo](./defaultto/) | <b><i>(BETA)</i></b> Returns the second argument if it is not <code>null</code>, <code>undefined</code> or <code>NaN</code>; otherwise the first argument is returned. |
| [divide](./divide/) | Divide its second argument from its first argument. |
| [endsWith](./endswith/) | Checks if a string ends with the provided substring. |
| [F](./f/) | A function that always returns <code>false</code>. Any passed in parameters are ignored. |
Expand All @@ -28,6 +29,7 @@
| [isBoolean](./isboolean/) | Whatever argument is type of <code>boolean</code> or not. |
| [isEmpty](./isempty/) | Returns <code>true</code> if the given value is its type's empty value; otherwise <code>false</code>. |
| [isFunction](./isfunction/) | Whatever argument is type of <code>function</code> or not. |
| [isNaN](./isnan/) | <b><i>(BETA)</i></b> Whatever argument is <code>NaN</code> or not. |
| [isNill](./isnill/) | Whatever argument is type of <code>undefined</code> or <code>null</code>. |
| [isNull](./isnull/) | Whatever argument is type of <code>null</code> or not. |
| [isNumber](./isnumber/) | Whatever argument is type of <code>number</code> or not. |
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { and } from './src/and.ts'
export { append } from './src/append.ts'
export { _ } from './src/constants/index.ts'
export { dec } from './src/dec.ts'
export { defaultTo } from './src/defaultTo.ts'
export { divide } from './src/divide.ts'
export { endsWith } from './src/endsWith.ts'
export { F } from './src/F.ts'
Expand All @@ -19,6 +20,7 @@ export { isBigint } from './src/isBigint.ts'
export { isBoolean } from './src/isBoolean.ts'
export { isEmpty } from './src/isEmpty.ts'
export { isFunction } from './src/isFunction.ts'
export { isNaN } from './src/isNaN.ts'
export { isNill } from './src/isNill.ts'
export { isNull } from './src/isNull.ts'
export { isNumber } from './src/isNumber.ts'
Expand Down
34 changes: 34 additions & 0 deletions src/defaultTo.ts
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 }
20 changes: 20 additions & 0 deletions test/defaultTo.spec.ts
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)
})
})

0 comments on commit ebe3c90

Please sign in to comment.