@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
35 lines (34 loc) • 776 B
JavaScript
import { check } from '@augment-vir/assert';
/**
* Filters the input array to all valid values from the given enum.
*
* @category Array
* @category Enum
* @category Package : @augment-vir/common
* @example
*
* ```ts
* enum MyEnum {
* A = 'a',
* B = 'b',
* }
*
* const result = filterToEnumValues(
* [
* 1,
* 2,
* 3,
* 'a',
* 'b',
* MyEnum.A,
* ],
* MyEnum,
* ); // result is `[MyEnum.A, MyEnum.B, MyEnum.A]`
* ```
*
* @returns A new array (does not mutate).
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export function filterToEnumValues(inputs, checkEnum) {
return inputs.filter((input) => check.isEnumValue(input, checkEnum));
}