Skip to content

Commit

Permalink
feat(remove-from-array): added new func to remove all of specific ite…
Browse files Browse the repository at this point in the history
…m from array
  • Loading branch information
JamieSlome committed Dec 21, 2020
1 parent ed8e0c7 commit 7e580c8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const removeFrom = require("./util/removeFrom");
const removeFromString = require("./util/removeFromString");
const removeFromArray = require("./util/removeFromArray");

module.exports = {
removeFrom
removeFromString,
removeFromArray
};
26 changes: 23 additions & 3 deletions test/polyfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ const expect = require("chai").expect;
require("chai").should();

describe("polyfig", function () {
describe("removeFrom", function () {
describe("removeFromString", function () {
it("should exist as a method", function () {
expect(polyfig.removeFrom).to.exist;
expect(polyfig.removeFromString).to.exist;
});

it("should remove all occurrences of a string from another", function () {
const x = "polyfig is a simple JavaScript utility library";
const y = "polyfig";
const outcome = polyfig.removeFrom(x, y);
const outcome = polyfig.removeFromString(x, y);
outcome.should.equal(" is a simple JavaScript utility library");
});
});

describe("removeFromArray", function () {
it("should exist as a method", function () {
expect(polyfig.removeFromArray).to.exist;
});

it("should remove all occurrences of a specific number from array", function () {
const x = [1, 2, 3, 2, 1, 2, 3, 2, 1];
const y = 2;
const outcome = polyfig.removeFromArray(x, y);
outcome.should.eql([1, 3, 1, 3, 1]);
});

it("should remove all occurrences of a specific string from array", function () {
const x = ["1", "2", "3", "2", "1", "2", "3", "2", "1"];
const y = "2";
const outcome = polyfig.removeFromArray(x, y);
outcome.should.eql(["1", "3", "1", "3", "1"]);
});
});
});
23 changes: 23 additions & 0 deletions util/removeFromArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
* Summary - Remove from *array* `x`, all of specific item `y`
*
* Description - This method removes
* all of specific item of *any* `y` from *array* `x`
*
* @name removeFromArray
*
* @param {array} x - array
* @param {any} y - any
*
* @since 1.0.1
* @access public
*
* @return `x` with all of specific item `y` removed
*/

module.exports = (x, y) => {
return Array.isArray(x)
? x.filter((item) => item !== y)
: new Error("x is not of type array");
};
2 changes: 1 addition & 1 deletion util/removeFrom.js → util/removeFromString.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description - This method removes
* all occurrences of *string* `y` from *string* `x`
*
* @name removeFrom
* @name removeFromString
*
* @param {string} x - string
* @param {string} y - string
Expand Down

0 comments on commit 7e580c8

Please sign in to comment.