2021-06-22 14:46:42 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
export type NullToUndefined<T> = Extract<T, null> extends never
|
|
|
|
? T
|
|
|
|
: Exclude<T, null> | undefined;
|
2021-06-22 14:46:42 +00:00
|
|
|
|
|
|
|
export function dropNull<T>(
|
|
|
|
value: NonNullable<T> | null | undefined
|
|
|
|
): T | undefined {
|
2022-09-14 21:40:44 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2021-06-22 14:46:42 +00:00
|
|
|
if (value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export function shallowDropNull<O extends { [key: string]: any }>(
|
|
|
|
value: O | null | undefined
|
|
|
|
):
|
|
|
|
| {
|
|
|
|
[Property in keyof O]: NullToUndefined<O[Property]>;
|
|
|
|
}
|
|
|
|
| undefined {
|
2022-09-14 21:40:44 +00:00
|
|
|
if (value == null) {
|
2021-07-09 19:36:10 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const result: any = {};
|
|
|
|
|
|
|
|
for (const [key, propertyValue] of Object.entries(value)) {
|
|
|
|
result[key] = dropNull(propertyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|