Skip to content

Add methods addAll, addAllUnique and removeAll #459

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 5 commits into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,14 @@ describe('Parse Object', () => {

child.save().then(() => {
parent.add('children', child);
parent.addAll('children', [child, child]);
return parent.save();
}).then(() => {
let query = new Parse.Query('Person');
return query.get(parent.id);
}).then((p) => {
assert.equal(p.get('children')[0].id, child.id);
assert.equal(p.get('children').length, 3);
done();
});
});
Expand Down
36 changes: 36 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,17 @@ export default class ParseObject {
return this.set(attr, new AddOp([item]));
}

/**
* Atomically add the objects to the end of the array associated with a given
* key.
* @method addAll
* @param attr {String} The key.
* @param items {[]} The items to add.
*/
addAll(attr: string, items: Array<mixed>): ParseObject | boolean {
return this.set(attr, new AddOp(items));
}

/**
* Atomically add an object to the array associated with a given key, only
* if it is not already present in the array. The position of the insert is
Expand All @@ -748,6 +759,19 @@ export default class ParseObject {
return this.set(attr, new AddUniqueOp([item]));
}

/**
* Atomically add the objects to the array associated with a given key, only
* if it is not already present in the array. The position of the insert is
* not guaranteed.
*
* @method addAllUnique
* @param attr {String} The key.
* @param items {[]} The objects to add.
*/
addAllUnique(attr: string, items: Array<mixed>): ParseObject | boolean {
return this.set(attr, new AddUniqueOp(items));
}

/**
* Atomically remove all instances of an object from the array associated
* with a given key.
Expand All @@ -760,6 +784,18 @@ export default class ParseObject {
return this.set(attr, new RemoveOp([item]));
}

/**
* Atomically remove all instances of the objects from the array associated
* with a given key.
*
* @method removeAll
* @param attr {String} The key.
* @param items {[]} The object to remove.
*/
removeAll(attr: string, items: Array<mixed>): ParseObject | boolean {
return this.set(attr, new RemoveOp(items));
}

/**
* Returns an instance of a subclass of Parse.Op describing what kind of
* modification has been performed on this field since the last time it was
Expand Down
17 changes: 9 additions & 8 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,13 @@ describe('ParseObject', () => {
it('can add elements to an array field', () => {
var o = new ParseObject('Schedule');
o.add('available', 'Monday');
o.add('available', 'Wednesday');
expect(o.get('available')).toEqual(['Monday', 'Wednesday']);
o.addAll('available', ['Wednesday', 'Friday']);
expect(o.get('available')).toEqual(['Monday', 'Wednesday', 'Friday']);

o.set('colors', ['red', 'green']);
o.add('colors', 'blue');
expect(o.get('colors')).toEqual(['red', 'green', 'blue']);
o.set('colors', ['red']);
o.add('colors', 'green');
o.addAll('colors', ['blue', 'alpha']);
expect(o.get('colors')).toEqual(['red', 'green', 'blue', 'alpha']);

o._handleSaveResponse({
objectId: 'S1',
Expand All @@ -419,15 +420,15 @@ describe('ParseObject', () => {
});

o.addUnique('available', 'Thursday');
o.addUnique('available', 'Monday');
o.addAllUnique('available', ['Monday']);
expect(o.get('available')).toEqual(['Monday', 'Wednesday', 'Thursday']);
});

it('can remove elements from an array field', () => {
var o = new ParseObject('Schedule');
o.set('available', ['Monday', 'Tuesday']);
o.remove('available', 'Tuesday');
o.remove('available', 'Saturday');
o.removeAll('available', ['Saturday']);
expect(o.get('available')).toEqual(['Monday']);

o._handleSaveResponse({
Expand All @@ -436,7 +437,7 @@ describe('ParseObject', () => {
});

o.remove('available', 'Monday');
o.remove('available', 'Tuesday');
o.removeAll('available', ['Tuesday']);
expect(o.get('available')).toEqual([]);
});

Expand Down