Skip to content

Commit 4a5d3dc

Browse files
authored
Merge pull request webpack#4469 from willmendesneto/refactor-remove-and-do
refactor(removeAndDo): upgrading to ES6
2 parents 62d56e9 + c326071 commit 4a5d3dc

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

lib/removeAndDo.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
"use strict";
6+
57
module.exports = function removeAndDo(collection, thing, action) {
6-
var idx = this[collection].indexOf(thing);
7-
if(idx >= 0) {
8+
const idx = this[collection].indexOf(thing);
9+
const hasThingInCollection = idx >= 0;
10+
if(hasThingInCollection) {
811
this[collection].splice(idx, 1);
912
thing[action](this);
10-
return true;
1113
}
12-
return false;
14+
return hasThingInCollection;
1315
};

test/removeAndDo.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* globals describe, it, beforeEach */
2+
"use strict";
3+
4+
const should = require("should");
5+
const sinon = require("sinon");
6+
const removeAndDo = require("../lib/removeAndDo");
7+
8+
describe("removeAndDo", () => {
9+
let actionSpy;
10+
let thingsMock;
11+
let contextMock;
12+
let anotherThingsMock;
13+
14+
beforeEach(() => {
15+
actionSpy = sinon.spy();
16+
thingsMock = {
17+
action: actionSpy
18+
};
19+
anotherThingsMock = {
20+
action: actionSpy
21+
};
22+
contextMock = {
23+
context: [thingsMock]
24+
};
25+
});
26+
27+
it("should return true", () => {
28+
should(removeAndDo.bind(contextMock)('context', thingsMock, 'action')).be.eql(true);
29+
actionSpy.callCount.should.be.exactly(1);
30+
});
31+
32+
it("should return false", () => {
33+
should(removeAndDo.bind(contextMock)('context', anotherThingsMock, 'anotherAction')).be.eql(false);
34+
actionSpy.callCount.should.be.exactly(0);
35+
});
36+
});

0 commit comments

Comments
 (0)