/** * @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.Functional'); /** * @summary A set of functional utility functions. */ shaka.util.Functional = class { /** * Creates a promise chain that calls the given callback for each element in * the array in a catch of a promise. * * e.g.: * Promise.reject().catch(callback(array[0])).catch(callback(array[1])); * * @param {!Array.} array * @param {function(ELEM):!Promise.} callback * @return {!Promise.} * @template ELEM,RESULT */ static createFallbackPromiseChain(array, callback) { return array.reduce((promise, elem) => { return promise.catch(() => callback(elem)); }, Promise.reject()); } /** * Returns the first array concatenated to the second; used to collapse an * array of arrays into a single array. * * @param {!Array.} all * @param {!Array.} part * @return {!Array.} * @template T */ static collapseArrays(all, part) { return all.concat(part); } /** * A no-op function that ignores its arguments. This is used to suppress * unused variable errors. * @param {...*} args */ static ignored(...args) {} /** * A no-op function. Useful in promise chains. */ static noop() {} /** * Returns if the given value is not null; useful for filtering out null * values. * * @param {T} value * @return {boolean} * @template T */ static isNotNull(value) { return value != null; } };