UNPKG

1.59 kBJavaScriptView Raw
1import {isBoolean, isString, isFn, isArray} from './types';
2
3/** Configuration settings helpers */
4
5/**
6 * If passed value is not of boolean type return the default value
7 * otherwise return the value itself
8 * @param {Boolean|Any} value
9 * @param {Boolean} default value
10 * @return {Boolean|Any}
11 */
12export const defaultsBool =
13 (val, defaultVal) => isBoolean(val) ? val : defaultVal;
14
15/**
16 * If passed value is not of string type return the default value
17 * otherwise return the value itself
18 * @param {String|Any} value
19 * @param {String} default value
20 * @return {String|Any}
21 */
22export const defaultsStr =
23 (val, defaultVal) => isString(val) ? val : defaultVal;
24
25/**
26 * If passed value is not of number type return the default value
27 * otherwise return the value itself
28 * @param {Number|Any} value
29 * @param {Number} default value
30 * @return {Number|Any}
31 */
32export const defaultsNb =
33 (val, defaultVal) => isNaN(val) ? defaultVal : val;
34
35/**
36 * If passed value is not of array type return the default value
37 * otherwise return the value itself
38 * @param {Array|Any} value
39 * @param {Array} default value
40 * @return {Array|Any}
41 */
42export const defaultsArr =
43 (val, defaultVal) => isArray(val) ? val : defaultVal;
44
45/**
46 * If passed value is not of function type return the default value
47 * otherwise return the value itself
48 * @param {Function|Any} value
49 * @param {Function} default value
50 * @return {Function|Any}
51 */
52export const defaultsFn =
53 (val, defaultVal) => isFn(val) ? val : defaultVal;