From 7c230f67b5f1052b1799af21da6872d1fb52fc12 Mon Sep 17 00:00:00 2001 From: Oscar Gentilezza Date: Mon, 22 Jan 2018 19:36:45 -0300 Subject: [PATCH] Add increment and decrement to instance --- src/instance.js | 60 ++++++++++++++++++ test/instance.spec.js | 144 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 203 insertions(+), 1 deletion(-) diff --git a/src/instance.js b/src/instance.js index b35df2d..85943d4 100644 --- a/src/instance.js +++ b/src/instance.js @@ -289,6 +289,66 @@ fakeModelInstance.prototype.update = function (obj) { return this.save(); }; +/** + * Increment + * @instance + * @param {String|Array|Object} fields + * @param {Object} [options] + * @param {Object} [options.by = 1] The number to increment by + * @return {Promise} + */ +fakeModelInstance.prototype.increment = function (fields, options) { + if (!Array.isArray(fields) && typeof fields === 'object') { + for (let k in fields) { + this.increment(k, { by: fields[k] }); + } + + return bluebird.resolve(this); + } + + if (typeof fields === 'string') { + fields = [fields]; + } + + let by = typeof options !== 'undefined' ? options.by || 1 : 1; + + for(let i = 0; i <= fields.length; i++) { + this._values[fields[i]] += by; + } + + return bluebird.resolve(this); +}; + +/** + * Decrement + * @instance + * @param {String|Array|Object} fields + * @param {Object} [options] + * @param {Object} [options.by = 1] The number to decrement by + * @return {Promise} + */ +fakeModelInstance.prototype.decrement = function (fields, options) { + if (!Array.isArray(fields) && typeof fields === 'object') { + for (let k in fields) { + this.decrement(k, { by: fields[k] }); + } + + return bluebird.resolve(this); + } + + if (typeof fields === 'string') { + fields = [fields]; + } + + let by = typeof options !== 'undefined' ? options.by || 1 : 1; + + for(let i = 0; i <= fields.length; i++) { + this._values[fields[i]] -= by; + } + + return bluebird.resolve(this); +}; + /** * Returns all the values in a JSON representation. * diff --git a/test/instance.spec.js b/test/instance.spec.js index 916c71b..735d31a 100644 --- a/test/instance.spec.js +++ b/test/instance.spec.js @@ -280,7 +280,149 @@ describe('Instance', function () { }).catch(done); }); }); - + + describe('#increment', () => { + it('should increment field passed by 1', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + }; + + inst.increment('foo'); + + inst._values.foo.should.equal(4); + }); + + it('should increment field passed by 2', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + }; + + inst.increment('foo', { by: 2 }); + + inst._values.foo.should.equal(5); + }); + + it('should increment fields passed by 1', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + bar: 2, + }; + + inst.increment(['foo', 'bar']); + + inst._values.foo.should.equal(4); + inst._values.bar.should.equal(3); + }); + + it('should increment fields passed by 2', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + bar: 2, + }; + + inst.increment(['foo', 'bar'], { by: 2 }); + + inst._values.foo.should.equal(5); + inst._values.bar.should.equal(4); + }); + + it('should increment fields as dicto', () => { + let inst = new Instance(); + + inst._values = { + foo: 2, + bar: 2, + }; + + inst.increment({ + foo: 2, + bar: 3, + }); + + inst._values.foo.should.equal(4); + inst._values.bar.should.equal(5); + }); + }); + + describe('#decrement', () => { + it('should decrement field passed by 1', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + }; + + inst.decrement('foo'); + + inst._values.foo.should.equal(2); + }); + + it('should decrement field passed by 2', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + }; + + inst.decrement('foo', { by: 2 }); + + inst._values.foo.should.equal(1); + }); + + it('should decrement fields passed by 1', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + bar: 2, + }; + + inst.decrement(['foo', 'bar']); + + inst._values.foo.should.equal(2); + inst._values.bar.should.equal(1); + }); + + it('should decrement fields passed by 2', () => { + let inst = new Instance(); + + inst._values = { + foo: 3, + bar: 2, + }; + + inst.decrement(['foo', 'bar'], { by: 2 }); + + inst._values.foo.should.equal(1); + inst._values.bar.should.equal(0); + }); + + it('should decrement fields as dicto', () => { + let inst = new Instance(); + + inst._values = { + foo: 2, + bar: 2, + }; + + inst.decrement({ + foo: 2, + bar: 3, + }); + + inst._values.foo.should.equal(0); + inst._values.bar.should.equal(-1); + }); + }); + describe('#toJSON', function () { it('should have the function aliased to toJson', function () { var inst = new Instance();