Skip to content

Commit

Permalink
feat: add merge() function
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 19, 2017
1 parent f3a9e50 commit 285325e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Expand Up @@ -333,4 +333,22 @@ Kareem.prototype.clone = function() {
return n;
};

Kareem.prototype.merge = function(other) {
var ret = this.clone();
for (var key in other._pres) {
if (!other._pres.hasOwnProperty(key)) {
continue;
}
ret._pres[key] = (ret._pres[key] || []).concat(other._pres[key].slice());
}
for (var key in other._posts) {
if (!other._posts.hasOwnProperty(key)) {
continue;
}
ret._posts[key] = (ret._posts[key] || []).concat(other._posts[key].slice());
}

return ret;
};

module.exports = Kareem;
18 changes: 18 additions & 0 deletions test/examples.test.js
Expand Up @@ -337,3 +337,21 @@ describe('clone()', function() {
assert.deepEqual(['cook'], Object.keys(k2._posts));
});
});

describe('merge()', function() {
it('pulls hooks from another Kareem object', function() {
var k1 = new Kareem();
var test1 = function() {};
k1.pre('cook', test1);
k1.post('cook', function() {});

var k2 = new Kareem();
var test2 = function() {};
k2.pre('cook', test2);
var k3 = k2.merge(k1);
assert.equal(k3._pres['cook'].length, 2);
assert.equal(k3._pres['cook'][0].fn, test2);
assert.equal(k3._pres['cook'][1].fn, test1);
assert.equal(k3._posts['cook'].length, 1);
});
});

0 comments on commit 285325e

Please sign in to comment.