Skip to content

Loading…

Add automatic array mapping #22

Merged
merged 2 commits into from

2 participants

@robholland

If you have a .maps syntax you'd prefer, that makes it explicit an array is expected, let me know and I'll adjust the code.

Rob Holland added some commits
@masylum masylum merged commit 6cce0b2 into masylum:master
@masylum
Owner

not sure 100% about this, but the commit is good and tested so I will release it and see how works on production :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 7, 2011
  1. Correct typo.

    Rob Holland committed
  2. Automatically map array fields.

    Rob Holland committed
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();
Something went wrong with that request. Please try again.