All files / src util.js

100% Statements 9/9
100% Branches 1/1
100% Functions 5/5
100% Lines 6/6
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            4x 59x               68x               23x               4x 34x              
/**
 * 合并普通的js对象
 * @param prev
 * @param next
 * @return {{}}
 */
const mergePlainObject = function (prev, next) {
    return ({...prev, ...next})
}
 
/**
 * 循环key
 * @param target
 * @return {string[]}
 */
const mapKeys = (target) => Object.keys(target)
 
/**
 * 创建新的key
 * @param namespace
 * @param key
 * @return {string}
 */
const createNewKeyStr = (namespace, key) => `${namespace}/${key}`
 
/**
 * 根据namespace重命名普通的js对象的key
 * @param namespace
 * @param plainObject
 * @return {{[p: string]: *}}
 */
const renamePlainObjectKey = (namespace, plainObject = {}) =>
    mapKeys(plainObject).map((key) => ({[createNewKeyStr(namespace, key)]: plainObject[key]})).reduce(mergePlainObject, {})
 
export {
    mergePlainObject,
    mapKeys,
    createNewKeyStr,
    renamePlainObjectKey
}