Skip to content

Instantly share code, notes, and snippets.

@halaxa
Created December 16, 2016 07:59
Show Gist options
  • Save halaxa/7e20deb9c3e34ffb2b86fca555ea589e to your computer and use it in GitHub Desktop.
Save halaxa/7e20deb9c3e34ffb2b86fca555ea589e to your computer and use it in GitHub Desktop.
JSON data extractor in JavaScript for JetBrains IntelliJ platform
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
/*
* Based on HTML-JavaScript.html.js data extractor
*/
'use strict';
var NEWLINE = "\n";
function eachWithIdx(iterable, f) { var i = iterable.iterator(); var idx = 0; while (i.hasNext()) f(i.next(), idx++); }
function output() { for (var i = 0; i < arguments.length; i++) { OUT.append(arguments[i]); } }
output('[');
eachWithIdx(ROWS, function (row, i) {
output(i ? ',':'', NEWLINE, ' {');
eachWithIdx(COLUMNS, function (col, j) {
var value = FORMATTER.format(row, col);
switch (true) {
case value.toUpperCase() == 'NULL':
value = null;
break;
case parseInt(value).toString() == value:
value = parseInt(value);
break;
case parseFloat(value).toString() == value:
value = parseFloat(value);
break;
}
output(j ? ',':'', NEWLINE, ' ', JSON.stringify(col.name()), ':', JSON.stringify(value));
});
output(NEWLINE, ' }');
});
output(NEWLINE, ']');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment