Skip to content

Commit b801aef

Browse files
committed
Update the browserify
1 parent 11769be commit b801aef

File tree

1 file changed

+66
-65
lines changed

1 file changed

+66
-65
lines changed

browserify/jsonapi-serializer.js

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -303,47 +303,49 @@ module.exports = {
303303
'use strict';
304304
var isPlainObject = require('lodash/isPlainObject');
305305
var isFunction = require('lodash/isFunction');
306-
var _find = require('lodash/find');
307306
var _merge = require('lodash/merge');
308307
var _identity = require('lodash/identity');
309308
var _transform = require('lodash/transform');
310309
var _mapValues = require('lodash/mapValues');
311310
var _mapKeys = require('lodash/mapKeys');
312311
var _pick = require('lodash/pick');
313312
var _pickBy = require('lodash/pickBy');
314-
var _keys = require('lodash/keys');
315313
var _each = require('lodash/each');
316314
var _isNil = require('lodash/isNil');
317315
var Inflector = require('./inflector');
318316

319-
module.exports = function (collectionName, record, payload, opts) {
317+
module.exports = function (collectionName, record, payload, opts, includedSet) {
320318
function isComplexType(obj) {
321319
return Array.isArray(obj) || isPlainObject(obj);
322320
}
323321

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+
324342
function keyForAttribute(attribute) {
325343
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);
333345
} 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);
341347
} else {
342-
if (isFunction(opts.keyForAttribute)) {
343-
return opts.keyForAttribute(attribute);
344-
} else {
345-
return Inflector.caserize(attribute, opts);
346-
}
348+
return keyForAttributeFunction(attribute);
347349
}
348350
}
349351

@@ -411,45 +413,46 @@ module.exports = function (collectionName, record, payload, opts) {
411413
}
412414
}
413415

416+
function pickFunction (value, key) {
417+
return keyForAttribute(key);
418+
}
419+
414420
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);
418422
}
419423

420-
function isCompoundDocumentIncluded(included, item) {
421-
return _find(payload.included, { id: item.id, type: item.type });
424+
function isCompoundDocumentIncluded(setKey) {
425+
return includedSet[setKey];
422426
}
423427

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);
426431
if (included) {
427432
// Merge relationships
428433
included.relationships = _merge(included.relationships,
429-
_pickBy(include.relationships, _identity));
434+
_pickBy(item.relationships, _identity));
430435

431436
// Merge attributes
432437
included.attributes = _merge(included.attributes,
433-
_pickBy(include.attributes, _identity));
438+
_pickBy(item.attributes, _identity));
434439
} 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);
437443
}
438444
}
439445

440446
this.serialize = function (dest, current, attribute, opts) {
441-
var that = this;
442447
var data = null;
443448

444449
if (opts && opts.ref) {
445450
if (!dest.relationships) { dest.relationships = {}; }
446451

447452
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));
451454
} else {
452-
data = that.serializeRef(current[attribute], current, attribute,
455+
data = this.serializeRef(current[attribute], current, attribute,
453456
opts);
454457
}
455458

@@ -472,16 +475,14 @@ module.exports = function (collectionName, record, payload, opts) {
472475
} else {
473476
if (Array.isArray(current[attribute])) {
474477
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));
478479
} else {
479480
data = current[attribute];
480481
}
481482

