UNPKG

@ts-common/azure-js-dev-tools

Version:

Developer dependencies for TypeScript related projects

226 lines 7.98 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for * license information. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.removeFirst = exports.indexOf = exports.toArray = exports.map = exports.where = exports.contains = exports.last = exports.first = exports.any = void 0; var tslib_1 = require("tslib"); /** * Get whether or not the provided array contains any values. * @param values The array to check. * @returns Whether or not the provided array contains any values. */ function any(values) { return !!(values && values.length > 0); } exports.any = any; /** * Get the first value in the provided array of values that matches the provided condition. * @param values The array of values to search through. * @param condition The condition to use when looking through the array of values. */ function first(values, condition) { var e_1, _a; var result; if (values) { if (condition instanceof Function) { try { for (var values_1 = tslib_1.__values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) { var value = values_1_1.value; if (condition(value)) { result = value; break; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1); } finally { if (e_1) throw e_1.error; } } } else if (condition !== undefined) { result = first(values, function (value) { return value === condition; }); } else { result = values[0]; } } return result; } exports.first = first; /** * Get the last value in the provided array of values that matches the provided condition. * @param values The array of values to search through. * @param condition The condition to use when looking through the array of values. */ function last(values, condition) { var result; if (values) { if (condition instanceof Function) { for (var i = values.length - 1; 0 <= i; --i) { var value = values[i]; if (condition(value)) { result = value; break; } } } else if (condition !== undefined) { result = last(values, function (value) { return value === condition; }); } else { result = values[values.length - 1]; } } return result; } exports.last = last; /** * Get whether or not the provided array of values contains a value that matches the provided * condition. * @param values The array of values to search through. * @param condition The condition to look for within the array of values. * @returns Whether or not the provided array of values contains a value that matches the provided * condition. */ function contains(values, condition) { var e_2, _a; var result = false; if (values) { if (condition instanceof Function) { try { for (var values_2 = tslib_1.__values(values), values_2_1 = values_2.next(); !values_2_1.done; values_2_1 = values_2.next()) { var value = values_2_1.value; if (condition(value)) { result = true; break; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (values_2_1 && !values_2_1.done && (_a = values_2.return)) _a.call(values_2); } finally { if (e_2) throw e_2.error; } } } else { result = contains(values, function (value) { return value === condition; }); } } return result; } exports.contains = contains; /** * Get all of the values within the provided array of values that match the provided condition. * @param values The array of values to filter. * @param condition The condition to look for within the array of values. * @returns The array of values from the original values that match the provided condition. */ function where(values, condition) { var e_3, _a; var result = []; try { for (var values_3 = tslib_1.__values(values), values_3_1 = values_3.next(); !values_3_1.done; values_3_1 = values_3.next()) { var value = values_3_1.value; if (condition(value)) { result.push(value); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (values_3_1 && !values_3_1.done && (_a = values_3.return)) _a.call(values_3); } finally { if (e_3) throw e_3.error; } } return result; } exports.where = where; /** * Map the values in the provided array to a new array of values. * @param values The values to map to a new array of values. * @param conversion The function that will be used to convert the original values into the new * ones. * @returns The array with converted values. */ function map(values, conversion) { var e_4, _a; var result = []; if (values) { try { for (var values_4 = tslib_1.__values(values), values_4_1 = values_4.next(); !values_4_1.done; values_4_1 = values_4.next()) { var value = values_4_1.value; result.push(conversion(value)); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (values_4_1 && !values_4_1.done && (_a = values_4.return)) _a.call(values_4); } finally { if (e_4) throw e_4.error; } } } return result; } exports.map = map; /** * Ensure that a value that is either a single value or an array is an array by wrapping single * values in an array. * @param value The value to ensure is an array. * @param conversion The function that will be used to convert the non-array value to an array. This * defaults to just creating a new array with the single value. * @returns The array value. */ function toArray(value, conversion) { if (conversion === void 0) { conversion = function (valueToConvert) { return [valueToConvert]; }; } return value instanceof Array ? value : conversion(value); } exports.toArray = toArray; /** * Get the index of the first value that matches the provided condition. -1 will be returned if no * matching index is found. * @param values The values to look through. * @param condition The condition to check against each of the elements. * @returns The first index that matches the provided condition or -1 if the condition is never * satisfied. */ function indexOf(values, condition) { var result = -1; if (values) { for (var i = 0; i < values.length; ++i) { if (condition(values[i], i)) { result = i; break; } } } return result; } exports.indexOf = indexOf; /** * Remove and return the first element in the provided values that matches the provided condition. * Undefined will be returned if no matching element is found. * @param values The values to look through. * @param condition The condition to check against each of the elements. */ function removeFirst(values, condition) { var result; if (values) { var indexToRemove = indexOf(values, condition); if (indexToRemove !== -1) { result = values.splice(indexToRemove, 1)[0]; } } return result; } exports.removeFirst = removeFirst; //# sourceMappingURL=arrays.js.map