foop
Version:
interfaces that describe their intentions.
59 lines (58 loc) • 1.43 kB
JavaScript
// /**
// * get attribute from path, return undefined when no such path
// * @param {Object} obj
// * @param {String} path like: 'a.b.c.0.e'
// * @return {any}
// */
// const get = (obj, path, defaultValue) => {
// if (!isPlainObject(obj) && !isArray(obj)) return undefined
// if (!path) return obj
// //compatible with lodash style
// path = path.replace(/\[|\]\./g, '.')
//
// let pathArray = path.split('.')
// let p;
// let v = obj;
// while ((p = pathArray.shift())) {
// v = v[p]
// if (isUndefined(v)) return defaultValue
// }
// return v
// }
//
// const set = (obj, path, value) => {
// if (!isPlainObject(obj)) return undefined
//
// //compatible with lodash style
// path = path.replace(/\[|\]\./g, '.')
//
// let pathArray = path.split('.')
// let p;
// let v = obj;
//
// for (let i = 0; i < pathArray.length; i++) {
// let p = pathArray[i]
// if (i >= pathArray.length - 1) {
// v[p] = value
// } else {
// if (isUndefined(v[p])) v[p] = {}
// }
// v = v[p]
// }
// return obj
// }
//
// const pick = (obj, keys) => {
// if (!isPlainObject(obj) || !isArray(keys)) return obj
// return keys.map(path => {
// const value = get(obj, path)
// const key = last(path.split('.'))
// return {
// key,
// value
// }
// }).reduce((memo, o) => {
// memo[o.key] = o.value
// return memo
// }, {})
// }