Files
shaka-player/lib/util/array_utils.js
T
Joey Parrish f539147d48 fix: Correct license headers in compiled output
This fixes all the license headers in the main library, which corrects
the appearance of the main license in the compiled output.

It seems that the `!` in the header forces the compiler to keep it in
the output.  I believe older compiler releases did this purely based
on `@license`.

Issue #2638

Change-Id: I7f0e918caad10c9af689c9d07672b7fe9be7b2f3
2020-06-09 16:05:09 -07:00

99 lines
2.1 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.ArrayUtils');
/**
* @namespace shaka.util.ArrayUtils
* @summary Array utility functions.
*/
shaka.util.ArrayUtils = class {
/**
* Returns whether the two values contain the same value. This correctly
* handles comparisons involving NaN.
* @param {T} a
* @param {T} b
* @return {boolean}
* @template T
*/
static defaultEquals(a, b) {
// NaN !== NaN, so we need to special case it.
if (typeof a === 'number' &&
typeof b === 'number' && isNaN(a) && isNaN(b)) {
return true;
}
return a === b;
}
/**
* Remove given element from array (assumes no duplicates).
* @param {!Array.<T>} array
* @param {T} element
* @template T
*/
static remove(array, element) {
const 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
*/
static count(array, check) {
let count = 0;
for (const element of array) {
count += check(element) ? 1 : 0;
}
return count;
}
/**
* Determines if the given arrays contain the same elements.
*
* @param {!Array.<T>} a
* @param {!Array.<T>} b
* @param {function(T, T):boolean=} compareFn
* @return {boolean}
* @template T
*/
static hasSameElements(a, b, compareFn) {
if (!compareFn) {
compareFn = shaka.util.ArrayUtils.defaultEquals;
}
if (a.length != b.length) {
return false;
}
const copy = b.slice();
for (const item of a) {
const idx = copy.findIndex((other) => compareFn(item, other));
if (idx == -1) {
return false;
}
// Since order doesn't matter, just swap the last element with
// this one and then drop the last element.
copy[idx] = copy[copy.length - 1];
copy.pop();
}
return copy.length == 0;
}
};