@@ -303,47 +303,49 @@ module.exports = {
303
303
'use strict' ;
304
304
var isPlainObject = require ( 'lodash/isPlainObject' ) ;
305
305
var isFunction = require ( 'lodash/isFunction' ) ;
306
- var _find = require ( 'lodash/find' ) ;
307
306
var _merge = require ( 'lodash/merge' ) ;
308
307
var _identity = require ( 'lodash/identity' ) ;
309
308
var _transform = require ( 'lodash/transform' ) ;
310
309
var _mapValues = require ( 'lodash/mapValues' ) ;
311
310
var _mapKeys = require ( 'lodash/mapKeys' ) ;
312
311
var _pick = require ( 'lodash/pick' ) ;
313
312
var _pickBy = require ( 'lodash/pickBy' ) ;
314
- var _keys = require ( 'lodash/keys' ) ;
315
313
var _each = require ( 'lodash/each' ) ;
316
314
var _isNil = require ( 'lodash/isNil' ) ;
317
315
var Inflector = require ( './inflector' ) ;
318
316
319
- module . exports = function ( collectionName , record , payload , opts ) {
317
+ module . exports = function ( collectionName , record , payload , opts , includedSet ) {
320
318
function isComplexType ( obj ) {
321
319
return Array . isArray ( obj ) || isPlainObject ( obj ) ;
322
320
}
323
321
322
+ function transformFunction ( result , value , key ) {
323
+ if ( isComplexType ( value ) ) {
324
+ result [ keyForAttribute ( key ) ] = keyForAttribute ( value ) ;
325
+ } else {
326
+ result [ keyForAttribute ( key ) ] = value ;
327
+ }
328
+ }
329
+
330
+ function attributeFunction ( attr ) {
331
+ if ( isComplexType ( attr ) ) {
332
+ return keyForAttribute ( attr ) ;
333
+ } else {
334
+ return attr ;
335
+ }
336
+ }
337
+
338
+ const keyForAttributeFunction = isFunction ( opts . keyForAttribute )
339
+ ? opts . keyForAttribute
340
+ : ( attribute ) => Inflector . caserize ( attribute , opts ) ;
341
+
324
342
function keyForAttribute ( attribute ) {
325
343
if ( isPlainObject ( attribute ) ) {
326
- return _transform ( attribute , function ( result , value , key ) {
327
- if ( isComplexType ( value ) ) {
328
- result [ keyForAttribute ( key ) ] = keyForAttribute ( value ) ;
329
- } else {
330
- result [ keyForAttribute ( key ) ] = value ;
331
- }
332
- } ) ;
344
+ return _transform ( attribute , transformFunction ) ;
333
345
} else if ( Array . isArray ( attribute ) ) {
334
- return attribute . map ( function ( attr ) {
335
- if ( isComplexType ( attr ) ) {
336
- return keyForAttribute ( attr ) ;
337
- } else {
338
- return attr ;
339
- }
340
- } ) ;
346
+ return attribute . map ( attributeFunction ) ;
341
347
} else {
342
- if ( isFunction ( opts . keyForAttribute ) ) {
343
- return opts . keyForAttribute ( attribute ) ;
344
- } else {
345
- return Inflector . caserize ( attribute , opts ) ;
346
- }
348
+ return keyForAttributeFunction ( attribute ) ;
347
349
}
348
350
}
349
351
@@ -411,45 +413,46 @@ module.exports = function (collectionName, record, payload, opts) {
411
413
}
412
414
}
413
415
416
+ function pickFunction ( value , key ) {
417
+ return keyForAttribute ( key ) ;
418
+ }
419
+
414
420
function pick ( obj , attributes ) {
415
- return _mapKeys ( _pick ( obj , attributes ) , function ( value , key ) {
416
- return keyForAttribute ( key ) ;
417
- } ) ;
421
+ return _mapKeys ( _pick ( obj , attributes ) , pickFunction ) ;
418
422
}
419
423
420
- function isCompoundDocumentIncluded ( included , item ) {
421
- return _find ( payload . included , { id : item . id , type : item . type } ) ;
424
+ function isCompoundDocumentIncluded ( setKey ) {
425
+ return includedSet [ setKey ] ;
422
426
}
423
427
424
- function pushToIncluded ( dest , include ) {
425
- var included = isCompoundDocumentIncluded ( dest , include ) ;
428
+ function pushToIncluded ( payload , item ) {
429
+ const setKey = `${ item . type } -${ item . id } ` ;
430
+ var included = isCompoundDocumentIncluded ( setKey ) ;
426
431
if ( included ) {
427
432
// Merge relationships
428
433
included . relationships = _merge ( included . relationships ,
429
- _pickBy ( include . relationships , _identity ) ) ;
434
+ _pickBy ( item . relationships , _identity ) ) ;
430
435
431
436
// Merge attributes
432
437
included . attributes = _merge ( included . attributes ,
433
- _pickBy ( include . attributes , _identity ) ) ;
438
+ _pickBy ( item . attributes , _identity ) ) ;
434
439
} else {
435
- if ( ! dest . included ) { dest . included = [ ] ; }
436
- dest . included . push ( include ) ;
440
+ if ( ! payload . included ) { payload . included = [ ] ; }
441
+ includedSet [ setKey ] = item ;
442
+ payload . included . push ( item ) ;
437
443
}
438
444
}
439
445
440
446
this . serialize = function ( dest , current , attribute , opts ) {
441
- var that = this ;
442
447
var data = null ;
443
448
444
449
if ( opts && opts . ref ) {
445
450
if ( ! dest . relationships ) { dest . relationships = { } ; }
446
451
447
452
if ( Array . isArray ( current [ attribute ] ) ) {
448
- data = current [ attribute ] . map ( function ( item ) {
449
- return that . serializeRef ( item , current , attribute , opts ) ;
450
- } ) ;
453
+ data = current [ attribute ] . map ( ( item ) => this . serializeRef ( item , current , attribute , opts ) ) ;
451
454
} else {
452
- data = that . serializeRef ( current [ attribute ] , current , attribute ,
455
+ data = this . serializeRef ( current [ attribute ] , current , attribute ,
453
456
opts ) ;
454
457
}
455
458
@@ -472,16 +475,14 @@ module.exports = function (collectionName, record, payload, opts) {
472
475
} else {
473
476
if ( Array . isArray ( current [ attribute ] ) ) {
474
477
if ( current [ attribute ] . length && isPlainObject ( current [ attribute ] [ 0 ] ) ) {
475
- data = current [ attribute ] . map ( function ( item ) {
476
- return that . serializeNested ( item , current , attribute , opts ) ;
477
- } ) ;
478
+ data = current [ attribute ] . map ( ( item ) => this . serializeNested ( item , current , attribute , opts ) ) ;
478
479
} else {
479
480
data = current [ attribute ] ;
480
481
}
481
482
482
483
dest . attributes [ keyForAttribute ( attribute ) ] = data ;
483
484
} else if ( isPlainObject ( current [ attribute ] ) ) {
484
- data = that . serializeNested ( current [ attribute ] , current , attribute , opts ) ;
485
+ data = this . serializeNested ( current [ attribute ] , current , attribute , opts ) ;
485
486
dest . attributes [ keyForAttribute ( attribute ) ] = data ;
486
487
} else {
487
488
dest . attributes [ keyForAttribute ( attribute ) ] = current [ attribute ] ;
@@ -490,7 +491,6 @@ module.exports = function (collectionName, record, payload, opts) {
490
491
} ;
491
492
492
493
this . serializeRef = function ( dest , current , attribute , opts ) {
493
- var that = this ;
494
494
var id = getRef ( current , dest , opts ) ;
495
495
var type = getType ( attribute , dest ) ;
496
496
@@ -517,11 +517,12 @@ module.exports = function (collectionName, record, payload, opts) {
517
517
var included = { type : type , id : id } ;
518
518
if ( includedAttrs ) { included . attributes = pick ( dest , includedAttrs ) ; }
519
519
520
- relationships . forEach ( function ( relationship ) {
520
+ const relationShipsFunction = ( relationship ) => {
521
521
if ( dest && ( isComplexType ( dest [ relationship ] ) || dest [ relationship ] === null ) ) {
522
- that . serialize ( included , dest , relationship , opts [ relationship ] ) ;
522
+ this . serialize ( included , dest , relationship , opts [ relationship ] ) ;
523
523
}
524
- } ) ;
524
+ }
525
+ relationships . forEach ( relationShipsFunction ) ;
525
526
526
527
if ( includedAttrs . length &&
527
528
( opts . included === undefined || opts . included ) ) {
@@ -536,8 +537,6 @@ module.exports = function (collectionName, record, payload, opts) {
536
537
} ;
537
538
538
539
this . serializeNested = function ( dest , current , attribute , opts ) {
539
- var that = this ;
540
-
541
540
var embeds = [ ] ;
542
541
var attributes = [ ] ;
543
542
@@ -553,11 +552,12 @@ module.exports = function (collectionName, record, payload, opts) {
553
552
554
553
if ( attributes ) { ret . attributes = pick ( dest , attributes ) ; }
555
554
556
- embeds . forEach ( function ( embed ) {
555
+ const embedsFunction = ( embed ) => {
557
556
if ( isComplexType ( dest [ embed ] ) ) {
558
- that . serialize ( ret , dest , embed , opts [ embed ] ) ;
557
+ this . serialize ( ret , dest , embed , opts [ embed ] ) ;
559
558
}
560
- } ) ;
559
+ }
560
+ embeds . forEach ( embedsFunction ) ;
561
561
} else {
562
562
ret . attributes = dest ;
563
563
}
@@ -566,8 +566,6 @@ module.exports = function (collectionName, record, payload, opts) {
566
566
} ;
567
567
568
568
this . perform = function ( ) {
569
- var that = this ;
570
-
571
569
if ( record === null ) {
572
570
return null ;
573
571
}
@@ -591,7 +589,7 @@ module.exports = function (collectionName, record, payload, opts) {
591
589
data . meta = getMeta ( record , opts . dataMeta ) ;
592
590
}
593
591
594
- _each ( opts . attributes , function ( attribute ) {
592
+ _each ( opts . attributes , ( attribute ) => {
595
593
var splittedAttributes = attribute . split ( ':' ) ;
596
594
597
595
if ( opts [ attribute ] && ! record [ attribute ] && opts [ attribute ] . nullIfMissing ) {
@@ -607,15 +605,15 @@ module.exports = function (collectionName, record, payload, opts) {
607
605
attributeMap = splittedAttributes [ 1 ] ;
608
606
}
609
607
610
- that . serialize ( data , record , attribute , opts [ attributeMap ] ) ;
608
+ this . serialize ( data , record , attribute , opts [ attributeMap ] ) ;
611
609
}
612
610
} ) ;
613
611
614
612
return data ;
615
613
} ;
616
614
} ;
617
615
618
- } , { "./inflector" :5 , "lodash/each" :156 , "lodash/find" : 159 , "lodash/ identity" :165 , "lodash/isFunction" :171 , "lodash/isNil" :173 , "lodash/isPlainObject" :176 , "lodash/keys" : 179 , "lodash/mapKeys" :181 , "lodash/mapValues" :182 , "lodash/merge" :184 , "lodash/pick" :185 , "lodash/pickBy" :186 , "lodash/transform" :195 } ] , 7 :[ function ( require , module , exports ) {
616
+ } , { "./inflector" :5 , "lodash/each" :156 , "lodash/identity" :165 , "lodash/isFunction" :171 , "lodash/isNil" :173 , "lodash/isPlainObject" :176 , "lodash/mapKeys" :181 , "lodash/mapValues" :182 , "lodash/merge" :184 , "lodash/pick" :185 , "lodash/pickBy" :186 , "lodash/transform" :195 } ] , 7 :[ function ( require , module , exports ) {
619
617
'use strict' ;
620
618
var isFunction = require ( 'lodash/isFunction' ) ;
621
619
var _mapValues = require ( 'lodash/mapValues' ) ;
@@ -625,23 +623,26 @@ module.exports = function (collectionName, records, opts) {
625
623
this . serialize = function ( records ) {
626
624
var that = this ;
627
625
var payload = { } ;
626
+ const includedSet = new Set ( ) ;
627
+
628
+ function getLinksFunction ( value ) {
629
+ if ( isFunction ( value ) ) {
630
+ return value ( records ) ;
631
+ } else {
632
+ return value ;
633
+ }
634
+ }
628
635
629
636
function getLinks ( links ) {
630
- return _mapValues ( links , function ( value ) {
631
- if ( isFunction ( value ) ) {
632
- return value ( records ) ;
633
- } else {
634
- return value ;
635
- }
636
- } ) ;
637
+ return _mapValues ( links , getLinksFunction ) ;
637
638
}
638
639
639
640
function collection ( ) {
640
641
payload . data = [ ] ;
641
642
642
643
records . forEach ( function ( record ) {
643
644
var serializerUtils = new SerializerUtils ( that . collectionName , record ,
644
- payload , that . opts ) ;
645
+ payload , that . opts , includedSet ) ;
645
646
payload . data . push ( serializerUtils . perform ( ) ) ;
646
647
} ) ;
647
648
@@ -650,7 +651,7 @@ module.exports = function (collectionName, records, opts) {
650
651
651
652
function resource ( ) {
652
653
payload . data = new SerializerUtils ( that . collectionName , records , payload ,
653
- that . opts ) . perform ( records ) ;
654
+ that . opts , includedSet ) . perform ( records ) ;
654
655
655
656
return payload ;
656
657
}
0 commit comments