I found the solution by :
- Expanding the main range into array of numbers.
- Loop collection, expand also those into array of numbers.
- Check if the array is within the range, if it's within range remove those numbers from the main array, if not return false (array to check is not witin range)
- After the loop, check the length of main array, if it's more than 0 return false (array collection is within range, but doesn't cover the main range). If it's 0 meaning the collection array is within range, and cover the whole main array.
When googling I found the use of lodash. here is the code in my function :
let parentRange = _.range(start, end + 1); // Expand into array of numbers
for (var i = 0; i < collection.length; i++) {
let rangeToCheck = _.range(collection[i].start, collection[i].end + 1); //Also expand into array of numbers
if (_.difference(rangeToCheck, parentRange).length === 0) { // Check if collection range is within parent range
_.pullAll(parentRange, rangeToCheck); // Remove the numbers from parent range that matches collcection range
} else {
return false; // Not within parent range
}
}
// Check if parent range has values left
if (parentRange.length != 0) {
return false; // Collection ranges does not cover parent range
} else {
return true; // Collection ranges is within parent range, and cover the whole numbers
}