Skip to content

Commit

Permalink
new discard() method (#7)
Browse files Browse the repository at this point in the history
* new discard() method

* fix(test): updated tests and docs
  • Loading branch information
oguzzkilic committed Jul 3, 2019
1 parent fa95151 commit 430f5a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ let nums = new SuperSet([0, 1, 2]);
nums.update([2, 4, 6]); // → SuperSet { 0, 1, 2, 4, 6 }
```

## `discard(iterable)`

The discard() method deletes the iterable elements from the data set and returns the updated elements.

### Example

```js
let nums = new SuperSet([1, 2, 3, 4]);
nums.discard([1, 3]); // → SuperSet { 2, 4 }
```

---

Parts of this documentation are adapted from [MDN][mdn-url].
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ class SuperSet extends Set {
symmetricDiff(otherSetObj) {
return new SuperSet(xorGen(this, otherSetObj));
}

/**
* The discard() method deletes the iterable elements from the set and return the updated elements.
*
* @param {Iterable} iterable It contains iterable elements to be deleted from the current set.
* @returns {SuperSet} It returns the current state of the set after the deleted elements.
*/

discard(iterable) {
for (const itm of iterable)
this.delete(itm);

return this;
}
}

module.exports = SuperSet;
22 changes: 22 additions & 0 deletions test/superset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,26 @@ describe("SuperSet", () => {
expect(Array.from(result)).to.eql([1, 4, 5]);
});
});

describe("discard", () => {
it("should delete elements in an iterable way and return updated elements.", () => {
const result = testSet.discard([1, 3]);

expect(Array.from(result)).to.eql([2]);
});

it("should delete undefined elements and return updated elements.", () => {
const result = testSet.discard([7]);

expect(Array.from(result)).to.eql([1, 2, 3]);
});

it("should delete again same elements in an iterable way and return current elements.", () => {
const result = testSet.discard([1]);
expect(Array.from(result)).to.eql([2, 3]);

const sameResult = testSet.discard([1]);
expect(Array.from(sameResult)).to.eql([2, 3]);
});
});
});

0 comments on commit 430f5a5

Please sign in to comment.