Skip to content

Instantly share code, notes, and snippets.

@MD4
Last active July 20, 2016 09:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MD4/c744443b5cf2bc283769dfdfd3653f14 to your computer and use it in GitHub Desktop.
Save MD4/c744443b5cf2bc283769dfdfd3653f14 to your computer and use it in GitHub Desktop.
🐒 Chainable forEach monkey patch !
(function(oldForEach) {
Array.prototype.forEach = function() {
oldForEach.apply(this, arguments);
return this;
};
}(Array.prototype.forEach));
[1, 2, 3]
.forEach(item => console.log(1, item))
.forEach(item => console.log(2, item))
// 1 1
// 1 2
// 1 3
// 2 1
// 2 2
// 2 3
@FXHibon
Copy link

FXHibon commented Jun 8, 2016

describe('forEach', () => {

    const testData = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];

    describe('oldForEach', () => {
        it('should not return anything', () => {
            const result = testData.forEach((item) => { testData.should.containEql(item); });
            should.not.exist(result);
        });
    });

    describe('newForEach', () => {
        it('should return the origina list', () => {

            require('./jojoChainable');

            const result = testData.forEach((item) => { testData.should.containEql(item); });
            should.exist(result);
            result.should.be.exactly(testData);
        });
    });

});

@MD4
Copy link
Author

MD4 commented Jun 8, 2016

🐵

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment