KBEC-00045 - Common JavaScript property syntax examples for avoiding an accidental regular expression reference

Article ID:360033194791
3 minute readKnowledge base

Description

When referencing properties, syntax is often an issue, particularly when creating JavaScript code. Often there are syntax alternatives that get the same result while avoiding some unpleasant syntax side effects. This article provides examples.

Solution

If you use the property reference, the value of the property appears in the step’s Full Details. Only the result of the of the JavaScript is shown, which means the JavaScript is more difficult to debug if there are problems.

Examples

As of ElectricCommander v3.0, JavaScript is read-only, and only API object reference access is supported.

Property syntax

Below are examples showing a property reference in a Perl print statement. The properties are replaced during property expansion. First, a property expansion reference, second, the JavaScript form of a property reference, third, a property expansion reference in a JavaScript condition, fourth, a Javascript form of a property reference in a JavaScript condition.

print "show $[/myResource/hostName] \n";
print "show $[/javascript myResource.hostName ] \n";
print "show $[/javascript if ('$[/myResource/hostName]' == 'localhost') 'echo true'] \n";
print "show $[/javascript if (myResource.hostName == 'localhost') 'echo true'] \n";

The log file output from the ElectricCommander step above is:

show localhost
show localhost
show echo true
show echo true

Job details displays:

print "show localhost \n";
print "show localhost \n";
print "show echo true \n";
print "show echo true \n";
JavaScript and regular expressions and properties

In JavaScript, the slash character has special meaning. The following incorrect example shows how to form more complex JavaScript property references using a property steps_to_skip.

$[/javascript ! myCall.steps_to_skip.match(/myStep.stepName/) // string as literals inside a regular expression ]

The above uses the / character to define a regular expression. The problem is that the text inside the slashes is treated as a string literal, not as a symbol to be evaluated.

Another incorrect way to write the example above:

$[/javascript ! myCall.steps_to_skip.match(new RegExp("myStep.stepName")) // string as literals inside RegExp function ]

Note that here it is clear that myStep.stepName is being treated as a string literal.

The correct way to write the run condition is:

$[/javascript ! myCall.steps_to_skip.match(new RegExp(myStep.stepName)) // string passed to RegExp function ]

The above JavaScript syntax creates a regular expression using the evaluated step name contents as expected.

JavaScript and dashes and properties

In JavaScript, the dash character can mean minus or it may be in a name. The following incorrect example shows how to reference a property name savedResourceName with a minus sign as the value of the property, say foo-bar.

The correct way to write the run condition is:

$[/javascript if(typeof(getProperty("/resources/$[savedResourceName]/BuildServer"))==null)  // string is evaluated as name]

The above JavaScript syntax prevents the property from being reevaluated by JavaScript and the dash remains a dash.

JavaScript and brackets and dot notation

In JavaScript, you can use brackets and dot notation to avoid using path names to properties:

my $lastP4Change = '$[/javascript var netpath = projects["SAND_BOX"].procedures["Archive Artifacts"].netpath; netpath;]';
print "$lastP4Change\n";

Is the equivalent of the slash notation using getProperty:

my $lastP4Change = '$[/javascript var netpath = getProperty("/projects/SAND_BOX/procedures/Archive Artifacts/netpath"); netpath;]';
print "$lastP4Change\n";
Using JavaScript to determine if the step is called as a procedure or subprocedure

myParent is generally only useful when called from a subprocedure. You can use JavaScript to filter actions to run only in subprocedures with the following syntax:

$[/javascript if (myParent.procedure != null) 'echo my subprocdure name is $[/javascript myParent.subprocedure]']
Using JavaScript to reference objects by Id

The Javascript 'objects' array is indexed by entity references (e.g. "project-1", "job-2", "workflow-3"). It returns the corresponding object, which you can then use to fetch intrinsic or custom properties by name (e.g. projectName). To reference the name of the object after you have the Id, use the following syntax:

$[/javascript objects["job-1"].jobName]
$[/javascript objects["workflow-1"].workflowName]
$[/javascript objects["project-1"].projectName]