Created
April 20, 2016 15:01
-
-
Save timsgardner/313f79065cc8e03446fc35dc0d46cbad to your computer and use it in GitHub Desktop.
prototype table
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 m = require('mori'); | |
function uniquify(coll){ | |
var seen = m.set(); | |
var retval = m.reduce( | |
function(bldg, x){ | |
if(m.hasKey(seen, x)){ | |
return bldg; | |
}else{ | |
seen = m.conj(seen, x); | |
return m.conj(bldg, x);}}, | |
m.vector(), | |
coll); | |
return retval;} | |
function proto(x){ | |
if (x === undefined || x === null){ | |
return null; | |
} | |
return (Object.prototype.hasOwnProperty.call(x, '__proto__') && x.__proto__) || null;} | |
function notNull(x){ | |
return !(x === null);} | |
// dies and takes the universe with it: | |
// function prototypeChain(x){ | |
// return m.intoArray( | |
// m.comp(m.takeWhile(notNull)), | |
// m.iterate(proto, x));} | |
// works: | |
function prototypeChain(x){ | |
return m.intoArray(m.takeWhile(notNull, m.iterate(proto, x)));} | |
function sjoin(separator, coll){ | |
return m.intoArray(coll).join(separator);} | |
function strPad(s, n){ | |
return s + ' '.repeat(Math.max(0, n - s.length));} | |
var commonNames = m.set(Object.getOwnPropertyNames((({}).__proto__))); | |
var filterCommonNames = false; // gross but expedient | |
function nameStr(x){return '' + (x.name || x);}; | |
function printProtoTable(x){ | |
var objNameF = arguments[1] || nameStr; | |
var hidden = m.set(m.concat(arguments[2], (filterCommonNames && commonNames) || null)); | |
var pchain = prototypeChain(x); | |
var objnames = m.map(function(x){return '' + objNameF(x);}, pchain); | |
var propss = m.map(Object.getOwnPropertyNames, pchain); | |
var propsm = m.zipmap(pchain, m.map(m.set, propss)); | |
var allProps = uniquify(m.remove(function(x){return m.hasKey(hidden, x);}, | |
m.reduce(m.into, m.vector(), propss))); | |
var propMaxLen = Math.max.apply(null, | |
m.intoArray( | |
m.map(function(p){return p.length;}, | |
allProps))); | |
console.log(''); | |
console.log(sjoin(' | ', m.cons(' '.repeat(propMaxLen), objnames))); | |
console.log(sjoin('-+-', m.cons('-'.repeat(propMaxLen), | |
m.map(function(nm){return '-'.repeat(nm.length);}, | |
objnames)))); | |
m.each(allProps, function(prop){ | |
console.log( | |
sjoin(' | ', | |
m.cons(strPad(prop, propMaxLen), | |
m.map( | |
function(obj, objname){ | |
var s = ' '; | |
if(m.hasKey(m.get(propsm, obj), prop)){s = 'x';}else{s = ' ';} | |
return s.repeat(objname.length);}, | |
pchain, | |
objnames))));});} | |
function tidyPrintProtoTable(x){ | |
printProtoTable(x, arguments[1], commonNames); | |
} | |
module.exports = {uniquify: uniquify, | |
prototypeChain: prototypeChain, | |
sjoin: sjoin, | |
strPad: strPad, | |
commonNames: commonNames, | |
printProtoTable: printProtoTable, | |
tidyPrintProtoTable: tidyPrintProtoTable, | |
getFilterCommonNames: function(){return filterCommonNames;}, | |
setFilterCommonNames: function(bool){filterCommonNames = bool; return bool;}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires mori