@typespec/http-server-js
Version:
TypeSpec HTTP server code generator for JavaScript
72 lines • 2.47 kB
JavaScript
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license.
/**
* Returns true if a value implements the ECMAScript Iterable interface.
*/
export function isIterable(value) {
return (typeof value === "object" &&
value !== null &&
Symbol.iterator in value &&
typeof value[Symbol.iterator] === "function");
}
/**
* Concatenate multiple iterables into a single iterable.
*/
export function* cat(...iterables) {
for (const iterable of iterables) {
yield* iterable;
}
}
/**
* Filter and collect an iterable into multiple groups based on a categorization function.
*
* The categorization function returns a string key for each value, and the values are returned in an object where each
* key is a category returned by the categorization function and the value is an array of values in that category.
*
* @param values - an iterable of values to categorize
* @param categorize - a categorization function that returns a string key for each value
* @returns an object where each key is a category and the value is an array of values in that category
*/
export function categorize(values, categorize) {
const result = {};
for (const value of values) {
(result[categorize(value)] ??= []).push(value);
}
return result;
}
/**
* Filter and collect an iterable into two categorizations based on a predicate function.
*
* Items for which the predicate returns true will be returned in the first array.
* Items for which the predicate returns false will be returned in the second array.
*
* @param values - an iterable of values to filter
* @param predicate - a predicate function that decides whether a value should be included in the first or second array
*
* @returns a tuple of two arrays of values filtered by the predicate
*/
export function bifilter(values, predicate) {
const pass = [];
const fail = [];
for (const value of values) {
if (predicate(value)) {
pass.push(value);
}
else {
fail.push(value);
}
}
return [pass, fail];
}
/**
* Prepends a string `indentation` to each value in `values`.
*
* @param values - an iterable of strings to indent
* @param indentation - the string to prepend to the beginning of each value
*/
export function* indent(values, indentation = " ") {
for (const value of values) {
yield indentation + value;
}
}
//# sourceMappingURL=iter.js.map