Skip to content

Commit

Permalink
Merge pull request #16 from klerick/fix-transform-row-data
Browse files Browse the repository at this point in the history
fix(json-api-nestjs): fix check if field exist and camelcase transform
  • Loading branch information
klerick authored Dec 13, 2022
2 parents 9f3ecb3 + 7606844 commit 823b868
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions libs/json-api-nestjs/src/lib/helper/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ describe('Test utils', () => {
const result = snakeToCamel('test_test');
const result1 = snakeToCamel('test-test');
const result2 = snakeToCamel('testTest');
const result3 = snakeToCamel('event_incident_typeFK');
expect(result).toBe('testTest');
expect(result1).toBe('testTest');
expect(result2).toBe('testTest');
expect(result3).toBe('eventIncidentTypeFK');
});

it('isString', () => {
Expand Down
8 changes: 3 additions & 5 deletions libs/json-api-nestjs/src/lib/helper/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ export function snakeToCamel(str: string): string {
if (!str.match(/[\s_-]/g)) {
return str;
}
return str
.toLowerCase()
.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace('-', '').replace('_', '')
);
return str.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace('-', '').replace('_', '')
);
}

export function camelToKebab(str: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class TransformMixinService<T> {
this.commonColumnsForRelation.has(tableName) &&
this.commonColumnsForRelation.get(tableName).has(fieldName)
) {
if (!currentItem[field]) {
if (!(field in currentItem)) {
continue;
}
relationObject[tableName] = relationObject[tableName] || {};
Expand All @@ -141,7 +141,9 @@ export class TransformMixinService<T> {
val
);
if (Array.isArray(acum[key])) {
acum[key].push(plainObject);
if (plainObject[this.relationPrimaryField.get(key)] !== null) {
acum[key].push(plainObject);
}
} else {
acum[key] = plainObject;
}
Expand Down

0 comments on commit 823b868

Please sign in to comment.