Skip to content

Commit

Permalink
chore(utils/array): add isNotNullOrUndefined (#22081)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrindel committed May 11, 2023
1 parent b5d87c6 commit ebf064e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/util/array.spec.ts
@@ -0,0 +1,12 @@
import { isNotNullOrUndefined } from './array';

describe('util/array', () => {
it.each`
a | exp
${null} | ${false}
${undefined} | ${false}
${{ name: 'foo' }} | ${true}
`('.isNotNullOrUndefined', ({ a, exp }) => {
expect(isNotNullOrUndefined(a)).toEqual(exp);
});
});
9 changes: 9 additions & 0 deletions lib/util/array.ts
Expand Up @@ -10,3 +10,12 @@ export function coerceArray<T>(input: T[] | null | undefined): T[] {
export function sortNumeric(a: number, b: number): number {
return a - b;
}

// Useful for filtering an array so that it includes values that are not null or
// undefined. This predicate acts as a type guard so that the resulting type for
// `values.filter(isNotNullOrUndefined)` is `T[]`.
export function isNotNullOrUndefined<T>(
value: T | undefined | null
): value is T {
return !is.nullOrUndefined(value);
}

0 comments on commit ebf064e

Please sign in to comment.