forked from PoshWeb/Turtle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_JavaScript.ps1
More file actions
70 lines (62 loc) · 2.44 KB
/
get_JavaScript.ps1
File metadata and controls
70 lines (62 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
.SYNOPSIS
`Turtle.js` definition
.DESCRIPTION
Our JavaScipt turtle is actually contained in a PowerShell object first.
This object has a number of properties ending with `.js`.
These are portions of the class.
To create our class, we simply join these properties together, and output a javascript object.
.NOTES
This is an experimental feature and is subject to change and improvement.
.EXAMPLE
$turtleJs = [PSCustomObject]@{PSTypeName='Turtle.js'}
$html = @(
"<html><body>"
"<svg id='output' width='100%' height='100%'>"
"<path id='outputPath' stroke='#4488ff' />"
"</svg>"
"<script>"
"const turtle = $turtleJS"
"turtle.go('ROTATE', 45,'forward', 42)"
"document.getElementById('outputPath').setAttribute('d',turtle.pathData)"
"document.getElementById('output').setAttribute('viewBox', ``0 0 `${turtle.width} `${turtle.height}`` )"
"</script>"
"</body></html>"
) > ./TurtleTest.html
#>
param()
$objectParts =
foreach ($javaScriptProperty in $this.psobject.properties | Sort-Object Name) {
# We only want the .js properties
if ($javaScriptProperty.Name -notmatch '\.js$') { continue }
# If the property is a function, we need to handle it differently
if ($javaScriptProperty.value -match '^function.+?\(') {
# specificically, we need to remove the "function " prefix from the name
$predicate, $extra = $javaScriptProperty.value -split '\(', 2
# and then we need to reassemble it as a javascript method
$functionName = $predicate -replace 'function\s{1,}'
if ($functionName -match '^[gs]et_') {
$getSet = $functionName -replace '_.+$'
$propertyName = $functionName -replace '^[gs]et_'
if ($getSet -eq 'get') {
$extra = $extra -replace '^.{0,}\)\s{0,}'
"$getSet ${propertyName}() $($extra)"
} else {
"$getSet ${propertyName}($($extra)"
}
} else {
"${functionName}:function ($extra"
}
}
else {
# Otherwise, include it inline.
$javaScriptProperty.value
}
}
# Since we are building a javascript object, we need to wrap everything in curly braces
@("{
"
# Indentation does not matter to most machines, but people tend to appreciate it.
" "
($objectParts -join (',' + [Environment]::Newline + ' '))
"}") -join ''