Skip to content

Commit

Permalink
save over 500KB with one weird trick (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 authored and samselikoff committed Mar 27, 2019
1 parent eb689e3 commit 100b827
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
31 changes: 17 additions & 14 deletions addon/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import _isEmpty from 'lodash/isEmpty';
import _includes from 'lodash/includes';
import _assign from 'lodash/assign';
import _get from 'lodash/get';
import _ from 'lodash';
import _flatten from 'lodash/flatten';
import _compact from 'lodash/compact';
import _uniqBy from 'lodash/uniqBy';

/**
Serializers are responsible for formatting your route handler's response.
Expand Down Expand Up @@ -473,19 +475,20 @@ class Serializer {
return [hash, []];

} else {
let addToIncludes = _(serializer.getKeysForIncluded())
.map((key) => {
if (this.isCollection(resource)) {
return resource.models.map((m) => m[key]);
} else {
return resource[key];
}
})
.flatten()
.compact()
.uniqBy(m => m.toString())
.value();

let addToIncludes = _uniqBy(
_compact(
_flatten(
serializer.getKeysForIncluded().map(key => {
if (this.isCollection(resource)) {
return resource.models.map(m => m[key]);
} else {
return resource[key];
}
})
)
),
m => m.toString()
);
return [hash, addToIncludes];
}
}
Expand Down
15 changes: 7 additions & 8 deletions addon/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Serializer from '../serializer';
import { dasherize, pluralize, camelize } from '../utils/inflector';
import _get from 'lodash/get';
import _ from 'lodash';
import _flatten from 'lodash/flatten';
import _compact from 'lodash/compact';
import _uniqBy from 'lodash/uniqBy';
import _isEmpty from 'lodash/isEmpty';
import assert from 'ember-cli-mirage/assert';

/**
Expand Down Expand Up @@ -95,11 +98,7 @@ const JSONAPISerializer = Serializer.extend({
includes.push(newIncludes);
});

return _(includes)
.flatten()
.compact()
.uniqBy(m => m.toString())
.value();
return _uniqBy(_compact(_flatten(includes)), m => m.toString());
},

getIncludesForResourceAndPath(resource, ...names) {
Expand Down Expand Up @@ -182,14 +181,14 @@ const JSONAPISerializer = Serializer.extend({
relationshipHash.data = data;
}

if (!_.isEmpty(relationshipHash)) {
if (!_isEmpty(relationshipHash)) {
relationships[relationshipKey] = relationshipHash;
}

return relationships;
}, {});

if (!_.isEmpty(relationships)) {
if (!_isEmpty(relationships)) {
hash.relationships = relationships;
}

Expand Down

0 comments on commit 100b827

Please sign in to comment.