Skip to content

Instantly share code, notes, and snippets.

@brito
Last active February 3, 2017 00:56
Show Gist options
  • Save brito/2dd1a0fa88bd160029228ad7c1e32407 to your computer and use it in GitHub Desktop.
Save brito/2dd1a0fa88bd160029228ad7c1e32407 to your computer and use it in GitHub Desktop.
var rules = [ // WIP
// trim value white space
[,/^ +| +$/g,''],
// cast to boolean
['featured',, Boolean],
// cast to date
['lastupdate',, Date]].map(transform);
Catalog(someObject, orArraysThereof);
/*
┌─┐┌─┐┌┬┐┌─┐┬ ┌─┐┌─┐
│ ├─┤ │ ├─┤│ │ ││ ┬
└─┘┴ ┴ ┴ ┴ ┴┴─┘└─┘└─┘ */
function Catalog(...items){
return items.reduce((catalog), {});
function catalog(dict, item){
for (let [key, value] of Object.entries(item)){
let initial = key in dict? dict[key]: new Map;
switch (value.constructor.name){
case 'Array':
dict[key] = value.reduce(catalog, initial);
break;
case 'Object':
dict[key] = [value].reduce(catalog, initial);
break;
default:
dict[key] = archive(initial, value, item);
break;
}
}
return dict;
function archive(context, key, item){
context[key] = key in context ?
[item].concat(context[key])
: [item];
return context;
}
}
}
//WIP
function transform(rules){
var [l, r, lambda] = rules;
return rules.map(function(rule){
return function(key, value){
return [key, value];
}
});
}
//WIP
function reductor(tuple, fn){
//console.warn(tuple[0], tuple[1]);
return tuple;
}
// II. transform by key pattern
// 1. featured: Boolean
// 2. lastupdate: Date
// 3. owner: Person
// 4. url: protocol, host, port, pathname, search, hash
// 5. type: Navigation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment