Created
December 20, 2016 19:17
-
-
Save miguel-leon/f26fc598ab4e9bee86252694baad6b94 to your computer and use it in GitHub Desktop.
Create object from schema
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
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