-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdefaultTo.ts
More file actions
27 lines (22 loc) · 803 Bytes
/
defaultTo.ts
File metadata and controls
27 lines (22 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
// @types
type DefaultTo_2<T1> = <T2>(value: T2) => T1 | T2;
type DefaultTo =
& (<T1>(defaultV: T1) => DefaultTo_2<T1>)
& (<T1, T2>(defaultV: T1, value: T2) => T1 | T2);
function _defaultTo<T1, T2>(defaultV: T1, value: T2) {
return value == null || value !== value ? defaultV : value;
}
/**
* Returns the second argument if it is not `null`, `undefined` or `NaN`;
* otherwise the first argument is returned.
*
* const defaultTo125 = Fae.defaultTo(125)
*
* defaultTo125(null) //=> 125
* defaultTo125(undefined) //=> 125
* defaultTo125(false) //=> false
* defaultTo125('Fae') //=> 'Fae'
*/
export const defaultTo = curryN(2, _defaultTo) as DefaultTo;