Skip to content

Commit 345a5e6

Browse files
Adds decrement() to ParseObject (#1069)
* added console.log * added _batchCount and _batchIndex properties * test batch index and count * added hint * added hint to ParseQuery * fixed failed tests * removed _batchIndex and _batchCount * added documentation and support for chaining * added documentation and tests * added support for metadata and tags * added more docs * removed validation for values * added docs for beforeSaveFile and afterSaveFile * updated docs * add getters * clean up * added decrement function to ParseObject * udpated scope * reverted package.json Co-authored-by: Diamond Lewis <[email protected]>
1 parent 7f6a65d commit 345a5e6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/ParseObject.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,24 @@ class ParseObject {
764764
return this.set(attr, new IncrementOp(amount));
765765
}
766766

767+
/**
768+
* Atomically decrements the value of the given attribute the next time the
769+
* object is saved. If no amount is specified, 1 is used by default.
770+
*
771+
* @param attr {String} The key.
772+
* @param amount {Number} The amount to decrement by (optional).
773+
* @return {(ParseObject|Boolean)}
774+
*/
775+
decrement(attr: string, amount?: number): ParseObject | boolean {
776+
if (typeof amount === 'undefined') {
777+
amount = 1;
778+
}
779+
if (typeof amount !== 'number') {
780+
throw new Error('Cannot decrement by a non-numeric amount.');
781+
}
782+
return this.set(attr, new IncrementOp(amount * -1));
783+
}
784+
767785
/**
768786
* Atomically add an object to the end of the array associated with a given
769787
* key.

src/__tests__/ParseObject-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,50 @@ describe('ParseObject', () => {
491491
expect(o2.attributes).toEqual({ age: 41 });
492492
});
493493

494+
495+
it('can decrement a field', () => {
496+
const o = new ParseObject('Person');
497+
o.decrement('age');
498+
expect(o.attributes).toEqual({ age: -1 });
499+
expect(o.op('age') instanceof IncrementOp).toBe(true);
500+
expect(o.dirtyKeys()).toEqual(['age']);
501+
expect(o._getSaveJSON()).toEqual({
502+
age: { __op: 'Increment', amount: -1 }
503+
});
504+
505+
o.decrement('age', 4);
506+
expect(o.attributes).toEqual({ age: -5 });
507+
expect(o._getSaveJSON()).toEqual({
508+
age: { __op: 'Increment', amount: -5 }
509+
});
510+
511+
expect(o.decrement.bind(o, 'age', 'four')).toThrow(
512+
'Cannot decrement by a non-numeric amount.'
513+
);
514+
expect(o.decrement.bind(o, 'age', null)).toThrow(
515+
'Cannot decrement by a non-numeric amount.'
516+
);
517+
expect(o.decrement.bind(o, 'age', { amount: 4 })).toThrow(
518+
'Cannot decrement by a non-numeric amount.'
519+
);
520+
521+
o.set('age', 30);
522+
o.decrement('age');
523+
expect(o.attributes).toEqual({ age: 29 });
524+
expect(o._getSaveJSON()).toEqual({
525+
age: 29
526+
});
527+
528+
const o2 = new ParseObject('Person');
529+
o2._finishFetch({
530+
objectId: 'ABC123',
531+
age: 40
532+
});
533+
expect(o2.attributes).toEqual({ age: 40 });
534+
o2.decrement('age');
535+
expect(o2.attributes).toEqual({ age: 39 });
536+
});
537+
494538
it('can set nested field', () => {
495539
const o = new ParseObject('Person');
496540
o._finishFetch({

0 commit comments

Comments
 (0)