Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
coerce null ratings to 0 [close #15]
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-weber committed Nov 27, 2015
1 parent db703ab commit ce8318e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/js/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ function f(requiredItems, optionalItems) {
field.explanation = opt.explanation || '';
field.is_datetime = opt.is_datetime || false;

if (opt.coerce) {
field.coerce = opt.coerce;
} else {
field.coerce = val => {return val;};
}

return field;
}

Expand All @@ -38,7 +44,10 @@ exports.fields = [
f([22, 'playCount', Lf.Type.INTEGER], {
label: 'play count'}),
f([23, 'rating', Lf.Type.INTEGER], {
explanation: '0: no thumb, 1: down thumb, 5: up thumb.'}),
explanation: '0: no thumb, 1: down thumb, 5: up thumb.',
// coerce nulls to 0; see https://github.com/simon-weber/Autoplaylists-for-Google-Music/issues/15.
coerce: val => {return val || 0;},
}),

// Lf.Type.DATE_TIME introduces a TypeError on indexing,
// and lots of serialization headaches without any benefit.
Expand Down Expand Up @@ -74,7 +83,7 @@ exports.lfToBusinessTypes = lToB;
exports.fromJsproto = function fromJsproto(jsproto) {
const track = {};
exports.fields.forEach(field => {
track[field.name] = jsproto[field.protoNum];
track[field.name] = field.coerce(jsproto[field.protoNum]);
});

return track;
Expand Down

0 comments on commit ce8318e

Please sign in to comment.