Skip to content

Instantly share code, notes, and snippets.

@brito
Last active January 20, 2016 22:56
Show Gist options
  • Save brito/6975f6d1f1b43c0c4b3f to your computer and use it in GitHub Desktop.
Save brito/6975f6d1f1b43c0c4b3f to your computer and use it in GitHub Desktop.
/**
____ _
_ __ _____ __ | _ \ ___ ___ ___ _ __ __| |
| '_ \ / _ \ \ /\ / / | |_) / _ \/ __/ _ \| '__/ _` |
| | | | __/\ V V / | _ < __/ (_| (_) | | | (_| |
|_| |_|\___| \_/\_/ |_| \_\___|\___\___/|_| \__,_|
@constructor */
function Record(content){
// Without context, content is meaningless
if (!(this instanceof Record))
return new Record(content);
var context = this;
switch (typeof content){
case 'symbol':
if (!(content in context))
return Proxy(context, Symbol.for(content));
else
throw 'what now?'
case 'string':
case 'number':
return context[Symbol.for(content)];
// return this[Symbol.for(content)];
}
console.warn(content, context);
throw 'new Record({ this[+new Date]:content })'
console.warn(typeof content, content);
for (var key in content){
console.group(key);
Proxy(this, key);
// Uses the Proxy setter
this[key] = content[key];
console.groupEnd();
}
return this;
}
function Proxy(context, key){
if (!(key in context)) {
Object.defineProperty(context, key, {
enumerable: true,
get: function(){
return context[Symbol.for(key)];
},
set: function(value){
console.warn(typeof value);
if (value instanceof Array){
console.error(context[key], key, value);
value.map(context[key]);
throw 'Spread array of values into setter'
}
if ('object' == typeof value){
console.error(key, value);
Record({ key:value });
throw 'Record({ key:value })'
}
context[Symbol.for(key)] = (context[Symbol.for(key)] || {});
context[Symbol.for(key)][value] = (context[Symbol.for(key)][value] || []);
context[Symbol.for(key)][value].push(context);
}
});
}
return context[key];
}
/*
_
_____ ____ _ _ __ ___ _ __ | | ___ ___
/ _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \/ __|
| __/> < (_| | | | | | | |_) | | __/\__ \
\___/_/\_\__,_|_| |_| |_| .__/|_|\___||___/
|_|
var empty = new Record;
var tuple = new Record({ label: 'value' });
var view = new Record([ 1, 'A list of things', { type:'thing' } ]);
var list = new Record([ { type:'x' }, { type:'y' }, { type:'x', name:'the other x' } ]);
var nested = new Record({ name:'thing', another_thing:{ name:'another thing' } });
*/
/*
_____ _
|_ _|__ ___| |_ ___
| |/ _ \/ __| __/ __|
| | __/\__ \ |_\__ \
|_|\___||___/\__|___/
*/
with (console){
clear();
//https://gist.githubusercontent.com/brito/7a782859084f37d8915c/raw/528f341377554ab302e112fdba7e5ca6d2caf57b/data.json
var data = JSON.parse(document.querySelector('pre').innerHTML),
started = new Date;
timeStamp('Test started ' + started);
group('Test started ' + started);
assert(data, 'No data');
var data_items = 0;
for (var key in data)
data_items++
assert(data_items, 'Need a sample object with enumerables');
// table(data);
var record = new Record;
assert(record instanceof Record, 'Bad prototype chain');
var entries_in_empty = 0
for (var entry in record)
entries_in_empty++;
assert(!entries_in_empty, 'new Record should be empty');
// create
record = new Record(data);
assert('object' == typeof record, ':(');
var entries = 0;
for (var entry in record){
entries++;
assert(record[entry], 'No entry');
}
assert(entries, 'No enumerables in record');
// Object.get
// assert retrieve
for (var entry in record){
var columns = record[entry]
count_columns = 0;
for (var column in columns){
count_columns++;
assert(columns[column], 'Values vanished in thin air');
// throw 'carry on'
// console.info(entry, column, columns[column]);
console.info(entry, column, columns[column]);
// throw 'DO IT'
}
if (!count_columns)
console.warn(data[entry]);
assert(count_columns, 'No', entry);
}
// console.error('assert update');
// console.error('assert delete');
table(record);
info('Test complete', new Date - started +'ms');
groupEnd();
timeStamp('Test complete', new Date - started +'ms');
}
@brito
Copy link
Author

brito commented Jan 20, 2016

The basic idea is a cross-reference builder between keys and values (keys to values to objects and values to keys to objects):

var map = {};
for (var column in data){
    var value = data[column];
    map[column] = (map[column] || {});
    map[column][value] = (map[column][value] || []);
    map[column][value].push(data);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment