Skip to content

Instantly share code, notes, and snippets.

@rm-rf-etc
Last active December 26, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rm-rf-etc/74b04ee0b09b165cb0a4a9cd296dbb7f to your computer and use it in GitHub Desktop.
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.
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;
};
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