Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit b75ad71

Browse files
author
julian dondero
committed
use for in loop to include enumerable descriptors in relationships (getters computed)
1 parent 9529029 commit b75ad71

2 files changed

Lines changed: 4278 additions & 15 deletions

File tree

src/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,15 @@ export class JSONAPISerializer {
453453
}
454454

455455
private serializeRelationships(data: any, config: SerializerConfig) {
456-
return reduce(
457-
data,
458-
(acum, value, key) => {
459-
if (!get(config, "relationships", {})[key]) {
460-
return acum;
461-
}
456+
const serializedRelationships = {};
462457

458+
for (const property in data) {
459+
// * use for in loop to include enumerables descriptors
460+
if (get(config, "relationships", {})[property]) {
461+
const value = data[property];
463462
if (Array.isArray(value)) {
464-
acum[kebabCase(key)] = map(value, (v, k) => {
465-
const relationshipConfig = config.relationships[key](v);
463+
serializedRelationships[kebabCase(property)] = map(value, (v, k) => {
464+
const relationshipConfig = config.relationships[property](v);
466465
const entityType = get(v, "type") || relationshipConfig.type;
467466

468467
const output: any = {
@@ -475,7 +474,7 @@ export class JSONAPISerializer {
475474
return output;
476475
});
477476
} else {
478-
const relationshipConfig = config.relationships[key](value);
477+
const relationshipConfig = config.relationships[property](value);
479478

480479
const entityType = get(value, "type") || relationshipConfig.type;
481480
const output: any = {
@@ -485,13 +484,13 @@ export class JSONAPISerializer {
485484
if (value?.id) {
486485
output.id = value.id;
487486
}
488-
acum[kebabCase(key)] = isNull(value) ? null : output;
487+
serializedRelationships[kebabCase(property)] = isNull(value)
488+
? null
489+
: output;
489490
}
490-
491-
return acum;
492-
},
493-
{}
494-
);
491+
}
492+
}
493+
return serializedRelationships;
495494
}
496495

497496
private _serialize({

0 commit comments

Comments
 (0)