-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultToOneOf.js
27 lines (23 loc) · 1.11 KB
/
defaultToOneOf.js
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
/**
* @file Utility to set a default value from an array.
* @copyright Copyright (c) 2018-present, Graham Fairweather
* @module defaultToOneOf
*/
import _includes from './.internal/_includes';
import _last from './.internal/_last';
import requireObjectCoercible from './requireObjectCoercible';
/**
* Tests if a comparate exists in a list of comparates an then returns the comparate if there
* is a match; otherwise returns the last comparates value or fallback value if supplied.
*
* @param {Array|} [comparates=[]] - An array of values.
* @param {*} comparate - The value to compare against the supplied list of comparates.
* @param {Array} fallbackArg - The rest of the arguments array.
* @param {*} [fallbackArg.fallback=comparates[last]] - The returned value if no match exists.
* @returns {*} - The comparate upon a match; otherwise the fallback value.
*/
export default function defaultToOneOf(comparates, comparate, ...fallbackArg) {
requireObjectCoercible(comparates);
const fallback = fallbackArg.length ? fallbackArg[0] : _last(comparates);
return _includes(comparates, comparate) ? comparate : fallback;
}