Skip to content

Using JavaScript

perpil edited this page Apr 6, 2024 · 5 revisions

Usage

JavaScript expressions can be used to transform inputs, generate strings, manipulate content and access variables. Anything between ${} is interpolated as a Template string Everything between ${} must be on 1 line unless you use this approach.

Variables

Read about variables here

Functions

Aside from the standard JavaScript functions like String.toLowerCase() there are several built-in functions and a few libraries available.

Function Description Parameters Example
arrayify Converts a string or array into an array str || [str] arrayify("str") = ["str"]
arrayify(["str","str2"]) = ["str","str2"]
bashEscape Escapes a string for use between $'str' in Bash shells str "echo $'${bashEscape("It'sa me Mario")}'" = "echo $'It\'sa me Mario'"
cfn Get the value from a CloudFormation stack. Since it is async, you must call it with await. stack (await cfn(`speedrun-federation-${stage}`)).outputs.HostedZoneId=Z2OJLYMUO9EFXC
acfn Get the value from a CloudFormation stack. Wrapped version cfn so you don't need the await syntax. stack, pathToValue acfn(`speedrun-federation-${stage}`,'outputs.HostedZoneId')=Z2OJLYMUO9EFXC
dayjs The day.js library See docs dayjs().format() = "2022-08-17T15:02:17-07:00"
encodeCloudWatchInsightsParam Encodes a string for usages as a parameter in a Cloudwatch Insights url str encodeCloudWatchInsightsParam("/lambda/speedrun-federation") = "\*2flambda\*2fspeedrun-api"
encodeCloudWatchURL Encodes a string for usages as a parameter in a Cloudwatch url str encodeCloudWatchuRL("/lambda/speedrun-federation") = "$252Faws$252Flambda$252Fspeedrun-federation-dev"
firstGroup get the first matcher group of a regex regex, str firstGroup(/#(\w+)/,"#hashtag") = "hashtag"
firstNonNull Return the first value that isn't undefined or null ...str firstNonNull(null,undefined,"defined") = "defined"
includesSearch Return all elements that include str str, ["str","str2"] includesSearch("a",["ab","ba","cd"]) = ["ab","ba"]
JSON5 The JSON5 library See docs JSON5.parse('{name:"David"}') = {name:"David"}
lastLine Return the last line of str str lastLine('A long time ago\nIn a faraway land') = "In a faraway land"
nullSafe Returns a empty {} if o is undefined o nullSafe(undefined) = {}
nullSafe("str") = "str"
prefixSearch Return all elements that are prefixed with str str, ["str","str2"] includesSearch("a",["ab","ba","cd"]) = ["ab"]
prepend Return all elements prepended with str str, ["str1","str2",...] prepend('a',["b","c"]) = ["ab","ac"]
prettyJSON Formats json o, spaces=4 prettyJSON({region:"us-east-1", account:"10203456789"}) = {
  "region": "us-east-1",
  "account": "10203456789"
}
regexSearch Return all elements that are match regex regex, ["str","str2"] regexSearch(/a/,["ab","ba","cd"]) = ["ab","ba"]
srTimestamp Prompt user for recorded time interval and turn it into url form n/a srTimestamp() = "end~'2022-08-06T17:45:42.557Z~start~'2022-08-06T17:42:35.544Z~timeType~'ABSOLUTE~tz~'UTC"
stringify Converts o to a JavaScript escaped string o stringify({"region":"us-west-2"}) = "{\"region\":\"us-west-2\"}"
_ The lodash library See docs _.camelCase('Two Words') = "twoWords"

Writing your own functions

You can define your own functions in your srConfig. Functions must be prefixed with function: The body must be converted to a one line JSON string.

If there is only one argument, reference it with str.

"function:stripNewLines" : {
    "body" : "return str.replaceAll('\\n','')"
}

To define a function with multiple arguments specify args containing an array of their names:

"function:concatenate" : {
    "args" : ["str1","str2"],
    "body" : "return str1 + str2"
}

Multiline JavaScript

This is a sample of how to use multiline Javascript. You can use `` (Template string syntax) within a ${}. Click the code tab to see exact code.

#copy
~~~multiline=Multiline? {"type":"checkbox","suppress":"true"}~~~
${multiline?`This
is
some
beautiful multiline content`:'One line'}

Default values

If you want to use a default only if a variable isn't defined use this syntax: ${typeof start === 'undefined' ? '-3600' : start}

Clone this wiki locally