object.ts 578 B

1234567891011121314151617181920
  1. import { isArray, isPlainObject } from 'lodash';
  2. /** @returns a deep clone of the object, but with any null value removed */
  3. export function sortedDeepCloneWithoutNulls<T>(value: T): T {
  4. if (isArray(value)) {
  5. return value.map(sortedDeepCloneWithoutNulls) as unknown as T;
  6. }
  7. if (isPlainObject(value)) {
  8. return Object.keys(value)
  9. .sort()
  10. .reduce((acc: any, key) => {
  11. const v = (value as any)[key];
  12. if (v != null) {
  13. acc[key] = sortedDeepCloneWithoutNulls(v);
  14. }
  15. return acc;
  16. }, {});
  17. }
  18. return value;
  19. }