Skip to content

Commit

Permalink
fix(map): map should not mess with object keys
Browse files Browse the repository at this point in the history
  • Loading branch information
GertSallaerts committed Dec 2, 2022
1 parent b44d2d2 commit 8184a10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
14 changes: 4 additions & 10 deletions src/transformers/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const _transform = require('../lib/transform');
const _merge = require('lodash/merge');
const _isArray = require('lodash/isArray');

function MapTransformer(callback, parse) {
Expand All @@ -18,29 +17,24 @@ MapTransformer.prototype._createParse = function(key) {
const parse = this._parse;
const cache = this._cache;

if (!(key in cache)) {
cache[key] = this._callback(parse().select(key));
}
if (!(key in cache))
cache[key] = this._callback(parse());

return cache[key];
};

MapTransformer.prototype.parse = function(source, instance, root) {
const accumulator = _isArray(source) ? [] : {};
return _transform(source, (result, value, key) => {
result[key] = this._createParse(key).parse(source, instance, root);
result[key] = this._createParse(key).parse(value, instance, root);
}, accumulator);
};

MapTransformer.prototype.reverse = function(source, instance, root) {
const isArray = _isArray(source);
const accumulator = isArray ? [] : {};
return _transform(source, (result, value, key) => {
const reverse = this._createParse(key).reverse(source[key], instance, root);
if (isArray)
result[key] = reverse[key];
else
_merge(result, reverse);
result[key] = this._createParse(key).reverse(value, instance, root);
}, accumulator);
};

Expand Down
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mocha": true
},
"rules": {
"semi": 0
"semi": 0,
"strict": 0
}
}
42 changes: 29 additions & 13 deletions test/transformers/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('map', function() {

describe('#constructor', function() {
it('Should create an instance without new keyword when attached', function() {
const callback = function() {};

const obj = {
map: Map,
transform: instance => {
Expand All @@ -20,7 +22,6 @@ describe('map', function() {
}
};

const callback = function() {};
obj.map(callback);
})
})
Expand Down Expand Up @@ -66,13 +67,9 @@ describe('map', function() {
})

it('Should parse an array correctly', function() {
const callback = function(p) {
assert(p instanceof Parse);
assert(p._chain[0]);
assert.notEqual(typeof p._chain[0]._path, 'undefined');

const callback = function() {
return {
parse: (v) => '_' + v[p._chain[0]._path]
parse: (v) => '_' + v
};
}
const instance = new Map(callback);
Expand Down Expand Up @@ -107,24 +104,43 @@ describe('map', function() {
const callback = function(p) {
assert(p instanceof Parse);
return {
reverse: (v) => ({ test1: v })
reverse: () => 'abcd'
};
}
const instance = new Map(callback);
const result = instance.reverse({ test1: 'test', test2: 'abcd' });

assert.deepEqual(result, { test1: 'abcd' });
assert.deepEqual(result, { test1: 'abcd', test2: 'abcd' });
})

it('Should reverse an object with dots in keys correctly', function() {
const callback = function(p) {
assert(p instanceof Parse);

return {
reverse: (v) => '_' + v
};
}

const instance = new Map(callback);

const result = instance.reverse({
test1: 'test',
'test1.subTest': 'subTest'
});

assert.deepEqual(result, {
test1: '_test',
'test1.subTest': '_subTest'
});
})

it('Should detect object or array', function() {
const callback = function(p) {
assert(p instanceof Parse);
assert(p._chain[0]);
assert.notEqual(typeof p._chain[0]._path, 'undefined');

const id = p._chain[0]._path;
return {
reverse: (v) => ({ [id]: '_' + v })
reverse: (v) => '_' + v
};
}
const instance = new Map(callback);
Expand Down

0 comments on commit 8184a10

Please sign in to comment.