Skip to content

Commit

Permalink
add recursivity to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychopoulet committed Sep 21, 2018
1 parent 0faafd4 commit cdee2bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,33 @@ module.exports = class NodeContainerPattern extends Map {
return this;
}

delete (key) {
super.delete(ensureKey(key));
delete (_key) {

const key = ensureKey(_key);

if (this.has(key)) {

if (-1 < key.indexOf(this._recursionSeparator)) {

const keys = key.split(this._recursionSeparator);
const lastKey = keys.pop();
const parentKey = keys.join(this._recursionSeparator);

const parent = this.get(parentKey);

delete parent[lastKey];

this.set(parentKey, parent);

}
else {
super.delete(key);
}

}

return this;

}

document (_key, documentation) {
Expand Down
20 changes: 20 additions & 0 deletions tests/4_delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@

describe("delete", () => {

beforeEach(() => {
container.clear();
});

it("should check normal running", () => {

assert.strictEqual(container.set("test", "test").delete("test") instanceof Container, true, "normal running has invalid return");
assert.strictEqual(container.set("test", "test").delete("test").size, 0, "normal running has invalid return");

});

it("should check recursive running", () => {

container.set("test", {
"test": "test",
"test2": "test2"
});

assert.strictEqual(container.delete("test.test") instanceof Container, true, "recursive running has invalid return");
assert.strictEqual(container.delete("test.test").size, 1, "recursive running has invalid return");

assert.deepStrictEqual(container.get("test").size, {
"test2": "test2"
}, "recursive running has invalid return");

});

});

0 comments on commit cdee2bf

Please sign in to comment.