Version 1
[yaffs-website] / node_modules / video.js / es5 / utils / time-ranges.js
1 'use strict';
2
3 exports.__esModule = true;
4 exports.createTimeRange = undefined;
5 exports.createTimeRanges = createTimeRanges;
6
7 var _log = require('./log.js');
8
9 var _log2 = _interopRequireDefault(_log);
10
11 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12
13 /**
14  * Returns the time for the specified index at the start or end
15  * of a TimeRange object.
16  *
17  * @function time-ranges:indexFunction
18  *
19  * @param {number} [index=0]
20  *        The range number to return the time for.
21  *
22  * @return {number}
23  *         The time that offset at the specified index.
24  *
25  * @depricated index must be set to a value, in the future this will throw an error.
26  */
27
28 /**
29  * An object that contains ranges of time for various reasons.
30  *
31  * @typedef {Object} TimeRange
32  *
33  * @property {number} length
34  *           The number of time ranges represented by this Object
35  *
36  * @property {time-ranges:indexFunction} start
37  *           Returns the time offset at which a specified time range begins.
38  *
39  * @property {time-ranges:indexFunction} end
40  *           Returns the time offset at which a specified time range begins.
41  *
42  * @see https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges
43  */
44
45 /**
46  * Check if any of the time ranges are over the maximum index.
47  *
48  * @param {string} fnName
49  *        The function name to use for logging
50  *
51  * @param {number} index
52  *        The index to check
53  *
54  * @param {number} maxIndex
55  *        The maximum possible index
56  *
57  * @throws {Error} if the timeRanges provided are over the maxIndex
58  */
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 + ').');
62   }
63 }
64
65 /**
66  * Check if any of the time ranges are over the maximum index.
67  *
68  * @param {string} fnName
69  *        The function name to use for logging
70  *
71  * @param {string} valueIndex
72  *        The proprety that should be used to get the time. should be 'start' or 'end'
73  *
74  * @param {Array} ranges
75  *        An array of time ranges
76  *
77  * @param {Array} [rangeIndex=0]
78  *        The index to start the search at
79  *
80  * @return {number}
81  *         The time that offset at the specified index.
82  *
83  *
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
86  */
87 /**
88  * @file time-ranges.js
89  * @module time-ranges
90  */
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.');
94     rangeIndex = 0;
95   }
96   rangeCheck(fnName, rangeIndex, ranges.length - 1);
97   return ranges[rangeIndex][valueIndex];
98 }
99
100 /**
101  * Create a time range object givent ranges of time.
102  *
103  * @param {Array} [ranges]
104  *        An array of time ranges.
105  */
106 function createTimeRangesObj(ranges) {
107   if (ranges === undefined || ranges.length === 0) {
108     return {
109       length: 0,
110       start: function start() {
111         throw new Error('This TimeRanges object is empty');
112       },
113       end: function end() {
114         throw new Error('This TimeRanges object is empty');
115       }
116     };
117   }
118   return {
119     length: ranges.length,
120     start: getRange.bind(null, 'start', 0, ranges),
121     end: getRange.bind(null, 'end', 1, ranges)
122   };
123 }
124
125 /**
126  * Should create a fake `TimeRange` object which mimics an HTML5 time range instance.
127  *
128  * @param {number|Array} start
129  *        The start of a single range or an array of ranges
130  *
131  * @param {number} end
132  *        The end of a single range.
133  *
134  * @private
135  */
136 function createTimeRanges(start, end) {
137   if (Array.isArray(start)) {
138     return createTimeRangesObj(start);
139   } else if (start === undefined || end === undefined) {
140     return createTimeRangesObj();
141   }
142   return createTimeRangesObj([[start, end]]);
143 }
144
145 exports.createTimeRange = createTimeRanges;