Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save over 500KB with one weird trick #1561

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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