Source: lib/util/functional.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Functional');
  7. /**
  8. * @summary A set of functional utility functions.
  9. */
  10. shaka.util.Functional = class {
  11. /**
  12. * Creates a promise chain that calls the given callback for each element in
  13. * the array in a catch of a promise.
  14. *
  15. * e.g.:
  16. * Promise.reject().catch(callback(array[0])).catch(callback(array[1]));
  17. *
  18. * @param {!Array.<ELEM>} array
  19. * @param {function(ELEM):!Promise.<RESULT>} callback
  20. * @return {!Promise.<RESULT>}
  21. * @template ELEM,RESULT
  22. */
  23. static createFallbackPromiseChain(array, callback) {
  24. return array.reduce((promise, elem) => {
  25. return promise.catch(() => callback(elem));
  26. }, Promise.reject());
  27. }
  28. /**
  29. * Returns the first array concatenated to the second; used to collapse an
  30. * array of arrays into a single array.
  31. *
  32. * @param {!Array.<T>} all
  33. * @param {!Array.<T>} part
  34. * @return {!Array.<T>}
  35. * @template T
  36. */
  37. static collapseArrays(all, part) {
  38. return all.concat(part);
  39. }
  40. /**
  41. * A no-op function that ignores its arguments. This is used to suppress
  42. * unused variable errors.
  43. * @param {...*} args
  44. */
  45. static ignored(...args) {}
  46. /**
  47. * A no-op function. Useful in promise chains.
  48. */
  49. static noop() {}
  50. /**
  51. * Returns if the given value is not null; useful for filtering out null
  52. * values.
  53. *
  54. * @param {T} value
  55. * @return {boolean}
  56. * @template T
  57. */
  58. static isNotNull(value) {
  59. return value != null;
  60. }
  61. };