Source: lib/util/array_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.ArrayUtils');
  18. /**
  19. * @namespace shaka.util.ArrayUtils
  20. * @summary Array utility functions.
  21. */
  22. /**
  23. * Returns whether the two values contain the same value. This correctly
  24. * handles comparisons involving NaN.
  25. * @param {T} a
  26. * @param {T} b
  27. * @return {boolean}
  28. * @template T
  29. */
  30. shaka.util.ArrayUtils.defaultEquals = function(a, b) {
  31. // NaN !== NaN, so we need to special case it.
  32. if (typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b)) {
  33. return true;
  34. }
  35. return a === b;
  36. };
  37. /**
  38. * Remove duplicate entries from an array. Order N^2, so use with caution.
  39. * @param {!Array.<T>} array
  40. * @param {function(T, T): boolean=} compareFn An optional function which
  41. * will be used to compare items in the array.
  42. * @return {!Array.<T>}
  43. * @template T
  44. */
  45. shaka.util.ArrayUtils.removeDuplicates = function(array, compareFn) {
  46. if (!compareFn) {
  47. compareFn = shaka.util.ArrayUtils.defaultEquals;
  48. }
  49. let result = [];
  50. for (const item of array) {
  51. const idx = shaka.util.ArrayUtils.indexOf(result, item, compareFn);
  52. if (idx == -1) {
  53. result.push(item);
  54. }
  55. }
  56. return result;
  57. };
  58. /**
  59. * Find an item in an array. For use when comparison of entries via == will
  60. * not suffice.
  61. * @param {!Array.<T>} array
  62. * @param {T} value
  63. * @param {function(T, T): boolean} compareFn A function which will be used to
  64. * compare items in the array.
  65. * @return {number} The index, or -1 if not found.
  66. * @template T
  67. */
  68. shaka.util.ArrayUtils.indexOf = function(array, value, compareFn) {
  69. for (let i = 0; i < array.length; ++i) {
  70. if (compareFn(array[i], value)) {
  71. return i;
  72. }
  73. }
  74. return -1;
  75. };
  76. /**
  77. * Remove given element from array (assumes no duplicates).
  78. * @param {!Array.<T>} array
  79. * @param {T} element
  80. * @template T
  81. */
  82. shaka.util.ArrayUtils.remove = function(array, element) {
  83. let index = array.indexOf(element);
  84. if (index > -1) {
  85. array.splice(index, 1);
  86. }
  87. };
  88. /**
  89. * Count the number of items in the list that pass the check function.
  90. * @param {!Array.<T>} array
  91. * @param {function(T):boolean} check
  92. * @return {number}
  93. * @template T
  94. */
  95. shaka.util.ArrayUtils.count = function(array, check) {
  96. let count = 0;
  97. array.forEach(function(element) {
  98. count += check(element) ? 1 : 0;
  99. });
  100. return count;
  101. };