Skip to content

Commit

Permalink
fix _convertCase issues
Browse files Browse the repository at this point in the history
closes #80
  • Loading branch information
danivek committed Aug 7, 2019
1 parent 5163aed commit 2406a54
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 30 deletions.
73 changes: 43 additions & 30 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,38 +764,51 @@ module.exports = class JSONAPISerializer {
* @returns {object} Object with it's keys converted as per the convertCaseOptions
*/
_convertCase(data, convertCaseOptions) {
let converted;
if (typeof data === 'object') {
converted = transform(
data,
(result, value, key) => {
if (value && typeof value === 'object') {
result[this._convertCase(key, convertCaseOptions)] = this._convertCase(
value,
convertCaseOptions
);
} else {
result[this._convertCase(key, convertCaseOptions)] = value;
}
return result;
},
{}
);
} else {
switch (convertCaseOptions) {
case 'snake_case':
converted = toSnakeCase(data);
break;
case 'kebab-case':
converted = toKebabCase(data);
break;
case 'camelCase':
converted = toCamelCase(data);
break;
default: // Do nothing
if (data !== undefined && data !== null) {
if (Array.isArray(data)) {
return data.map(item => this._convertCase(item, convertCaseOptions));
}

if (typeof data === 'object') {
return transform(
data,
(result, value, key) => {
let converted;
if (value && Array.isArray(value)) {
converted = value.map(item => this._convertCase(item, convertCaseOptions));
} else if (value && typeof value === 'object') {
converted = this._convertCase(value, convertCaseOptions);
} else {
converted = value;
}

result[this._convertCase(key, convertCaseOptions)] = converted;
return result;
},
{}
);
}

if (typeof data === 'string') {
let converted;

switch (convertCaseOptions) {
case 'snake_case':
converted = toSnakeCase(data);
break;
case 'kebab-case':
converted = toKebabCase(data);
break;
case 'camelCase':
converted = toCamelCase(data);
break;
default: // Do nothing
}

return converted;
}
}

return converted;
return data;
}
};
46 changes: 46 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2062,4 +2062,50 @@ describe('JSONAPISerializer', function() {
done();
});
})

describe('convertCase', function() {
it('should convert an array of object to kebab-case', function() {
const jsonapiSerializer = new JSONAPISerializer();
const converted = jsonapiSerializer._convertCase(
[{
arrayOfObject: [
{
firstProperty: 'test',
secondProperty: null,
thirdProperty: 0
}
],
arrayOfNumber: [1, 2, 3, 4, 5]
}],
'kebab-case'
);

expect(converted).to.deep.equal([{
'array-of-object': [{ 'first-property': 'test', 'second-property': null, 'third-property': 0 }],
'array-of-number': [1, 2, 3, 4, 5]
}]);
});

it('should convert an object to kebab-case', function() {
const jsonapiSerializer = new JSONAPISerializer();
const converted = jsonapiSerializer._convertCase(
{
arrayOfObject: [
{
firstProperty: 'test',
secondProperty: null,
thirdProperty: 0
}
],
arrayOfNumber: [1, 2, 3, 4, 5]
},
'kebab-case'
);

expect(converted).to.deep.equal({
'array-of-object': [{ 'first-property': 'test', 'second-property': null, 'third-property': 0 }],
'array-of-number': [1, 2, 3, 4, 5]
});
});
});
});

0 comments on commit 2406a54

Please sign in to comment.