Skip to content

Commit

Permalink
fix(utilities): add tsdoc for filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Jul 20, 2021
1 parent e2e822f commit 36d51d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/utilities/src/lib/filterNullAndUndefined.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { isNullOrUndefined } from './isNullOrUndefined';
import type { Nullish } from './utilityTypes';

/**
* Checks whether a value is not `null` nor `undefined`.
* This can be used in {@link Array.filter} to remove `null` and `undefined` from the array type
* @param value The value to verify that is neither `null` nor `undefined`
* @returns A boolean that is `true` if the value is neither `null` nor `undefined`, false otherwise.
* @example
* ```typescript
* // TypeScript Type: (string | undefined | null)[]
* const someArray = ['one', 'two', undefined, null, 'five'];
*
* // TypeScript Type: string[]
* const filteredArray = someArray.filter(filterNullAndUndefined);
* // Result: ['one', 'two', 'five']
* ```
*/
export function filterNullAndUndefined<TValue>(value: TValue | Nullish): value is TValue {
return !isNullOrUndefined(value);
}
15 changes: 15 additions & 0 deletions packages/utilities/src/lib/filterNullAndUndefinedAndEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { isNullOrUndefinedOrEmpty } from './isNullOrUndefinedOrEmpty';
import type { Nullish } from './utilityTypes';

/**
* Checks whether a value is not `null` nor `undefined` nor `''` (empty string).
* This can be used in {@link Array.filter} to remove `null`, `undefined` from the array type
* @param value The value to verify that is neither `null`, `undefined` nor `''` (empty string)
* @returns A boolean that is `true` if the value is neither `null`, `undefined` nor `''` (empty string), false otherwise.
* @example
* ```typescript
* // TypeScript Type: (string | undefined | null)[]
* const someArray = ['one', 'two', undefined, null, ''];
*
* // TypeScript Type: string[]
* const filteredArray = someArray.filter(filterNullAndUndefinedAndEmpty);
* // Result: ['one', 'two']
* ```
*/
export function filterNullAndUndefinedAndEmpty<TValue>(value: TValue | Nullish | ''): value is TValue {
return !isNullOrUndefinedOrEmpty(value);
}
15 changes: 15 additions & 0 deletions packages/utilities/src/lib/filterNullAndUndefinedAndZero.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { isNullOrUndefinedOrZero } from './isNullOrUndefinedOrZero';
import type { Nullish } from './utilityTypes';

/**
* Checks whether a value is not `null` nor `undefined` nor `0`.
* This can be used in {@link Array.filter} to remove `null`, `undefined` from the array type
* @param value The value to verify that is neither `null`, `undefined` nor `0`
* @returns A boolean that is `true` if the value is neither `null`, `undefined` nor `0`, false otherwise.
* @example
* ```typescript
* // TypeScript Type: (string | number | undefined | null)[]
* const someArray = ['one', 'two', undefined, null, 0, 1];
*
* // TypeScript Type: (string | number)[]
* const filteredArray = someArray.filter(filterNullAndUndefinedAndZero);
* // Result: ['one', 'two', 1]
* ```
*/
export function filterNullAndUndefinedAndZero<TValue>(value: TValue | Nullish | 0): value is TValue {
return !isNullOrUndefinedOrZero(value);
}

0 comments on commit 36d51d1

Please sign in to comment.