Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Last active July 22, 2022 17:26
Show Gist options
  • Save imbcmdth/279fad279b94917cd36c to your computer and use it in GitHub Desktop.
Save imbcmdth/279fad279b94917cd36c to your computer and use it in GitHub Desktop.
/**
* @file time-ranges.js
*
* Should create a fake TimeRange object
* Mimics an HTML5 time range instance, which has functions that
* return the start and end times for a range
* TimeRanges are returned by the buffered() method
*
* @param {(Number|Array)} Start of a single range or an array of ranges
* @param {Number} End of a single range
* @private
* @method createTimeRanges
*/
export function createTimeRanges(start, end){
if (Array.isArray(start)) {
return createTimeRangesObj(start);
} else if (start === undefined || end === undefined) {
return createTimeRangesObj();
}
return createTimeRangesObj([[start, end]]);
}
export { createTimeRanges as createTimeRange };
function createTimeRangesObj(ranges){
if (ranges === undefined || ranges.length === 0) {
return {
length: 0,
start: function() {
throw new Error('This TimeRanges object is empty');
},
end: function() {
throw new Error('This TimeRanges object is empty');
}
};
}
return {
length: ranges.length,
start: function(i) { return ranges[i][0]; },
end: function(i) { return ranges[i][1]; }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment