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

add fix for missing attributes #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
184 changes: 94 additions & 90 deletions m3u/AttributeList.js
Original file line number Diff line number Diff line change
@@ -1,124 +1,128 @@
var AttributeList = module.exports = function AttributeList(attributes) {
this.attributes = {};
this.mergeAttributes(attributes);
this.attributes = {};
this.mergeAttributes(attributes);
};

var dataTypes = AttributeList.dataTypes = {
'audio' : 'quoted-string',
'autoselect' : 'boolean',
'bandwidth' : 'decimal-integer',
'byterange' : 'enumerated-string',
'codecs' : 'quoted-string',
'default' : 'boolean',
'duration' : 'decimal-floating-point',
'forced' : 'boolean',
'group-id' : 'quoted-string',
'language' : 'quoted-string',
'name' : 'quoted-string',
'program-id' : 'decimal-integer',
'resolution' : 'decimal-resolution',
'subtitles' : 'quoted-string',
'title' : 'enumerated-string',
'type' : 'enumerated-string',
'uri' : 'quoted-string',
'video' : 'quoted-string'
'audio' : 'quoted-string',
'average-bandwidth' : 'decimal-integer',
'autoselect' : 'boolean',
'bandwidth' : 'decimal-integer',
'byterange' : 'enumerated-string',
'closed-captions' : 'quoted-string',
'codecs' : 'quoted-string',
'default' : 'boolean',
'duration' : 'decimal-floating-point',
'forced' : 'boolean',
'frame-rate' : 'decimal-floating-point',
'group-id' : 'quoted-string',
'instream-id' : 'quoted-string',
'language' : 'quoted-string',
'name' : 'quoted-string',
'program-id' : 'decimal-integer',
'resolution' : 'decimal-resolution',
'subtitles' : 'quoted-string',
'title' : 'enumerated-string',
'type' : 'enumerated-string',
'uri' : 'quoted-string',
'video' : 'quoted-string'
};

AttributeList.prototype.mergeAttributes = function mergeAttributes(attributes) {
var self = this;
if (Array.isArray(attributes)) {
attributes.forEach(function(attribute) {
self.set(attribute.key, attribute.value);
});
}
var self = this;
if (Array.isArray(attributes)) {
attributes.forEach(function (attribute) {
self.set(attribute.key, attribute.value);
});
}
};

AttributeList.prototype.get = function getValue(key) {
return this.attributes[key];
return this.attributes[key];
};

AttributeList.prototype.set = function setValue(key, value) {
key = key.toLowerCase();
this.attributes[key] = parse[dataTypes[key] || 'unknown'](value, key);
key = key.toLowerCase();
this.attributes[key] = parse[dataTypes[key] || 'unknown'](value, key);

return this;
return this;
};

AttributeList.prototype.getCoerced = function getCoerced(key) {
return coerce[dataTypes[key] || 'unknown'](this.get(key));
return coerce[dataTypes[key] || 'unknown'](this.get(key));
};

AttributeList.prototype.toString = function toString() {
var self = this;
return Object.keys(this.attributes).map(function(key) {
return [key.toUpperCase(), self.getCoerced(key)].join('=');
}).join(',');
var self = this;
return Object.keys(this.attributes).map(function (key) {
return [key.toUpperCase(), self.getCoerced(key)].join('=');
}).join(',');
};

AttributeList.prototype.serialize = function serialize() {
return this.attributes;
return this.attributes;
};

AttributeList.unserialize = function unserialize(object) {
var list = new AttributeList;
list.attributes = object;
return list;
var list = new AttributeList;
list.attributes = object;
return list;
};

var coerce = {
'boolean': function coerceBoolean(value) {
return value ? 'YES' : 'NO';
},
'decimal-floating-point': parseFloat,
'decimal-integer': function coerceDecimalInteger(value) {
return parseInt(value, 10);
},
'decimal-resolution': function coerceDecimalResolution(value) {
if (Array.isArray(value)) {
return value.join('x');
} else {
return value;
'boolean': function coerceBoolean(value) {
return value ? 'YES' : 'NO';
},
'decimal-floating-point': parseFloat,
'decimal-integer': function coerceDecimalInteger(value) {
return parseInt(value, 10);
},
'decimal-resolution': function coerceDecimalResolution(value) {
if (Array.isArray(value)) {
return value.join('x');
} else {
return value;
}
},
'enumerated-string': function coerceEnumeratedString(value) {
return value;
},
'quoted-string': function coerceQuotedString(value) {
return '"' + value.replace(/"/g, '\\"') + '"';
},
'unknown': function coerceUnknown(value) {
return value;
}
},
'enumerated-string': function coerceEnumeratedString(value) {
return value;
},
'quoted-string': function coerceQuotedString(value) {
return '"' + value.replace(/"/g, '\\"') + '"';
},
'unknown': function coerceUnknown(value) {
return value;
}
};

var parse = {
'boolean': function parseBoolean(value) {
return typeof value == 'boolean'
? value
: (value == 'YES' ? true : false);
},
'decimal-floating-point': parseFloat,
'decimal-integer': function parseDecimalInteger(value) {
return parseInt(value, 10);
},
'decimal-resolution': function coerceDecimalResolution(value) {
return value.split('x').map(parse['decimal-integer']);
},
'enumerated-string': function parseEnumeratedString(value) {
return value;
},
'quoted-string': function parseQuotedString(value) {
if (Array.isArray(value)) {
return value.join(',');
} else if (value.indexOf('"') === 0 &&
value.lastIndexOf('"') == value.length - 1) {
return value.slice(1,-1);
} else {
return value;
'boolean': function parseBoolean(value) {
return typeof value == 'boolean'
? value
: (value == 'YES' ? true : false);
},
'decimal-floating-point': parseFloat,
'decimal-integer': function parseDecimalInteger(value) {
return parseInt(value, 10);
},
'decimal-resolution': function coerceDecimalResolution(value) {
return value.split('x').map(parse['decimal-integer']);
},
'enumerated-string': function parseEnumeratedString(value) {
return value;
},
'quoted-string': function parseQuotedString(value) {
if (Array.isArray(value)) {
return value.join(',');
} else if (value.indexOf('"') === 0 &&
value.lastIndexOf('"') == value.length - 1) {
return value.slice(1, -1);
} else {
return value;
}
},
'unknown': function parseUnknown(value, key) {
console.error('Handling value:', value, ' for unknown key:', key);
return value;
}
},
'unknown': function parseUnknown(value, key) {
console.error('Handling value:', value, ' for unknown key:', key);
return value;
}
};
};