Javascript LINQ-like Querying?
July 15. 2007
0 Comments
- Posted in:
- javascript
Just coding for coding sake, I decided to try implement a LINQ-like query in JavaScript. And the result is as follow:
(function() { 'use strict'; /** * Javascript LINQ-like query proof of concept * Jimmy Chandra - 16 July 2007 * Updated on 2015-02-22 */ /* * 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) { /*jshint validthis: true */ 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) { /*jshint validthis: true */ return _each(this.items, l, function(r, o, i) { r.push(l(o[i])); }); } From.prototype.select = _select; function Where(o) { /*jshint validthis: true */ 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) { /*jshint validthis: true */ 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++) { console.log(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 (x = 0, l = p.length; x < l; x++) { console.log(p[x].FullName + ":" + p[x].Age); } }());
Run it on jsfiddle: http://jsfiddle.net/jchandra/6ugyxc2s/
Fun :)