Last active
July 23, 2020 04:12
-
-
Save isotopeee/9451b44bda1f17d01759c66db13926f2 to your computer and use it in GitHub Desktop.
Create schema recursion
This file contains 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
function createSchema(obj) { | |
return Object.entries(obj).reduce(recurse, {}); | |
} | |
function recurse(prev, [currentKey, currentValue]) { | |
let valType; | |
switch (typeof currentValue) { | |
case 'string': | |
valType = 'string'; | |
break; | |
case 'number': | |
valType = 'number'; | |
break; | |
default: | |
valType = Object.entries(currentValue).reduce(recurse, {}); | |
} | |
return { | |
...prev, | |
[currentKey]: typeof currentValue === 'object' ? valType : { | |
name: currentKey, | |
type: valType, | |
}, | |
}; | |
} |
This file contains 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
function createSchema(obj) { | |
return Object.entries(obj).reduce(recurse, {}) | |
} | |
function recurse(prev, [currentKey, currentValue]) { | |
let valType; | |
switch (typeof currentValue) { | |
case 'string': | |
valType = 'string'; | |
break; | |
case 'number': | |
valType = 'number'; | |
break; | |
default: | |
valType = Object.entries(currentValue).reduce(recurse, {}); | |
} | |
return { | |
...prev, | |
[currentKey]: valType, | |
}; | |
} |
This file contains 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
function createSchema(obj) { | |
return recurse(obj); | |
} | |
function recurse(obj) { | |
return Object.entries(obj).reduce((prev, [currentKey, currentValue]) => { | |
let retval; | |
switch (typeof currentValue) { | |
case 'number': | |
retval = 'number'; | |
break; | |
case 'string': | |
retval = 'string'; | |
break; | |
default: | |
retval = recurse(currentValue); | |
} | |
return { | |
...prev, | |
[currentKey]: retval, | |
}; | |
}, {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment