Skip to content

Instantly share code, notes, and snippets.

@miguel-leon
Created December 20, 2016 19:17
Show Gist options
  • Save miguel-leon/f26fc598ab4e9bee86252694baad6b94 to your computer and use it in GitHub Desktop.
Save miguel-leon/f26fc598ab4e9bee86252694baad6b94 to your computer and use it in GitHub Desktop.
Create object from schema
if (!Object.fromSchema) {
Object.defineProperty(Object, 'fromSchema', {
enumerable: false,
value: function fromSchema(schema) {
var result = {};
Object.keys(schema).forEach(function (key) {
value = schema[key];
if (value && value.constructor === Function) {
result[key] = value();
}
else if (value && value.constructor === Object) {
result[key] = Object.fromSchema(value);
}
else {
result[key] = value;
}
}, this);
return result;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment