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

feat(time-ranges): make iteratable if Symbol.iterator exists #7330

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Changes from 1 commit
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
22 changes: 16 additions & 6 deletions src/js/utils/time-ranges.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file time-ranges.js
* @module time-ranges
*/
import window from 'global/window';

/**
* Returns the time for the specified index at the start or end
Expand Down Expand Up @@ -94,8 +95,10 @@ function getRange(fnName, valueIndex, ranges, rangeIndex) {
* An array of time ranges.
*/
function createTimeRangesObj(ranges) {
let timeRangesObj;

if (ranges === undefined || ranges.length === 0) {
return {
timeRangesObj = {
length: 0,
start() {
throw new Error('This TimeRanges object is empty');
Expand All @@ -104,12 +107,19 @@ function createTimeRangesObj(ranges) {
throw new Error('This TimeRanges object is empty');
}
};
} else {
timeRangesObj = {
length: ranges.length,
start: getRange.bind(null, 'start', 0, ranges),
end: getRange.bind(null, 'end', 1, ranges)
};
}
return {
length: ranges.length,
start: getRange.bind(null, 'start', 0, ranges),
end: getRange.bind(null, 'end', 1, ranges)
};

if (window.Symbol && window.Symbol.iterator) {
timeRangesObj[window.Symbol.iterator] = () => (ranges || []).values();
}

return timeRangesObj;
}

/**
Expand Down