clean up inflected dependency
clean ups for #129
This is an attempt to clean up inflected dependency.
- serialize
keyForAttributecan only take a function now and defaults to(k) => k- no longer accepts strings for casing
- serialize
pluralizeTypecan only take a function now and defaults to(t) => t-
pluralizeTypeno longer defaults totrue, 'pluralizing' types
-
- deserialize
keyForAttributecan only take a function now and defaults to(k) => k- no longer accepts strings for casing
These are definitely all major changes, v3->v4 probably, but i'd argue that they are steps in a good direction.
- clean up dependencies, smaller footprint
- keeps the same versatility for
keyForAttributeand removes unneeded methods/tests to handle string casing - adds more versatility to
pluralizeTypeby allowing a function that users can pass to handle their specific use cases
Thank you so much @hartzis !
yeah! take a look, let me know what you think.
I'll write a little migration readme here in the comments that you could possibly use in the wiki too.
@SeyZ Attempt at migration readme info:
Migrate from 3.0 to 4.0
V4 updates a few options making them a little more flexible:
-
keyForAttribute(serialize and deserialize) takes only a function now that returns a string- defaults to:
(key) => key
- defaults to:
-
pluralizeTypetakes only a function now that returns a string- defaults to:
(type) => type
- defaults to:
We've eliminated inflected as a dependency. Doing this eliminates another dependency making for a smaller package size overall.
v3 keyForAttribute defaulted to 'dash-case':
var data = [{ id: 1, firstName: 'Sandro', lastName: 'Munda' }];
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName']
});
var users = UserSerializer.serialize(data);
Produced(notice the attribute keys):
{
"data": [{
"type": "users",
"id": "1",
"attributes": {
"first-name": "Sandro",
"last-name": "Munda"
}
}
}
v4 attribute keys by default do not change now:
var data = [{ id: 1, firstName: 'Sandro', lastName: 'Munda' }];
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName']
});
var users = UserSerializer.serialize(data);
Now produces:
{
"data": [{
"type": "users",
"id": "1",
"attributes": {
"firstName": "Sandro",
"lastName": "Munda"
}
}]
}
NOTE To migrate and achieve v3 default behavior is easy! You can add inflected to your libraries dependencies and do:
import Inflector from 'inflected';
function dasherize(key) {
key = inflected.underscore(key);
return inflected.dasherize(key);
}
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName'],
keyForAttribute: dasherize,
pluralizeType: Inflector.pluralize
});
#142 seems pretty important, and I can update this PR after that gets in :)
seems there are definitely a few other important PRs that should probably get merged in before this.
I can get this updated after those other PRs get merged in!