Skip to content

Instantly share code, notes, and snippets.

@mattfenwick
Last active August 29, 2015 13:57
Show Gist options
  • Save mattfenwick/9373554 to your computer and use it in GitHub Desktop.
Save mattfenwick/9373554 to your computer and use it in GitHub Desktop.
'use strict';
var genhtml = require('genhtml-js'),
h = genhtml.html,
s = genhtml.serialize,
types = require('function-js').types;
// extend Array
function ArrayExt() {}
ArrayExt.prototype = [1, 2, 3];
// extend Object
function ObjectExt() {}
// extend RegExp
function RegExpExt() {}
RegExpExt.prototype = /matt/;
// put examples inside a function to get access to `arguments`
function getExamples() {
var functionText = "new Function('x', 'return x + 1;')";
return [
// schema:
// 0: human-readable text
// 1: expression to be inspected
// 2: (optional) constructor for instanceof-checking
['undefined' , undefined , null ],
['null' , null , null ],
["'abc'" , 'abc' , String ],
["new String('abc')", new String('abc') , String ],
['123' , 123 , Number ],
['new Number(123)' , new Number(123) , Number ],
['Infinity' , Infinity , Number ],
['NaN' , NaN , Number ],
['true' , true , Boolean ],
['new Boolean(true)', new Boolean(true) , Boolean ],
['function g(x) {}' , function g(x) {} , Function ],
[functionText , new Function('x', 'return x + 1;'), Function ],
["{'a': 1, 'b': 2}" , {'a': 1, 'b': 2} , null ],
['new Object()' , new Object() , null ],
['new ObjectExt()' , new ObjectExt() , ObjectExt],
['[1, 2, 3]' , [1, 2, 3] , Array ],
['new Array()' , new Array() , Array ],
['new ArrayExt()' , new ArrayExt() , Array ], // yes! to make a point about subclassing Array
['/a/' , /a/ , RegExp ],
["new RegExp('a')" , new RegExp('a') , RegExp ],
['new RegExpExt()' , new RegExpExt() , RegExp ], // to make a point about subclassing RegExp
['new Date()' , new Date() , Date ],
["new Error('!')" , new Error('!') , Error ],
['Math' , Math , null ],
['JSON' , JSON , null ],
['arguments' , arguments , null ],
['this' , this /* the root object, right? */, null ] //Window ]
];
}
var protos = [Object, Array, Function, RegExp, Date, Boolean,
String, Number, Error, ArrayExt, ObjectExt, RegExpExt];
// EventTarget, Window, RegExpExt];
function figurePrototype(obj) {
for (var i = 0; i < protos.length; i++) {
console.log(protos[i]);
if ( obj == protos[i].prototype ) {
return protos[i].name;
}
}
return obj;
}
var prim_types = {'number': 1, 'string': 1, 'boolean': 1, 'undefined': 1};
function isPrimitive(x) {
return (x === null) || (prim_types.hasOwnProperty(typeof x));
}
var results = getExamples().map(function(eg) {
var name = eg[0],
e = eg[1];
return {
'name' : name,
'typeof' : typeof e,
'instanceof Object': e instanceof Object,
'instanceof subtype': eg[2] ? [eg[2].name, e instanceof eg[2]] : null,
'toString': Object.prototype.toString.call(e),
'parents' : isPrimitive(e) ? null : types.getPrototypes(e)
};
});
function formatResults(rs) {
var tr = h.tr, th = h.th, td = h.td;
var header = tr({},
th({}, 'example'),
th({}, 'typeof'),
th({}, 'Object.prototype.toString'),
th({}, 'instanceof Object'),
th({}, 'instanceof subtype'),
th({}, 'prototype chain')),
rows = [header];
results.map(function(r) {
var sub = r['instanceof subtype'];
console.log('example -- ' + JSON.stringify(r)); // eg.map(function(x) {return "" + x;}).join(','));
rows.push(tr({},
td({}, r['name']),
td({}, r['typeof']),
td({}, r['toString']),
td({}, "" + r['instanceof Object']),
td({}, sub ? (sub[0] + ": " + sub[1] ) : '--'),
td({}, r['parents'] ? ("" + r['parents'].map(figurePrototype)) : '--')));
});
var my_table = h.table.apply(undefined, [{'class': 'bordered padded'}].concat(rows)),
my_doc = h.root('html', h.html({}, h.body({}, my_table)));
return s.serialize(my_doc);
}
console.log(formatResults(results));
module.exports = {
'results' : results,
'getExamples': getExamples,
'formatResults': formatResults
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment