Skip to content

Instantly share code, notes, and snippets.

@westc
Last active Feb 14, 2022
Embed
What would you like to do?
getApexString() - Takes a string and returns the corresponding Apex (Salesforce) string. If string ends up being too long for one line it will be shown as a multiple line expression using String.join().
function getApexString(string, maxOneLinerLength=80) {
var normal = "'" + JSON.stringify(string).slice(1, -1).replace(/\\"/g, '"').replace(/'/g, "\\'") + "'";
return (normal.length > maxOneLinerLength && string.indexOf('\n') >= 0)
? "String.join(new String[] {\n\t" + normal.replace(/\\[^n]|(\\n)/g, (m, isNewline) => isNewline ? "',\n\t'" : m) + "\n}, '\\n')"
: normal;
}
{
// Where the JS code lies
"files": ["getApexString.js"],
// Name of the function responsable for transforming the input into the output.
"transform": "getApexString",
// Arguments that will be passed to the function by name.
"params": [
{
"name": "maxOneLinerLength",
"label": "Maximum Length of a One-liner",
"type": "number",
"min": 3,
"step": 1,
"value": 80
}
],
// Input setup
"input": { "language": "text" },
// Output setup
"output": { "language": "apex" },
// Type of YourJS IO-App
"type": "split"
}
@westc
Copy link
Author

westc commented Feb 14, 2022

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