Last active
October 20, 2023 13:49
-
-
Save Insolita/17da02b24883115b8c13e1e21bca9d98 to your computer and use it in GitHub Desktop.
JetBrains table export in php-array format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful. Thank you !