Skip to content

Commit

Permalink
refactor: add null to undefined util (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-steinfeld authored Nov 7, 2023
1 parent 49132d0 commit 3185f66
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion projects/common/src/utilities/lang/lang-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqualWith } from 'lodash-es';
import { isEqualWith, mapValues } from 'lodash-es';

/**
* Useful in a place like the default case of a switch statement where each enum value is a case. If a new enum
Expand Down Expand Up @@ -37,3 +37,14 @@ export const isNonEmptyString = (str: string | undefined | null): str is string
export const hasOwnProperty = <X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> =>
// Since Typescript doesn't know how to type guard native hasOwnProperty, we wrap it here.
obj.hasOwnProperty(prop);

export const nullToUndefined = <T>(input: T): T => {
if (input instanceof Array) {
return input.map(nullToUndefined) as T;
}
if (typeof input === 'object' && input !== null) {
return mapValues(input, nullToUndefined) as T;
}

return input ?? (undefined as T);
};

0 comments on commit 3185f66

Please sign in to comment.