Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 50x 50x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // ========= Object const isObject = o => (typeof o === 'object' && o !== null && o !== undefined && Object.prototype.toString.call(o) === '[object Object]') const has = (o, property) => isObject(o) && Object.prototype.hasOwnProperty.call(o, property) const get = (o, property, defaultValue) => (o !== undefined && has(o, property) ? o[property] : defaultValue) const set = (o, property, value) => ({ ...o, [property]: value }) const isEmpty = o => !(isObject(o) && o.constructor === Object && Object.keys(o).length > 0) const toArray = o => (isObject(o) ? Object.keys(o) .map(t => o[t]) : []) const deep = (obj, props, defaultValue) => { // If we have reached an undefined/null property // then stop executing and return the default value. // If no default is provided it will return undefined. if (obj === undefined || obj === null) { return defaultValue } // If the path array has no more elements, we've reached // the intended property and return its value if (props.length === 0) { return obj } // Prepare our found property and path array for recursion const foundSoFar = obj[props[0]] const remainingProps = props.slice(1) return deep(foundSoFar, remainingProps, defaultValue) } const deepGet = (obj, props, defaultValue) => { // If we have reached an undefined/null property // then stop executing and return the default value. // If no default is provided it will return undefined. if (obj === undefined || obj === null) { return defaultValue } // If the path array has no more elements, we've reached // the intended property and return its value if (props.length === 0) { return obj } // Prepare our found property and path array for recursion const foundSoFar = obj[props[0]] const remainingProps = props.slice(1) return deepGet(foundSoFar, remainingProps, defaultValue) } const toArrayFilter = (o, r) => (isObject(o) ? Object.keys(o) .filter(t => r.test(t)) .map(t => o[t]) : []) const filterKeys = (o, r) => (isObject(o) ? Object.keys(o) .filter(t => r.test(t)) .reduce( (n, t) => set(n, t, o[t]), {} ) : {}) const cast = (fields, props) => { const items = (isObject(fields) ? Object.keys(fields) : []) const item = items.reduce( (acc, val) => set(acc, val, get(props, val, fields[val])), {} ) if (has(props, '_id')) { set(item, '_id', get(props, '_id', 0)) // item['_id'] = get(props, '_id', 0) } return item } export const createFilter = (fields, props) => Object.keys(fields) .reduce( (acc, val) => (has(props, val) ? set(acc, val, get(props, val, fields[val])) : acc), {} ) export const changeKey = (o, key, f, def = '') => set(o, key, f(get(o, key, def))) export const deepChangeKey = (o, key, f, def = '') => Object.keys(o) .reduce((a, c) => set(a, c, changeKey(o[c], key, f, def)), {}) // ========= Exports export const obj = { has, get, set, deep, deepGet, changeKey, deepChangeKey, isEmpty, toArray, toArrayFilter, cast, createFilter, filterKeys, isObject, } export default obj |