Last active
December 26, 2019 07:22
-
-
Save rm-rf-etc/74b04ee0b09b165cb0a4a9cd296dbb7f to your computer and use it in GitHub Desktop.
Removes the `_` property from every level of a nest gun.js data node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import omit from "lodash.omit"; | |
import keys from "lodash.keys"; | |
export default (ref1) => { | |
if (typeof ref1 !== "object") return ref1; | |
function stripProp(prop, ref2) { | |
if (typeof ref2[prop] !== "object") return; | |
/* eslint-disable-next-line no-param-reassign */ | |
ref2[prop] = omit(ref2[prop], "_"); | |
keys(ref2[prop]).forEach((next) => stripProp(next, ref2[prop])); | |
} | |
const newRef1 = omit(ref1, "_"); | |
keys(ref1).forEach((key) => stripProp(key, newRef1)); | |
return newRef1; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import stripDeep from "./omit"; | |
const testNested = { | |
_: { | |
a: 1, | |
b: "2", | |
}, | |
a: 1, | |
b: "2", | |
c: { | |
_: { | |
a: 1, | |
b: "2", | |
}, | |
a: 1, | |
b: "2", | |
c: { | |
_: { | |
a: 1, | |
b: "2", | |
}, | |
a: 1, | |
b: "2", | |
}, | |
}, | |
}; | |
describe("util method stripDeep", () => { | |
it("removes `_` property", () => { | |
expect(stripDeep(testNested)).toEqual({ | |
a: 1, | |
b: "2", | |
c: { | |
a: 1, | |
b: "2", | |
c: { | |
a: 1, | |
b: "2", | |
}, | |
}, | |
}); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment