Skip to content

Instantly share code, notes, and snippets.

@Keireira
Created October 9, 2020 11:23
Show Gist options
  • Select an option

  • Save Keireira/8153a7679d6be5ecca84d48a00f3af74 to your computer and use it in GitHub Desktop.

Select an option

Save Keireira/8153a7679d6be5ecca84d48a00f3af74 to your computer and use it in GitHub Desktop.
import cloneDeep from 'lodash/cloneDeep';
const replaceNull = (value) => (value === '') ? null : value;
const proxyHandler = {
get(target, key, receiver) {
const value = Reflect.get(target, key, receiver);
const nullifiedValue = replaceNull(value);
if (typeof nullifiedValue === 'object' && nullifiedValue !== null) {
return new Proxy(nullifiedValue, proxyHandler);
}
return nullifiedValue;
},
};
const replaceEmptyStringsByNull = (initialObject) => {
const proxy = new Proxy(initialObject, proxyHandler);
return cloneDeep(proxy);
};
export default replaceEmptyStringsByNull;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment