Skip to content

Commit 9533ec2

Browse files
leeyehwangxiao
authored andcommitted
fix(Object): add support to object.add(key, array) (#298)
Ensure the second param of AV.Object#add, AV.Object#addUnique, AV.Object#remove to be an Array.
1 parent bc07d42 commit 9533ec2

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ _.extend(AVError, {
103103
/**
104104
* Error code indicating an invalid channel name. A channel name is either
105105
* an empty string (the broadcast channel) or contains only a-zA-Z0-9_
106-
* characters and starts with a letter.
106+
* characters.
107107
* @constant
108108
*/
109109
INVALID_CHANNEL_NAME: 112,

src/object.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ module.exports = function(AV) {
715715
* @param item {} The item to add.
716716
*/
717717
add: function(attr, item) {
718-
return this.set(attr, new AV.Op.Add([item]));
718+
return this.set(attr, new AV.Op.Add(utils.ensureArray(item)));
719719
},
720720

721721
/**
@@ -727,7 +727,7 @@ module.exports = function(AV) {
727727
* @param item {} The object to add.
728728
*/
729729
addUnique: function(attr, item) {
730-
return this.set(attr, new AV.Op.AddUnique([item]));
730+
return this.set(attr, new AV.Op.AddUnique(utils.ensureArray(item)));
731731
},
732732

733733
/**
@@ -738,7 +738,7 @@ module.exports = function(AV) {
738738
* @param item {} The object to remove.
739739
*/
740740
remove: function(attr, item) {
741-
return this.set(attr, new AV.Op.Remove([item]));
741+
return this.set(attr, new AV.Op.Remove(utils.ensureArray(item)));
742742
},
743743

744744
/**

src/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ const request = require('./request');
99
// Helper function to check null or undefined.
1010
const isNullOrUndefined = (x) => _.isNull(x) || _.isUndefined(x);
1111

12+
const ensureArray = target => {
13+
if (_.isArray(target)) {
14+
return target;
15+
}
16+
if (target === undefined || target === null) {
17+
return [];
18+
}
19+
return [target];
20+
};
21+
1222
const init = (AV) => {
1323
// 挂载一些配置
1424
const AVConfig = AV._config;
@@ -551,4 +561,5 @@ const init = (AV) => {
551561
module.exports = {
552562
init,
553563
isNullOrUndefined,
564+
ensureArray,
554565
};

test/object.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,47 @@ describe('Objects', function(){
156156
});
157157
});
158158

159+
describe('Array Data', function () {
160+
let post;
161+
beforeEach(function() {
162+
post = new Post({
163+
data: [1, 2],
164+
});
165+
return post.save();
166+
});
167+
afterEach(function() {
168+
return post.destroy();
169+
});
159170

171+
it('add', function() {
172+
return post.add('data', 2).save().then(function() {
173+
return post.fetch();
174+
}).then(function(post) {
175+
expect(post.get('data')).to.be.eql([1,2,2]);
176+
});
177+
});
178+
it('addUnique', function() {
179+
return post.addUnique('data', 2).save().then(function() {
180+
return post.fetch();
181+
}).then(function(post) {
182+
expect(post.get('data')).to.be.eql([1,2]);
183+
});
184+
});
185+
it('remove', function() {
186+
return post.remove('data', 2).save().then(function() {
187+
return post.fetch();
188+
}).then(function(post) {
189+
expect(post.get('data')).to.be.eql([1]);
190+
});
191+
});
192+
it('accept array param', function() {
193+
return post.remove('data', [2]).save().then(function() {
194+
return post.fetch();
195+
}).then(function(post) {
196+
expect(post.get('data')).to.be.eql([1]);
197+
});
198+
});
199+
});
160200

161201
describe("Relational Data",function(){
162202

0 commit comments

Comments
 (0)