Skip to content

Instantly share code, notes, and snippets.

@isotopeee
Last active July 23, 2020 04:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isotopeee/9451b44bda1f17d01759c66db13926f2 to your computer and use it in GitHub Desktop.
Save isotopeee/9451b44bda1f17d01759c66db13926f2 to your computer and use it in GitHub Desktop.
Create schema recursion
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,
},
};
}
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,
};
}
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