Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #22 from robholland/master

Add automatic array mapping
  • Loading branch information...
commit 6cce0b25a60289b1e83e299d8878e82b7b32ec35 2 parents 64b87d0 + 4041c0b
@masylum authored
Showing with 22 additions and 8 deletions.
  1. +8 −2 lib/helpers/mapper.js
  2. +14 −6 test/helpers/mapper_test.js
View
10 lib/helpers/mapper.js
@@ -52,13 +52,19 @@ MAPPER.filterUpdate = function (maps, arg) {
} else {
is_matching = isMatching(maps, attr, level);
if (is_matching === 2) {
- el[attr] = getObject(maps, attr)(el[attr]);
+ if (Array.isArray(el[attr])) {
+ el[attr].forEach(function(value, i) {
+ el[attr][i] = getObject(maps, attr)(el[attr][i]);
+ });
+ } else {
+ el[attr] = getObject(maps, attr)(el[attr]);
+ }
} else {
if (is_matching === 1 && typeof el[attr] === 'object') {
filter(maps[attr], el[attr], level + 1);
}
}
- }
+ }
}
}
}
View
20 test/helpers/mapper_test.js
@@ -1,34 +1,35 @@
var testosterone = require('testosterone')({post: 3000, sync: true, title: 'mongolia/helpers/mapper_test.js'}),
assert = testosterone.assert,
- Namespacer = require('./../../lib/helpers/mapper');
+ Mapper = require('./../../lib/helpers/mapper');
testosterone
.before(function () {
})
.add('`filterUpdate` should filter documents before being inserted or updated', function () {
- var test = Namespacer.filterUpdate
+ var test = Mapper.filterUpdate
, arg
, toUpper = function (val) {
return val.toUpperCase();
}
- , update = {zemba: 'FOO', nested: {id: 123, name: '300'}, foo: true}
+ , update = {zemba: 'FOO', nested: {id: 123, name: '300'}, list: [1,2,3], foo: true}
, maps = { zemba: toUpper
, nested: { id: Number
, name: String
}
+ , list: Number
, foo: Boolean
};
// document
- arg = {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'};
+ arg = {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'};
test(maps, arg);
assert.deepEqual(arg, update);
assert.equal(typeof arg.nested.id, typeof update.nested.id);
assert.equal(typeof arg.nested.name, typeof update.nested.name);
// array
- arg = [{zemba: 'fleiba'}, {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'}];
+ arg = [{zemba: 'fleiba'}, {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'}];
test(maps, arg);
assert.deepEqual(arg[0], {zemba: 'FLEIBA'});
assert.deepEqual(arg[1], update);
@@ -36,7 +37,7 @@ testosterone
assert.equal(typeof arg[1].nested.name, typeof update.nested.name);
// with sepcial ops
- arg = {'$set': {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'}};
+ arg = {'$set': {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'}};
test(maps, arg);
assert.deepEqual(arg, {'$set': update});
assert.equal(typeof arg.$set.nested.id, typeof update.nested.id);
@@ -47,6 +48,13 @@ testosterone
test(maps, arg);
assert.equal(typeof arg.$set['nested.id'], typeof update.nested.id);
assert.equal(typeof arg.$set['nested.name'], typeof update.nested.name);
+
+ // array values
+ arg = {list: ['1', '2', 3]};
+ test(maps, arg);
+ assert.equal(typeof arg.list[0], typeof update.list[0]);
+ assert.equal(typeof arg.list[1], typeof update.list[1]);
+ assert.equal(typeof arg.list[2], typeof update.list[2]);
})
.run();
Please sign in to comment.
Something went wrong with that request. Please try again.