Created
October 9, 2020 11:23
-
-
Save Keireira/8153a7679d6be5ecca84d48a00f3af74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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