Skip to content

Commit

Permalink
Merge branch 'hotfix/4.0.28'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrmnn committed Sep 21, 2018
2 parents e4e204c + 7f186a9 commit 5e3ca73
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
8 changes: 5 additions & 3 deletions build/collect.js
Expand Up @@ -2703,10 +2703,12 @@ module.exports = function values() {

module.exports = function when(value, fn, defaultFn) {
if (value) {
fn(this);
} else {
defaultFn(this);
return fn(this, value);
} else if (defaultFn) {
return defaultFn(this, value);
}

return this;
};

/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion build/collect.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions dist/methods/when.js
Expand Up @@ -2,8 +2,10 @@

module.exports = function when(value, fn, defaultFn) {
if (value) {
fn(this);
} else {
defaultFn(this);
return fn(this, value);
} else if (defaultFn) {
return defaultFn(this, value);
}

return this;
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "collect.js",
"version": "4.0.27",
"version": "4.0.28",
"description": "Convenient and dependency free wrapper for working with arrays and objects.",
"main": "dist/index.js",
"typings": "index.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions src/methods/when.js
Expand Up @@ -2,8 +2,10 @@

module.exports = function when(value, fn, defaultFn) {
if (value) {
fn(this);
} else {
defaultFn(this);
return fn(this, value);
} else if (defaultFn) {
return defaultFn(this, value);
}

return this;
};
31 changes: 31 additions & 0 deletions test/methods/when_test.js
Expand Up @@ -18,4 +18,35 @@ module.exports = (it, expect, collect) => {

expect(collection.all()).to.eql([1, 2, 3, 4, 6]);
});

it('should pass the value over to the callback', () => {
let collection = collect(['michael', 'tom']);

collection.when('adam', (innerCollection, newName) => innerCollection.push(newName));

expect(collection.all()).to.eql(['michael', 'tom', 'adam']);

collection = collect(['michael', 'tom']);

collection.when(false, (innerCollection, newName) => innerCollection.push(newName));

expect(collection.all()).to.eql(['michael', 'tom']);
});

it('should call the default callback if the value is false', () => {
const collection = collect(['michael', 'tom']);

collection.when(false, innerCollection => innerCollection.push('adam'), innerCollection => innerCollection.push('taylor'));

expect(collection.all()).to.eql(['michael', 'tom', 'taylor']);
});

it('should return the collection object', () => {
const collection = collect(['michael', 'tom']);

const newCollection = collection.when('adam', innerCollection => innerCollection.push('adam'));

expect(newCollection).to.eql(collection);
expect(collection.all()).to.eql(['michael', 'tom', 'adam']);
});
};

0 comments on commit 5e3ca73

Please sign in to comment.