Skip to content

Commit

Permalink
HTML into and from
Browse files Browse the repository at this point in the history
  • Loading branch information
agershun committed Dec 27, 2014
1 parent 006d2d0 commit 3d33ae8
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 8 deletions.
10 changes: 10 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
10. Streams, cursors, if-then, while, Views, Comsole

## Next Versions

FROM HTML()
1. Social to alasql.org
2. Wiki
3. Check Blob.js

1. ? IN ?

For the question:
http://stackoverflow.com/questions/15441670/intersection-of-arrays-of-objects-based-on-object-property-value/27657487#27657487

Calculate prime numbers with SQL

2. CSV/TAB export with headers:["header1","header2"] for arrays

1. Add TRIM to CSV reading procedure

if(trim(s) = +s)
Expand Down
96 changes: 96 additions & 0 deletions alasql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion alasql.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions alasql.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions console/alasql.min.js

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions dist/alasql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alasql.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/alasql.min.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/83into.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@
// (c) 2014 Andrey Gershun
//

utils.emptyChildren = function (container){
var len = container.childNodes.length;
while (len--) {
container.removeChild(container.lastChild);
};
};

alasql.into.HTML = function(selector, opts, data, columns, cb) {
var opt = {};
alasql.utils.extend(opt, opts);

var sel = document.querySelector(selector);
if(!sel) {
throw new Error('Selected HTML element is not found');
};

if(columns.length == 0) {
if(typeof data[0] == "object") {
columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}});
} else {
// What should I do?
// columns = [{columnid:"_"}];
}
}

var tbe = document.createElement('table');
var thead = document.createElement('thead');
tbe.appendChild(thead);
if(opt.headers) {
var tre = document.createElement('tr');
for(var i=0;i<columns.length;i++){
var the = document.createElement('th');
the.textContent = columns[i].columnid;
tre.appendChild(the);
}
thead.appendChild(tre);
}

var tbody = document.createElement('tbody');
tbe.appendChild(tbody);
for(var j=0;j<data.length;j++){
var tre = document.createElement('tr');
for(var i=0;i<columns.length;i++){
var the = document.createElement('td');
the.textContent = data[j][columns[i].columnid];
tre.appendChild(the);
}
tbody.appendChild(tre);
};
alasql.utils.emptyChildren(sel);
console.log(tbe,columns);
sel.appendChild(tbe);
};

alasql.into.JSON = function(filename, opts, data, columns, cb) {
var s = JSON.stringify(data);
alasql.utils.saveFile(filename,s);
Expand Down
42 changes: 42 additions & 0 deletions src/84from.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@
//
*/

alasql.from.HTML = function(selector, opts, cb, idx, query) {
var opt = {};
alasql.utils.extend(opt, opts);

var sel = document.querySelector(selector);
if(!sel && sel.tagName != "TABLE") {
throw new Error('Selected HTML element is not TABLE');
};

var res = [];
var headers = opt.headers;

if(headers && !(headers instanceof Array)) {
headers = [];
var ths = sel.querySelector("thead tr").childNodes;
for(var i=0;i<ths.length;i++){
headers.push(ths.item(i).textContent);
}
}
// console.log(headers);

var trs = sel.querySelectorAll("tbody tr");

for(var j=0;j<trs.length;j++) {
var tds = trs.item(j).childNodes;
var r = {};
for(var i=0;i<tds.length;i++){
if(headers) {
r[headers[i]] = tds.item(i).textContent;
} else {
r[i] = tds.item(i).textContent;
// console.log(r);
}
}
res.push(r);
}
//console.log(res);
if(cb) res = cb(res, idx, query);
return res;
}


alasql.from.RANGE = function(start, finish, cb, idx, query) {
var res = [];
for(i=start;i<=finish;i++) res.push(i);
Expand Down
24 changes: 24 additions & 0 deletions test/test188.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if(typeof exports === 'object') {
var assert = require("assert");
var alasql = require('../alasql.js');
} else {
__dirname = '.';
};

//if(typeof exports != 'object') {

describe('Test 188 - Calculation of PI', function() {
it("1. EMPTY", function(done) {
// var n = 10;
// var res = alasql('SELECT COUNT(*) as cnt, VALUE AGGR(cnt/$[0]*4) as pi FROM (SELECT random() as x, random() as y FROM RANGE(1,$[0])) WHERE x*x+y*y<1',
// [n]);
// console.log(res);

//var res = alasql('SELECT COUNT(*) as cnt, AGGR(cnt/$[0]*4) as pi, random() as x, random() as y FROM RANGE(1,$[0]) WHERE x*x+y*y<1',
// [n]);
// var res = alasql('SELECT * FROM (SELECT random() AS x, random() AS y FROM RANGE(1,10)) WHERE x*x+y*y<1',[n]);
// console.log(res);
done();
});

});
Loading

0 comments on commit 3d33ae8

Please sign in to comment.