Skip to content

Commit

Permalink
Adding omitValues
Browse files Browse the repository at this point in the history
  • Loading branch information
edudavid committed Dec 26, 2018
1 parent 0a09e11 commit 48c64d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ npm i --save json-map-transform

**Transform:** Is a callback that can be used to transform the current property. It receives two parameters: (property, originalObject).

**Default:** In case a path does not exists in the input object, the default value will be uses.
**Default:** In case a path does not exists in the input object or the value returned by transform is undefined, the default value will be used.

**OmitValues:** Sometimes it is necessary to ignore values on the transformation (ex: undefined, empty string or other values depending on a business rule). With the omitValues, it is possible to define an array of values that should be ignored on the transformation. The output object will not the associated key.


Template example:

Expand All @@ -41,6 +44,7 @@ const template = {
},
label: {
path: ['category', 'categories'],
omitValues: ['', undefined, 'ERROR']
},
vendor: {
path: 'meta.vendor',
Expand Down
9 changes: 7 additions & 2 deletions lib/jsonMapTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const has = require('./utils/objectHandler').has;
const setObjectValue = (template, key, value, json, transformedJson) => {
const defaultValue = template[key].default;
const transform = template[key].transform;
const transformedValue = transform ? transform(value, json) : value;
const omitValues = template[key].omitValues || [];
const transformedValue = (transform ? transform(value, json) : value) || defaultValue;

return set(Object.assign({}, transformedJson), key, transformedValue || defaultValue);
if(omitValues.indexOf(transformedValue) == -1) {
return set(Object.assign({}, transformedJson), key, transformedValue);
}

return transformedJson;
};

const transformJson = (json, template, afterTransform) => {
Expand Down
28 changes: 28 additions & 0 deletions test/jsonMapTransform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ test('ignores invalid path', () => {
expect(convertedJson.vendor).toBe('Author name');
});

test('Omits undefined value', () => {
const convertedJson = transform(product1, Object.assign({}, template, {
wrongProperty: {
path: 'unexistingPath',
omitValues: [undefined],
}
}));

expect(convertedJson).not.toHaveProperty('wrongProperty');
expect(convertedJson.title).toBe('HELLO WORLD');
expect(convertedJson.label).toBe('books');
expect(convertedJson.vendor).toBe('Author name');
});

test('Omits undefined value returned by transform', () => {
const convertedJson = transform(product1, Object.assign({}, template, {
title: {
path: 'name',
omitValues: ['ERROR', '', undefined],
transform: () => 'ERROR'
}
}));

expect(convertedJson).not.toHaveProperty('title');
expect(convertedJson.label).toBe('books');
expect(convertedJson.vendor).toBe('Author name');
});

test('executes transformation callback for a single object', () => {
const convertedJson = transform(product1, template, afterTransform);

Expand Down

0 comments on commit 48c64d6

Please sign in to comment.