Permalink
Browse files

Make .extract, .find and .findOne chainable

Instead of an array, they now return results in a new mangler object.
Calling .end() on it will return the original object. After multiple
calls, .endAll() will return the root mangler object.
  • Loading branch information...
1 parent 51b60f1 commit 229215d82110020998c8a7b6ae26584f62ac96bc @DarthJDG committed Sep 5, 2016
Showing with 61 additions and 7 deletions.
  1. +19 −3 mangler.js
  2. +42 −4 test/test.js
View
@@ -1060,13 +1060,25 @@ var Mangler = (function(global) {
return this;
},
+ end: function() {
+ return this.parent || this;
+ },
+
+ endAll: function() {
+ var ret = this;
+ while(ret.parent) ret = ret.parent;
+ return ret;
+ },
+
explore: function(callback, state) {
fn.explore(this.items, callback, state);
return this;
},
extract: function(filter, options) {
- return fn.extract(this.items, filter, options);
+ var m = new ManglerObject(fn.extract(this.items, filter, options));
+ m.parent = this;
+ return m;
},
filter: function(cond) {
@@ -1075,11 +1087,15 @@ var Mangler = (function(global) {
},
find: function(cond) {
- return fn.find(this.items, cond);
+ var m = new ManglerObject(fn.find(this.items, cond));
+ m.parent = this;
+ return m;
},
findOne: function(cond) {
- return fn.first(this.items, cond);
+ var m = new ManglerObject(fn.first(this.items, cond));
+ m.parent = this;
+ return m;
},
first: function(cond) {
View
@@ -1103,6 +1103,29 @@
assert.strictEqual(result, 'ABC', 'passed');
});
+ QUnit.test('.end, .endAll', function(assert) {
+ assert.expect(13);
+
+ var m = Mangler([1,2,3,4,5]);
+ var m1 = m.find({ $lt: 5 });
+ var m2 = m1.find({ $gt: 1});
+ var m3 = m2.findOne({ $eq: 3 });
+
+ assert.notStrictEqual(m, m1, 'reference validation');
+ assert.notStrictEqual(m1, m2, 'reference validation');
+ assert.notStrictEqual(m2, m3, 'reference validation');
+ assert.deepEqual(m.items, [1,2,3,4,5], 'result validation');
+ assert.deepEqual(m1.items, [1,2,3,4], 'result validation');
+ assert.deepEqual(m2.items, [2,3,4], 'result validation');
+ assert.deepEqual(m3.items, [3], 'result validation');
+ assert.strictEqual(m3.end(), m2, 'end validation');
+ assert.strictEqual(m2.end(), m1, 'end validation');
+ assert.strictEqual(m1.end(), m, 'end validation');
+ assert.strictEqual(m.end(), m, 'end validation');
+ assert.strictEqual(m3.endAll(), m, 'endAll validation');
+ assert.strictEqual(m.endAll(), m, 'endAll validation');
+ });
+
QUnit.test('.explore', function(assert) {
var result = '';
Mangler([{ a: 'A' }, { b: 'B' }, { c: 'C' }]).explore(function(k, v, state) {
@@ -1112,7 +1135,7 @@
});
QUnit.test('.extract', function(assert) {
- assert.expect(2);
+ assert.expect(4);
var data = {
manager: { name: 'John' },
@@ -1123,26 +1146,41 @@
};
var m = Mangler(data);
+ var m1 = m.extract('manager|employees', { key: true, prop: true });
- assert.deepEqual(m.extract('manager|employees', { key: true, prop: true }), [
+ assert.deepEqual(m1.items, [
{ name: 'John', key: 'manager', prop: 'manager' },
{ name: 'Fred', key: 0, prop: 'employees' },
{ name: 'Bill', key: 1, prop: 'employees' }
], 'passed');
assert.strictEqual(m.items[0], data, 'original items preserved');
+ assert.notStrictEqual(m, m1, 'reference check');
+ assert.strictEqual(m1.end(), m, 'end check');
});
QUnit.test('.filter', function(assert) {
assert.deepEqual(Mangler([1, 2, 3]).filter({ $ne: 2 }).items, [1, 3], 'passed');
});
QUnit.test('.find', function(assert) {
- assert.deepEqual(Mangler([1, 2, 3]).find({ $lt: 3 }), [1, 2], 'passed');
+ assert.expect(3);
+ var m = Mangler([1, 2, 3]);
+ var m1 = m.find({ $lt: 3 });
+
+ assert.deepEqual(m1.items, [1, 2], 'passed');
+ assert.notStrictEqual(m, m1, 'reference check');
+ assert.strictEqual(m1.end(), m, 'end check');
});
QUnit.test('.findOne', function(assert) {
- assert.strictEqual(Mangler([1, 2, 3]).findOne({ $lt: 3 }), 1, 'passed');
+ assert.expect(3);
+ var m = Mangler([1, 2, 3]);
+ var m1 = m.findOne({ $lt: 3 });
+
+ assert.deepEqual(m1.items, [1], 'passed');
+ assert.notStrictEqual(m, m1, 'reference check');
+ assert.strictEqual(m1.end(), m, 'end check');
});
QUnit.test('.first', function(assert) {

0 comments on commit 229215d

Please sign in to comment.