Created
June 17, 2011 19:32
-
-
Save leeoniya/1032127 to your computer and use it in GitHub Desktop.
better-than-json recordset passing
This file contains 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
(function() { | |
// mimicing php's array_combine() | |
function combine(a, b) { | |
var i, o = {}; | |
for (i in a) | |
o[a[i]] = b[i]; | |
return o; | |
} | |
// parses recordsets and returns an array of object instances | |
function recSet(data, facts) { | |
facts = facts || window; | |
var head = data.head.split(data.fldt), | |
recs = data.recs.split(data.rect), | |
fact = data.fact ? facts[data.fact] : function(obj) {return obj;}; | |
for (var i in recs) | |
recs[i] = fact(combine(head, recs[i].split(data.fldt))); | |
return recs; | |
} | |
// factories | |
var facts = { | |
person: function(obj) {return obj;} | |
}; | |
// recordset | |
var data = { | |
type: "delim", | |
fldt: "|", | |
rect: "~", | |
fact: "person", // object instantiation factory | |
head: "fname|lname|moomoo", | |
recs: "a|b|c~d|e|f", | |
}; | |
console.log(JSON.stringify(data)); | |
console.log(JSON.stringify(recSet(data, facts))); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment