Skip to content

Commit 5b05d3a

Browse files
feat: Turtle.js.get_JavaScript ( Fixes #324, re #320 )
1 parent aaec26c commit 5b05d3a

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

Types/Turtle.js/ToString.ps1

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,5 @@ param()
1414

1515

1616

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 ''
17+
$javaScript = "$($this.JavaScript)"
18+
return $javaScript

Types/Turtle.js/get_JavaScript.ps1

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
$objectParts =
16+
17+
foreach ($javaScriptProperty in $this.psobject.properties | Sort-Object Name) {
18+
# We only want the .js properties
19+
if ($javaScriptProperty.Name -notmatch '\.js$') { continue }
20+
# If the property is a function, we need to handle it differently
21+
if ($javaScriptProperty.value -match '^function.+?\(') {
22+
# specificically, we need to remove the "function " prefix from the name
23+
$predicate, $extra = $javaScriptProperty.value -split '\(', 2
24+
# and then we need to reassemble it as a javascript method
25+
$functionName = $predicate -replace 'function\s{1,}'
26+
if ($functionName -match '^[gs]et_') {
27+
$getSet = $functionName -replace '_.+$'
28+
$propertyName = $functionName -replace '^[gs]et_'
29+
if ($getSet -eq 'get') {
30+
$extra = $extra -replace '^.{0,}\)\s{0,}'
31+
"$getSet ${propertyName}() $($extra)"
32+
} else {
33+
"$getSet ${propertyName}($($extra)"
34+
}
35+
36+
} else {
37+
"${functionName}:function ($extra"
38+
}
39+
40+
}
41+
else {
42+
# Otherwise, include it inline.
43+
$javaScriptProperty.value
44+
}
45+
46+
}
47+
# Since we are building a javascript object, we need to wrap everything in curly braces
48+
@("{
49+
"
50+
# Indentation does not matter to most machines, but people tend to appreciate it.
51+
" "
52+
($objectParts -join (',' + [Environment]::Newline + ' '))
53+
"}") -join ''

0 commit comments

Comments
 (0)