All files index.js

0% Statements 0/8
0% Branches 0/6
0% Functions 0/5
0% Lines 0/8

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                                                         
function parseKey(key) {
    return key.replace("[]", "");
}
// get(obj, 'a.b.c') => obj.a.b.c
export function getany(obj, key) {
    return key.split(".").reduce((prev, subkey) => {
        // a.b[].c b是数组
        return prev == null ? prev : prev[parseKey(subkey)];
    }, obj);
}
 
// set(obj, 'a.b.c', 1) => obj.a.b.c = 1
export function setany(obj, key, val) {
    const keys = key.split(".");
 
    const root = keys.slice(0, -1).reduce((parent, subkey) => {
        const realkey = parseKey(subkey);
        // a.b[].c b是数组
        // eslint-disable-next-line no-param-reassign
        return (parent[realkey] = parent[realkey]
            ? parent[realkey]
            : subkey.includes("[]")
            ? []
            : {});
    }, obj);
 
    root[keys[keys.length - 1]] = val;
}