2018-12-23 10:10:16 -08:00
|
|
|
// Some functions from Lodash that are a bit heavyweight and which
|
|
|
|
// we can just do in idiomatic ES2015+
|
|
|
|
|
|
|
|
export function get (obj, keys, defaultValue) {
|
2019-08-03 13:49:37 -07:00
|
|
|
for (const key of keys) {
|
2018-12-23 10:10:16 -08:00
|
|
|
if (obj && key in obj) {
|
|
|
|
obj = obj[key]
|
|
|
|
} else {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pickBy (obj, predicate) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const res = {}
|
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
2018-12-23 10:10:16 -08:00
|
|
|
if (predicate(value, key)) {
|
|
|
|
res[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-03-03 17:21:22 -08:00
|
|
|
|
|
|
|
export function padStart (string, length, chars) {
|
|
|
|
while (string.length < length) {
|
|
|
|
string = chars + string
|
|
|
|
}
|
|
|
|
return string
|
|
|
|
}
|
2019-10-13 08:08:06 -07:00
|
|
|
|
|
|
|
export function sum (list) {
|
|
|
|
let total = 0
|
|
|
|
for (const item of list) {
|
|
|
|
total += item
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
2019-11-17 20:51:28 -05:00
|
|
|
|
|
|
|
export function times (n, func) {
|
|
|
|
const res = []
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
res.push(func(i))
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|