Skip to content

Adds decrement() to ParseObject #1069

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

Merged
merged 34 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
616d6f3
added console.log
stevestencil Aug 8, 2019
4e693dd
added _batchCount and _batchIndex properties
stevestencil Aug 8, 2019
50aa5a8
test batch index and count
stevestencil Aug 9, 2019
9e51c56
Merge branch 'master' into master
stevestencil Aug 9, 2019
ffb9b11
Merge branch 'master' into master
stevestencil Aug 9, 2019
4275c29
added hint
stevestencil Dec 23, 2019
7d488b8
added hint to ParseQuery
stevestencil Jan 7, 2020
cd612b9
fixed failed tests
stevestencil Jan 7, 2020
7efb326
removed _batchIndex and _batchCount
stevestencil Jan 7, 2020
b28ac39
Merge pull request #2 from parse-community/master
stevestencil Jan 8, 2020
dcbcc4d
Merge branch 'master' into query-hint
stevestencil Jan 8, 2020
773601c
added documentation and support for chaining
stevestencil Jan 8, 2020
1827459
added documentation and tests
stevestencil Jan 8, 2020
edd9c42
Merge branch 'master' into query-hint
dplewis Jan 14, 2020
6b51be9
Merge pull request #4 from parse-community/master
stevestencil Jan 17, 2020
81088cd
added support for metadata and tags
stevestencil Jan 17, 2020
5a44266
added more docs
stevestencil Jan 17, 2020
47e462e
removed validation for values
stevestencil Jan 17, 2020
da5fa5b
added docs for beforeSaveFile and afterSaveFile
stevestencil Jan 18, 2020
312175d
updated docs
stevestencil Jan 18, 2020
483e4bc
Merge branch 'master' of https://github.com/stevestencil/Parse-SDK-JS
stevestencil Jan 18, 2020
b35b677
Merge branch 'master' of https://github.com/parse-community/Parse-SDK…
stevestencil Jan 18, 2020
720be0d
Merge branch 'query-hint'
stevestencil Jan 18, 2020
0d2df40
Merge branch 'master' into metadata-tags
dplewis Jan 19, 2020
23106c4
add getters
dplewis Jan 19, 2020
7a8b66d
Merge branch 'metadata-tags' of https://github.com/stevestencil/Parse…
dplewis Jan 19, 2020
4994edb
clean up
dplewis Jan 19, 2020
3d13de6
Merge branch 'master' of https://github.com/parse-community/Parse-SDK-JS
stevestencil Jan 19, 2020
8496b7c
added decrement function to ParseObject
stevestencil Jan 19, 2020
79a679d
Merge branch 'metadata-tags' into develop
stevestencil Jan 21, 2020
8cfcbbf
udpated scope
stevestencil Jan 21, 2020
908536c
reverted package.json
stevestencil Jan 21, 2020
2b83cf1
Merge branch 'master' into master
stevestencil Jan 21, 2020
8305b6f
Merge branch 'master' into master
dplewis Feb 7, 2020
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
18 changes: 18 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,24 @@ class ParseObject {
return this.set(attr, new IncrementOp(amount));
}

/**
* Atomically decrements the value of the given attribute the next time the
* object is saved. If no amount is specified, 1 is used by default.
*
* @param attr {String} The key.
* @param amount {Number} The amount to decrement by (optional).
* @return {(ParseObject|Boolean)}
*/
decrement(attr: string, amount?: number): ParseObject | boolean {
if (typeof amount === 'undefined') {
amount = 1;
}
if (typeof amount !== 'number') {
throw new Error('Cannot decrement by a non-numeric amount.');
}
return this.set(attr, new IncrementOp(amount * -1));
}

/**
* Atomically add an object to the end of the array associated with a given
* key.
Expand Down
44 changes: 44 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,50 @@ describe('ParseObject', () => {
expect(o2.attributes).toEqual({ age: 41 });
});


it('can decrement a field', () => {
const o = new ParseObject('Person');
o.decrement('age');
expect(o.attributes).toEqual({ age: -1 });
expect(o.op('age') instanceof IncrementOp).toBe(true);
expect(o.dirtyKeys()).toEqual(['age']);
expect(o._getSaveJSON()).toEqual({
age: { __op: 'Increment', amount: -1 }
});

o.decrement('age', 4);
expect(o.attributes).toEqual({ age: -5 });
expect(o._getSaveJSON()).toEqual({
age: { __op: 'Increment', amount: -5 }
});

expect(o.decrement.bind(o, 'age', 'four')).toThrow(
'Cannot decrement by a non-numeric amount.'
);
expect(o.decrement.bind(o, 'age', null)).toThrow(
'Cannot decrement by a non-numeric amount.'
);
expect(o.decrement.bind(o, 'age', { amount: 4 })).toThrow(
'Cannot decrement by a non-numeric amount.'
);

o.set('age', 30);
o.decrement('age');
expect(o.attributes).toEqual({ age: 29 });
expect(o._getSaveJSON()).toEqual({
age: 29
});

const o2 = new ParseObject('Person');
o2._finishFetch({
objectId: 'ABC123',
age: 40
});
expect(o2.attributes).toEqual({ age: 40 });
o2.decrement('age');
expect(o2.attributes).toEqual({ age: 39 });
});

it('can set nested field', () => {
const o = new ParseObject('Person');
o._finishFetch({
Expand Down