Skip to content

Commit d43d8e9

Browse files
committed
feat: added isNullish()
1 parent e802a69 commit d43d8e9

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/predicate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export * from './isLengthAware.js';
2626
export * from './isNonNullable.js';
2727
export * from './isNull.js';
2828
export * from './isNullArray.js';
29+
export * from './isNullish.js';
2930
export * from './isNumber.js';
3031
export * from './isNumberArray.js';
3132
export * from './isNumericString.js';

src/predicate/isNullish.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isNullish = (input: unknown): input is null | undefined => input == null;

test/predicate/isNullish.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { isNullish } from '../../src/predicate/isNullish.js';
4+
5+
describe('isNullish()', () => {
6+
it('Returns true for valid input', () => {
7+
expect(isNullish(undefined)).toBe(true);
8+
expect(isNullish(null)).toBe(true);
9+
});
10+
11+
it('Returns false for invalid input', () => {
12+
expect(isNullish('abc123')).toBe(false);
13+
expect(isNullish([])).toBe(false);
14+
});
15+
16+
it('Can be used with Array.filter()', () => {
17+
expect([1, null, 2, undefined, 3].filter(isNullish)).toEqual([null, undefined]);
18+
});
19+
});

0 commit comments

Comments
 (0)