JSFiddle - React, Tailwind, and code Playground
by Cyril J
HTML
<div id="example"></div>
JavaScript
/*
Super-lightweight JSON-to-JSON transformations
Cyril Jandia (02/2016)
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
( See also: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10 )
*/
JSLT();
// (A "Company" is just an object with a "Team")
function Company(obj) {
return obj.team && Team(obj.team);
}
// (A "Team" is just a non-empty array that contains at least one "Member")
function Team(obj) {
return ({ }.toString.call(obj) === "[object Array]") &&
obj.length &&
obj.find(function(item) { return Member(item); });
}
// (A "Member" must have first and last names, and a gender)
function Member(obj) {
return obj.first && obj.last && obj.sex;
}
function Dude(obj) {
return Member(obj) && (obj.sex === "Male");
}
function Girl(obj) {
return Member(obj) && (obj.sex === "Female");
}
var data = { team: [
{ first: "John", last: "Smith", sex: "Male" },
{ first: "Vaio", last: "Sony" },
{ first: "Anna", last: "Smith", sex: "Female" },
{ first: "Peter", last: "Olsen", sex: "Male" }
] };
var TO_SOMETHING_ELSE = { $: [
[ [ Company ],
function(company) {
return { some_virtual_dom: {
the_dudes: { ul: company.team.select(Dude).through(this) },
the_grrls: { ul: company.team.select(Girl).through(this) }
} }
} ],
[ [ Member ],
function(member) {
return { li: "{first} {last} ({sex})".of(member) };
} ]
] };
document.getElementById("example").innerHTML += "<xmp>" + JSON.stringify(data.through(TO_SOMETHING_ELSE), null, 4) + "</xmp>";
/*
.
.
.
.
.
.
.
.
.
.
*/
function JSLT() {
function dataset(obj) {
var extension = { groupBy: groupBy, orderBy: orderBy },
extend = function(o, x) {
for (var p in x) {
if (x.hasOwnProperty(p) && (typeof x[p] === "function")) {
o[p] = x[p].bind(o);
}
}
return o;
};
return extend(obj, extension);
}
function groupBy()...