3 exports.__esModule = true;
4 exports.createTimeRange = undefined;
5 exports.createTimeRanges = createTimeRanges;
7 var _log = require('./log.js');
9 var _log2 = _interopRequireDefault(_log);
11 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
14 * Returns the time for the specified index at the start or end
15 * of a TimeRange object.
17 * @function time-ranges:indexFunction
19 * @param {number} [index=0]
20 * The range number to return the time for.
23 * The time that offset at the specified index.
25 * @depricated index must be set to a value, in the future this will throw an error.
29 * An object that contains ranges of time for various reasons.
31 * @typedef {Object} TimeRange
33 * @property {number} length
34 * The number of time ranges represented by this Object
36 * @property {time-ranges:indexFunction} start
37 * Returns the time offset at which a specified time range begins.
39 * @property {time-ranges:indexFunction} end
40 * Returns the time offset at which a specified time range begins.
42 * @see https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges
46 * Check if any of the time ranges are over the maximum index.
48 * @param {string} fnName
49 * The function name to use for logging
51 * @param {number} index
54 * @param {number} maxIndex
55 * The maximum possible index
57 * @throws {Error} if the timeRanges provided are over the maxIndex
59 function rangeCheck(fnName, index, maxIndex) {
60 if (index < 0 || index > maxIndex) {
61 throw new Error('Failed to execute \'' + fnName + '\' on \'TimeRanges\': The index provided (' + index + ') is greater than or equal to the maximum bound (' + maxIndex + ').');
66 * Check if any of the time ranges are over the maximum index.
68 * @param {string} fnName
69 * The function name to use for logging
71 * @param {string} valueIndex
72 * The proprety that should be used to get the time. should be 'start' or 'end'
74 * @param {Array} ranges
75 * An array of time ranges
77 * @param {Array} [rangeIndex=0]
78 * The index to start the search at
81 * The time that offset at the specified index.
84 * @depricated rangeIndex must be set to a value, in the future this will throw an error.
85 * @throws {Error} if rangeIndex is more than the length of ranges
88 * @file time-ranges.js
91 function getRange(fnName, valueIndex, ranges, rangeIndex) {
92 if (rangeIndex === undefined) {
93 _log2['default'].warn('DEPRECATED: Function \'' + fnName + '\' on \'TimeRanges\' called without an index argument.');
96 rangeCheck(fnName, rangeIndex, ranges.length - 1);
97 return ranges[rangeIndex][valueIndex];
101 * Create a time range object givent ranges of time.
103 * @param {Array} [ranges]
104 * An array of time ranges.
106 function createTimeRangesObj(ranges) {
107 if (ranges === undefined || ranges.length === 0) {
110 start: function start() {
111 throw new Error('This TimeRanges object is empty');
113 end: function end() {
114 throw new Error('This TimeRanges object is empty');
119 length: ranges.length,
120 start: getRange.bind(null, 'start', 0, ranges),
121 end: getRange.bind(null, 'end', 1, ranges)
126 * Should create a fake `TimeRange` object which mimics an HTML5 time range instance.
128 * @param {number|Array} start
129 * The start of a single range or an array of ranges
131 * @param {number} end
132 * The end of a single range.
136 function createTimeRanges(start, end) {
137 if (Array.isArray(start)) {
138 return createTimeRangesObj(start);
139 } else if (start === undefined || end === undefined) {
140 return createTimeRangesObj();
142 return createTimeRangesObj([[start, end]]);
145 exports.createTimeRange = createTimeRanges;