Skip to content

Commit

Permalink
Merge pull request #24 from icapps/bugfix/return-plain-values-in-array
Browse files Browse the repository at this point in the history
bugfix/return-plain-values-in-array
  • Loading branch information
horstenwillem committed Apr 6, 2022
2 parents 04ede32 + a2330b7 commit 7662ab9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/utils/data-construction.util.js
Expand Up @@ -38,7 +38,7 @@ function getValue(data, key, config, options) {
return config[key].apply(data, [data[key], data]);
}

// if value is a string
// if value is a string, number, boolean or date
if (isFlatValue(data[key])) {
return data[key];
}
Expand Down Expand Up @@ -68,6 +68,11 @@ function constructData(data, customConfig = { attributes: [] }, options = {}) {
};
const config = Object.assign({}, defaultConfig, customConfig);

// if value is a string, number, boolean or date
if (isFlatValue(data)) {
return data;
}

// if the dataset is an array
if (isArray(data)) {
return data.map(item => constructData(item, config, options));
Expand Down
18 changes: 18 additions & 0 deletions tests/serializer.test.js
Expand Up @@ -63,4 +63,22 @@ describe('Serializer multiple resource', () => {
expect(meta.totalCount).toEqual(99);
expect(meta.planet).toEqual('earth');
});

test('should return plain values', () => {
const date = new Date();
const rawData = [
{ firstName: 'John', lastName: 'Doe', plainValues: [1, 'plain', true] },
{ firstName: 'Jane', lastName: 'Doe', plainValues: [8, date, false] },
];

const userSerializer = new Serializer('user', {
attributes: ['firstName', 'lastName', 'plainValues'],
});

const result = userSerializer.serialize(rawData);

const { data } = result;
expect(data[0].plainValues).toEqual([1, 'plain', true]);
expect(data[1].plainValues).toEqual([8, date, false]);
});
});

0 comments on commit 7662ab9

Please sign in to comment.