Assure type safety in getSources() and getQualityOptions() (always return arrays), and remove external conditions and type conversion no longer needed

This commit is contained in:
Albin Larsson 2018-06-11 03:21:18 +02:00
parent 1ad76800b0
commit 4c1337b4c5

View File

@ -8,35 +8,21 @@ import utils from './utils';
const html5 = { const html5 = {
getSources() { getSources() {
if (!this.isHTML5) { if (!this.isHTML5) {
return null; return [];
} }
return this.media.querySelectorAll('source'); return Array.from(this.media.querySelectorAll('source'));
}, },
// Get quality levels // Get quality levels
getQualityOptions() { getQualityOptions() {
if (!this.isHTML5) { // Get sizes from <source> elements
return null; const sizes = html5.getSources.call(this)
} .map(source => Number(source.getAttribute('size')))
.filter(Boolean);
// Get sources
const sources = html5.getSources.call(this);
if (utils.is.empty(sources)) {
return null;
}
// Get <source> with size attribute
const sizes = Array.from(sources).filter(source => !utils.is.empty(source.getAttribute('size')));
// If none, bail
if (utils.is.empty(sizes)) {
return null;
}
// Reduce to unique list // Reduce to unique list
return utils.dedupe(sizes.map(source => Number(source.getAttribute('size')))); return utils.dedupe(sizes);
}, },
extend() { extend() {
@ -51,34 +37,17 @@ const html5 = {
get() { get() {
// Get sources // Get sources
const sources = html5.getSources.call(player); const sources = html5.getSources.call(player);
const [source] = sources.filter(source => source.getAttribute('src') === player.source);
if (utils.is.empty(sources)) { // Return size, if match is found
return null; return source && Number(source.getAttribute('size'));
}
const matches = Array.from(sources).filter(source => source.getAttribute('src') === player.source);
if (utils.is.empty(matches)) {
return null;
}
return Number(matches[0].getAttribute('size'));
}, },
set(input) { set(input) {
// Get sources // Get sources
const sources = html5.getSources.call(player); const sources = html5.getSources.call(player);
if (utils.is.empty(sources)) {
return;
}
// Get matches for requested size // Get matches for requested size
const matches = Array.from(sources).filter(source => Number(source.getAttribute('size')) === input); const matches = sources.filter(source => Number(source.getAttribute('size')) === input);
// No matches for requested size
if (utils.is.empty(matches)) {
return;
}
// Get supported sources // Get supported sources
const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type'))); const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type')));