Skip to content

Commit 5e2c994

Browse files
author
James Brundage
committed
feat: Turtle.js.ToString() ( Fixes PoshWeb#320 )
1 parent 62f1d3a commit 5e2c994

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Types/Turtle.js/ToString.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<#
2+
.SYNOPSIS
3+
`Turtle.js` definition
4+
.DESCRIPTION
5+
Our JavaScipt turtle is actually contained in a PowerShell object first.
6+
7+
This object has a number of properties ending with `.js`.
8+
9+
These are portions of the class.
10+
11+
To create our class, we simply join these properties together, and output a javascript object.
12+
#>
13+
param()
14+
15+
16+
17+
@(
18+
19+
# Since we are building a javascript object, we need to wrap everything in curly braces
20+
"{
21+
"
22+
# Indentation does not matter to most machines, but people tend to appreciate it.
23+
" "
24+
@(foreach ($javaScriptProperty in $this.psobject.properties | Sort-Object Name) {
25+
# We only want the .js properties
26+
if ($javaScriptProperty.Name -notmatch '\.js$') { continue }
27+
# If the property is a function, we need to handle it differently
28+
if ($javaScriptProperty.value -match '^function.+?\(') {
29+
# specificically, we need to remove the "function " prefix from the name
30+
$predicate, $extra = $javaScriptProperty.value -split '\(', 2
31+
# and then we need to reassemble it as a javascript method
32+
$functionName = $predicate -replace 'function\s{1,}'
33+
"${functionName}:function ($extra"
34+
}
35+
else {
36+
# Otherwise, include it inline.
37+
$javaScriptProperty.value
38+
}
39+
40+
}) -join (',' + [Environment]::Newline + ' ')
41+
"}") -join ''

0 commit comments

Comments
 (0)