UNPKG

904 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { isNull } from "../is/null.js";
4import { isUndefined } from "../is/undefined.js";
5/**
6 * @name arrayFilter
7 * @summary Filters undefined and (optionally) null values from an array
8 * @description
9 * Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { arrayFilter } from '@polkadot/util';
15 *
16 * arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, '']
17 * arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, '']
18 * ```
19 */
20// eslint-disable-next-line @typescript-eslint/no-explicit-any
21
22export function arrayFilter(array, allowNulls = true) {
23 return array.filter(v => !isUndefined(v) && (allowNulls || !isNull(v)));
24}
\No newline at end of file