Skip to content

Commit 433198f

Browse files
authored
Merge pull request #8 from msftrncs/updates
Updates, Windows PowerShell compatibility
2 parents ff89f56 + 553506c commit 433198f

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

ConvertTo-CSON.ps1

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,33 @@ function ConvertTo-Cson {
4646
# $Indent is a string representing the indentation to use.
4747
# Typically use "`t" or " ".
4848

49-
# define a match evaluator for escaping characters
49+
# define a match evaluator delegate for escaping characters
5050
$escape_replacer = {
51-
if ($_.Groups[1].Success) {
51+
param(
52+
[Text.RegularExpressions.Match] $RepMatch
53+
)
54+
if ($RepMatch.Groups[1].Success) {
5255
# group 1, control characters
53-
switch ($_.Value[0]) {
56+
switch ($RepMatch.Value[0]) {
5457
<# appearing in order of expected frequency, from most frequent to least frequent #>
5558
([char]10) { '\n'; continue } # new line
5659
([char]9) { '\t'; continue } # tab
57-
([char]13) { '\r'; continue } # caridge return
60+
([char]13) { '\r'; continue } # carriage return
5861
([char]12) { '\f'; continue } # new form
59-
([char]8) { '\b'; continue } # bell
62+
([char]8) { '\b'; continue } # backspace
6063
default { '\u{0:X4}' -f [int16]$_ } # unicode escape all others
6164
}
62-
} elseif ($_.Groups[2].Success) {
65+
} elseif ($RepMatch.Groups[2].Success) {
6366
# group 2, items that need `\` escape
64-
"\$($_.Value)"
67+
"\$($RepMatch.Value)"
6568
}
6669
}
6770

6871
filter writeStringValue {
6972
# write an escaped CSON string property value
7073
# the purpose of making this a function, is a single place to change the escaping function used
7174
# TODO: escape more characters!
72-
"""$($_ -replace '([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)', $escape_replacer)"""
75+
"""$([regex]::Replace($_, '([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)', $escape_replacer))"""
7376
}
7477

7578
function writeObject ($item) {
@@ -86,7 +89,7 @@ function ConvertTo-Cson {
8689
$name
8790
}
8891
):$(
89-
if (($level -gt $Depth) -or ($null -eq $value) -or ($value -is [ValueType]) -or ($value -is [string])) {
92+
if (($level -gt $Depth) -or ($null -eq $value) -or ($value -is [valuetype]) -or ($value -is [string])) {
9093
" $(, $value | writeValue)" # comma forces $value to be treated as a whole object instead of being enumerated as subitems
9194
}
9295
elseif ($value -is [Collections.IList]) {
@@ -104,7 +107,7 @@ function ConvertTo-Cson {
104107
$level++ # level increases for arrays or objects
105108
$value | writeArray # write the nested array
106109
"$indention]" # nested array end token
107-
} elseif ($value -and ($value -isnot [ValueType]) -and ($value -isnot [string])) {
110+
} elseif ($value -and ($value -isnot [valuetype]) -and ($value -isnot [string])) {
108111
$indention = "$indention$Indent"
109112
writeObject $value # recurse the element to writeObject
110113
}
@@ -115,7 +118,7 @@ function ConvertTo-Cson {
115118
# write a object property or array element simple value
116119
if ($null -eq $_) {
117120
'null'
118-
} elseif (($_ -is [char]) -or ($EnumsAsStrings -and ($_ -is [enum])) -or ($_ -isnot [ValueType])) {
121+
} elseif (($_ -is [char]) -or ($EnumsAsStrings -and ($_ -is [enum])) -or ($_ -isnot [valuetype])) {
119122
# handle strings or characters, or objects exceeding the max depth
120123
"$_" | writeStringValue
121124
} elseif ($_ -is [boolean]) {
@@ -143,7 +146,7 @@ function ConvertTo-Cson {
143146
}
144147
process {
145148
# if depth not exceeded, check for a nested object
146-
if (($level -le $Depth) -and $_ -and ($_ -isnot [ValueType]) -and ($_ -isnot [string]) -and ($_ -isnot [Collections.IList])) {
149+
if (($level -le $Depth) -and $_ -and ($_ -isnot [valuetype]) -and ($_ -isnot [string]) -and ($_ -isnot [Collections.IList])) {
147150
# an object is nested within the array element
148151
if ($(if ($_ -is [Collections.IDictionary]) { $_.get_Keys().Count } else { @($_.psobject.get_Properties()).Count } ) -gt 0) {
149152
"$indentionArray{" # object start token
@@ -161,7 +164,7 @@ function ConvertTo-Cson {
161164
}
162165
}
163166

164-
if (($level -gt $Depth) -or ($null -eq $item) -or ($item -is [ValueType]) -or ($item -is [string])) {
167+
if (($level -gt $Depth) -or ($null -eq $item) -or ($item -is [valuetype]) -or ($item -is [string])) {
165168
"$indention$(, $item | writeValue)" # comma forces $item to be treated as a whole object instead of being enumerated as subitems
166169
} else {
167170
$level++ # level increases for arrays or objects
@@ -205,5 +208,5 @@ function ConvertTo-Cson {
205208
$InputObject # input from parameter argument
206209
}
207210
)
208-
) -join "$(if (-not $IsCoreCLR -or $IsWindows) { "`r" })`n"
211+
) -join "$(if (-not $local:IsCoreCLR -or $local:IsWindows) { "`r" })`n"
209212
}

0 commit comments

Comments
 (0)