-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (29 loc) · 1009 Bytes
/
index.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
28
29
30
31
32
33
'use strict'
/**
* @description Looks for a valid value in an object, iterating over a priority ordered array of keys.
* @param {Object} obj - key:value object.
* @param {Array} order - key order to look for values in `obj` object.
* @param {Object} options
* @param {boolean} [options.falsyValues] - `true` for falsy values to be valid. Defaults to `false`.
* @returns {*} value - First allowed value according to `order` array priority.
*/
module.exports = function propertyWaterfall (obj, order, { falsyValues = false } = {}) {
let value
// Iterate over order array to respect priorities.
for (let i = 0; i < order.length; i++) {
// If object HAS the property.
if (obj.hasOwnProperty(order[i])) {
// Falsy values allowed.
if (falsyValues) {
value = obj[order[i]]
break
}
// Falsy values NOT allowed & value is thruthy.
if (!falsyValues && obj[order[i]]) {
value = obj[order[i]]
break
}
}
}
return value
}