Files
shaka-player/lib/util/array_utils.js
T
Aaron Vaage bdaa18aaa5 Support Injecting Delays Networking Engine
Our storage progress tests on Edge were sometimes failing as the promise
resolution order would change. This happened because we queue streams to be
downloaded in parallel.

This changes FakeNetworkingEngine to allow us to register an async function
as a callback for a request. This allowed us to update the test to resolve
network requests in lock step ensuring a deterministic order.

Since the progress test rely on a lot of assumptions, the tests were moved
into their own top-level group and now have their own test manifests.

Change-Id: Id366efccd15d90814ee34ca9a17df7d733ab6d67
2018-08-09 21:17:17 +00:00

114 lines
2.7 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.util.ArrayUtils');
/**
* @namespace shaka.util.ArrayUtils
* @summary Array utility functions.
*/
/**
* Remove duplicate entries from an array. Order N^2, so use with caution.
* @param {!Array.<T>} array
* @param {function(T, T): boolean=} compareFn An optional function which
* will be used to compare items in the array.
* @return {!Array.<T>}
* @template T
*/
shaka.util.ArrayUtils.removeDuplicates = function(array, compareFn) {
let result = [];
for (let i = 0; i < array.length; ++i) {
let matchFound = false;
for (let j = 0; j < result.length; ++j) {
matchFound = compareFn ? compareFn(array[i], result[j]) :
array[i] === result[j];
if (matchFound) break;
}
if (!matchFound) {
result.push(array[i]);
}
}
return result;
};
/**
* Find an item in an array. For use when comparison of entries via == will
* not suffice.
* @param {!Array.<T>} array
* @param {T} value
* @param {function(T, T): boolean} compareFn A function which will be used to
* compare items in the array.
* @return {number} The index, or -1 if not found.
* @template T
*/
shaka.util.ArrayUtils.indexOf = function(array, value, compareFn) {
for (let i = 0; i < array.length; ++i) {
if (compareFn(array[i], value)) {
return i;
}
}
return -1;
};
/**
* Remove given element from array (assumes no duplicates).
* @param {!Array.<T>} array
* @param {T} element
* @template T
*/
shaka.util.ArrayUtils.remove = function(array, element) {
let index = array.indexOf(element);
if (index > -1) {
array.splice(index, 1);
}
};
/**
* Count the number of items in the list that pass the check function.
* @param {!Array.<T>} array
* @param {function(T):boolean} check
* @return {number}
* @template T
*/
shaka.util.ArrayUtils.count = function(array, check) {
let count = 0;
array.forEach(function(element) {
count += check(element) ? 1 : 0;
});
return count;
};
/**
* Create a copy of another array.
*
* @param {!Array.<T>} array
* @return {!Array.<T>}
* @template T
*/
shaka.util.ArrayUtils.copy = function(array) {
return array.map((x) => x);
};