482483
dest.attributes[keyForAttribute(attribute)] = data;
483484
} else if (isPlainObject(current[attribute])) {
484-
data = that.serializeNested(current[attribute], current, attribute, opts);
485+
data = this.serializeNested(current[attribute], current, attribute, opts);
485486
dest.attributes[keyForAttribute(attribute)] = data;
486487
} else {
487488
dest.attributes[keyForAttribute(attribute)] = current[attribute];
@@ -490,7 +491,6 @@ module.exports = function (collectionName, record, payload, opts) {
490491
};
491492

492493
this.serializeRef = function (dest, current, attribute, opts) {
493-
var that = this;
494494
var id = getRef(current, dest, opts);
495495
var type = getType(attribute, dest);
496496

@@ -517,11 +517,12 @@ module.exports = function (collectionName, record, payload, opts) {
517517
var included = { type: type, id: id };
518518
if (includedAttrs) { included.attributes = pick(dest, includedAttrs); }
519519

520-
relationships.forEach(function (relationship) {
520+
const relationShipsFunction = (relationship) => {
521521
if (dest && (isComplexType(dest[relationship]) || dest[relationship] === null)) {
522-
that.serialize(included, dest, relationship, opts[relationship]);
522+
this.serialize(included, dest, relationship, opts[relationship]);
523523
}
524-
});
524+
}
525+
relationships.forEach(relationShipsFunction);
525526

526527
if (includedAttrs.length &&
527528
(opts.included === undefined || opts.included)) {
@@ -536,8 +537,6 @@ module.exports = function (collectionName, record, payload, opts) {
536537
};
537538

538539
this.serializeNested = function (dest, current, attribute, opts) {
539-
var that = this;
540-
541540
var embeds = [];
542541
var attributes = [];
543542

@@ -553,11 +552,12 @@ module.exports = function (collectionName, record, payload, opts) {
553552

554553
if (attributes) { ret.attributes = pick(dest, attributes); }
555554

556-
embeds.forEach(function (embed) {
555+
const embedsFunction = (embed) => {
557556
if (isComplexType(dest[embed])) {
558-
that.serialize(ret, dest, embed, opts[embed]);
557+
this.serialize(ret, dest, embed, opts[embed]);
559558
}
560-
});
559+
}
560+
embeds.forEach(embedsFunction);
561561
} else {
562562
ret.attributes = dest;
563563
}
@@ -566,8 +566,6 @@ module.exports = function (collectionName, record, payload, opts) {
566566
};
567567

568568
this.perform = function () {
569-
var that = this;
570-
571569
if( record === null ){
572570
return null;
573571
}
@@ -591,7 +589,7 @@ module.exports = function (collectionName, record, payload, opts) {
591589
data.meta = getMeta(record, opts.dataMeta);
592590
}
593591

594-
_each(opts.attributes, function (attribute) {
592+
_each(opts.attributes, (attribute) => {
595593
var splittedAttributes = attribute.split(':');
596594

597595
if (opts[attribute] && !record[attribute] && opts[attribute].nullIfMissing) {
@@ -607,15 +605,15 @@ module.exports = function (collectionName, record, payload, opts) {
607605
attributeMap = splittedAttributes[1];
608606
}
609607

610-
that.serialize(data, record, attribute, opts[attributeMap]);
608+
this.serialize(data, record, attribute, opts[attributeMap]);
611609
}
612610
});
613611

614612
return data;
615613
};
616614
};
617615

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){
619617
'use strict';
620618
var isFunction = require('lodash/isFunction');
621619
var _mapValues = require('lodash/mapValues');
@@ -625,23 +623,26 @@ module.exports = function (collectionName, records, opts) {
625623
this.serialize = function (records) {
626624
var that = this;
627625
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+
}
628635

629636
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);
637638
}
638639

639640
function collection() {
640641
payload.data = [];
641642

642643
records.forEach(function (record) {
643644
var serializerUtils = new SerializerUtils(that.collectionName, record,
644-
payload, that.opts);
645+
payload, that.opts, includedSet);
645646
payload.data.push(serializerUtils.perform());
646647
});
647648

@@ -650,7 +651,7 @@ module.exports = function (collectionName, records, opts) {
650651

651652
function resource() {
652653
payload.data = new SerializerUtils(that.collectionName, records, payload,
653-
that.opts).perform(records);
654+
that.opts, includedSet).perform(records);
654655

655656
return payload;
656657
}

0 commit comments

Comments
 (0)