/*
*
* Javascript LINQ-like query proof of concept
* Jimmy Chandra - 16 July 2007
*/
/*
* Quicksort implementation for JavaScript Array (based on
* http://en.literateprograms.org/Quicksort_(JavaScript)),
* modified to pass a function delegate for the comparison function.
*/
Array.prototype.swap = function(i, j) { var a = this,
t = a[i];
a[i] = a[j];
a[j] = t;
}
function partition(a, s, e, p, l) { var v = a[p],
t = s,
i;
a.swap(p, e - 1);
for (i = s; i < e - 1; i++) { if (l(a[i], v)) { a.swap(t, i);
t++;
}
}
a.swap(e - 1, t);
return t;
}
function qsort(a, s, e, l) { var p;
if (e - 1 > s) { p = s + Math.floor((e - s) / 2);
p = partition(a, s, e, p, l);
qsort(a, s, p, l);
qsort(a, p + 1, e, l);
}
}
Array.prototype.quicksort = function (l) { qsort(this, 0, this.length, l);
}
/*
* JavaScript LINQ-like query engine sample implementation
*/
function _each(o, l, a) { var r = [],
m = o.length,
i;
if (l) { for (i = 0; i < m; i++) { if (l(o[i])) { a(r, o, i);
}
}
} else { for (i = 0; i < m; i++) { a(r, o, i);
}
}
return r;
}
function from(o) { return new _from(o);
}
function _from(o) { this.items = o;
}
_from.prototype.where = function(l) { return new _where(
_each(this.items, l,
function(r, o, i) { r.push(o[i]);
}));
}
function _select(l) { return _each(this.items, l,
function(r, o, i) { r.push(l(o[i]));
});
}
_from.prototype.select = _select;
function _where(o) { this.items = o;
}
_where.prototype.select = _select;
_where.prototype.orderby = function(a) { var f,
k = a.length,
r = _each(this.items, null,
function(r, o, i) { r.push(o[i]);
});
if (k === 1 && a[0] === "") { f = function(i, j) { return i <= j;
};
} else { f = function(i, j) { var l = "",
r = "";
for (var n = 0; n < k; n++) { l += i[a[n]];
r += j[a[n]];
}
return l <= r;
};
}
r.quicksort(f);
return new _orderby(r);
}
function _orderby(o) { this.items = o;
}
_orderby.prototype.select = _select;
/*
* Javascript LINQ-like querying example
*/
var numbers = [17, 6, 12, 14, 20, 13, 10];
var b = from (numbers).
where (function(n) { return n > 9 && n < 20; }). orderby ([ "" ]).
select (function(n) { return n; });
for (var x = 0, l = b.length; x < l; x++) { WScript.Echo(b[x]);
}
var peoples = [
{ FirstName : "John", LastName : "Doe", Age : 29 }, { FirstName : "Jane", LastName : "Doe", Age : 33 }, { FirstName : "Mary", LastName : "White", Age : 31 }, { FirstName : "Barry", LastName : "White", Age : 31 }, { FirstName : "Kevin", LastName : "Black", Age : 31 }, { FirstName : "Anna", LastName : "Smith", Age : 1 }];
var p = from (peoples).
where (function(n) { return n.LastName === "Doe" || n.LastName === "White"; }). orderby([ "Age", "LastName", "FirstName" ]).
select(function(n) { return { FullName : n.FirstName + " " + n.LastName, Age : n.Age }; });
for (var x = 0, l = p.length; x < l; x++) { WScript.Echo(p[x].FullName + ":" + p[x].Age);
}