Skip to content

Instantly share code, notes, and snippets.

@Insolita
Last active October 20, 2023 13:49
Show Gist options
  • Save Insolita/17da02b24883115b8c13e1e21bca9d98 to your computer and use it in GitHub Desktop.
Save Insolita/17da02b24883115b8c13e1e21bca9d98 to your computer and use it in GitHub Desktop.
JetBrains table export in php-array format
function eachWithIdx(iterable, f) {
var i = iterable.iterator();
var idx = 0;
while (i.hasNext()) f(i.next(), idx++);
}
function mapEach(iterable, f) {
var vs = [];
eachWithIdx(iterable, function (i) {
vs.push(f(i));
});
return vs;
}
function escape(str) {
str = str.replaceAll("\t|\b|\\f", "");
str = com.intellij.openapi.util.text.StringUtil.escapeXml(str);
str = str.replaceAll("\\r|\\n|\\r\\n", "<br/>");
return str;
}
var NEWLINE = "\n",
ARROW = " => ",
QUOTE = "'",
IDENT = ' ',
LBRACKET = '[',
RBRACKET = ']',
COMMA = ',',
NOQUOTED = ['NULL', 'true', 'false'];
function quoted(val) {
if (NOQUOTED.indexOf(val) !== -1) {
return val;
}
return QUOTE + val + QUOTE;
}
function output() {
for (var i = 0; i < arguments.length; i++) {
OUT.append(arguments[i]);
}
}
output("<?php", NEWLINE,
"return ", LBRACKET, NEWLINE);
eachWithIdx(ROWS, function (row, i) {
output(IDENT, quoted(i), ARROW, LBRACKET, NEWLINE);
mapEach(COLUMNS, function (col) {
output(IDENT, IDENT, quoted(col.name()), ARROW, quoted(escape(FORMATTER.format(row, col))), COMMA, NEWLINE);
});
output(IDENT, RBRACKET, COMMA, NEWLINE);
});
output(RBRACKET, ";", NEWLINE);
@jtouza
Copy link

jtouza commented Apr 9, 2021

very useful, thanks!

@tekord
Copy link

tekord commented Dec 15, 2021

Thank you!

@avilalegra
Copy link

Useful. Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment