Last active
December 26, 2015 06:38
-
-
Save jlewin/7108784 to your computer and use it in GitHub Desktop.
Early attempts at validating three.js help docs from extracted JSON
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
| var helpDocs = {}; | |
| helpDocs['MeshBasicMaterial'] = info; | |
| var types = { | |
| 'number': 'number', | |
| 'Integer': 'number', | |
| 'Float': 'number', | |
| 'Boolean': 'boolean', | |
| 'Boolean': 'boolean', | |
| 'string': 'string', | |
| 'String': 'string' | |
| }; | |
| var typeMap = { | |
| 'Texture' : THREE.Texture, | |
| 'TextureCube' : THREE.TextureCube | |
| }; | |
| function validateDocs(className, instance) { | |
| var helpDoc = helpDocs[className]; | |
| var properties = helpDoc.Properties; | |
| for (var p in properties) { | |
| hasExpectedProperty(instance, properties[p], helpDoc.properties); | |
| } | |
| } | |
| validateDocs('MeshBasicMaterial', obj); | |
| function hasExpectedProperty(instance, item) { | |
| var itemInstance = instance[item.name]; | |
| var actualType = typeof itemInstance; | |
| // Sanitize type names from help docs into js base types | |
| var expectedType = types[item.type] || item.type; | |
| var hasProp = itemInstance != null; | |
| var correctType = expectedType == actualType; | |
| if (!correctType && actualType == 'object') { | |
| if (itemInstance == null || typeof itemInstance == 'undefined') { | |
| console.log('Unable to validate null instance: ' + item.name); | |
| correctType == true; | |
| } else { | |
| var typeFunction = typeMap[item.type]; | |
| correctType = typeFunction && itemInstance instanceof typeFunction; | |
| } | |
| } | |
| console.log((( hasProp && correctType ) ? '- Pass: ' : '- Fail: ') + item.name + ' : ' + actualType); | |
| if (!hasProp) | |
| console.log(' - Missing ' + item.name); | |
| if (!correctType) | |
| console.log(' - Type mismatch: ' + item.type + ' != ' + actualType ); | |
| } | |
| function dumpDef2(obj) { | |
| var info = {}; | |
| for(var v in obj) { | |
| if (obj.hasOwnProperty(v)) | |
| info[v] = typeof obj[v]; | |
| else | |
| inherited[v] = v; | |
| } | |
| var scope = {}; | |
| info.inherited = scope; | |
| for(var v in inherited) { | |
| scope[v] = typeof obj[v]; | |
| } | |
| return info; | |
| } | |
| function dumpDef(obj) { | |
| var inherited = {}; | |
| console.log('************ Direct Properties ***********'); | |
| for (var v in obj) { | |
| if (obj.hasOwnProperty(v)) | |
| console.log(v + ' : ' + typeof obj[v]); | |
| else | |
| inherited[v] = v; | |
| } | |
| console.log('************ Inherited ***********'); | |
| for(var v in inherited) { console.log(v + ' : ' + typeof obj[v]); } | |
| } | |
| //dumpDef(obj) | |
| //console.log(dumpDef2(obj)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment