Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add increment and decrement to instance #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<instance>}
*/
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<instance>}
*/
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.
*
Expand Down
144 changes: 143 additions & 1 deletion test/instance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down