Source: lib/polyfill/typed_array.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2025 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.TypedArray');
  7. goog.require('shaka.polyfill');
  8. /**
  9. * @summary A polyfill to provide missing TypedArray methods for older
  10. * browsers (indexOf/lastIndexOf/includes).
  11. * @export
  12. */
  13. shaka.polyfill.TypedArray = class {
  14. /**
  15. * Install the polyfill if needed.
  16. * @export
  17. */
  18. static install() {
  19. const typedArrays = [
  20. Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
  21. Int8Array, Int16Array, Int32Array, Float32Array, Float64Array,
  22. ];
  23. for (const typedArray of typedArrays) {
  24. // eslint-disable-next-line no-restricted-syntax
  25. if (!typedArray.prototype.indexOf) {
  26. // eslint-disable-next-line no-restricted-syntax
  27. typedArray.prototype.indexOf = shaka.polyfill.TypedArray.indexOf_;
  28. }
  29. // eslint-disable-next-line no-restricted-syntax
  30. if (!typedArray.prototype.lastIndexOf) {
  31. // eslint-disable-next-line no-restricted-syntax
  32. typedArray.prototype.lastIndexOf =
  33. shaka.polyfill.TypedArray.lastIndexOf_;
  34. }
  35. // eslint-disable-next-line no-restricted-syntax
  36. if (!typedArray.prototype.includes) {
  37. // eslint-disable-next-line no-restricted-syntax
  38. typedArray.prototype.includes = shaka.polyfill.TypedArray.includes_;
  39. }
  40. }
  41. }
  42. /**
  43. * @param {number} searchElement
  44. * @param {number} fromIndex
  45. * @return {number}
  46. * @this {TypedArray}
  47. * @private
  48. */
  49. static indexOf_(searchElement, fromIndex) {
  50. // eslint-disable-next-line no-restricted-syntax
  51. return Array.prototype.indexOf.call(this, searchElement, fromIndex);
  52. }
  53. /**
  54. * @param {number} searchElement
  55. * @param {number} fromIndex
  56. * @return {number}
  57. * @this {TypedArray}
  58. * @private
  59. */
  60. static lastIndexOf_(searchElement, fromIndex) {
  61. // eslint-disable-next-line no-restricted-syntax
  62. return Array.prototype.lastIndexOf.call(this, searchElement, fromIndex);
  63. }
  64. /**
  65. * @param {number} searchElement
  66. * @param {number} fromIndex
  67. * @return {boolean}
  68. * @this {TypedArray}
  69. * @private
  70. */
  71. static includes_(searchElement, fromIndex) {
  72. return this.indexOf(searchElement, fromIndex) !== -1;
  73. }
  74. };
  75. shaka.polyfill.register(shaka.polyfill.TypedArray.install);