Skip to content

Commit 383c92a

Browse files
committed
optimize and format
merged writePropertyName in to writeProperty change writeStringValue from function to filter
1 parent 486f6b3 commit 383c92a

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

ConvertTo-CSON.ps1

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,11 @@ function ConvertTo-Cson {
6969
}
7070
}
7171

72-
function writeStringValue ([string]$value) {
72+
filter writeStringValue {
7373
# write an escaped CSON string property value
7474
# the purpose of making this a function, is a single place to change the escaping function used
7575
# TODO: escape more characters!
76-
"""$($value -replace '([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)', $escape_replacer)"""
77-
}
78-
79-
function writePropertyName ([string]$value, [bool]$isArray) {
80-
# write an property name, processed as required for CSON
81-
# the purpose of making this a function, is a single place to change the escaping function used
82-
"$(
83-
# if a property name is not all simple characters or start with numeric digit, it must be quoted and escaped
84-
if ($value -match '[^\p{L}\d_]|^\d') {
85-
# property name requires escaping
86-
writeStringValue $value
87-
}
88-
else {
89-
$value
90-
}
91-
):$(
92-
if ($isArray) {
93-
' ['
94-
}
95-
)"
76+
"""$($_ -replace '([\x00-\x1F\x85\u2028\u2029])|([\\"]|#\{)', $escape_replacer)"""
9677
}
9778

9879
function writeProperty ([string]$name, $item, [string]$indention, [int32]$level) {
@@ -104,7 +85,7 @@ function ConvertTo-Cson {
10485
"$indention$(
10586
if (($item -is [string]) -or ($item -is [char])) {
10687
# handle strings or characters
107-
writeStringValue $item
88+
$item | writeStringValue
10889
}
10990
else {
11091
if ($item -is [boolean]) {
@@ -120,7 +101,7 @@ function ConvertTo-Cson {
120101
$item
121102
}
122103
elseif ($EnumsAsStrings) {
123-
writeStringValue ($item.ToString())
104+
$item.ToString() | writeStringValue
124105
}
125106
else {
126107
$item.value__
@@ -133,9 +114,19 @@ function ConvertTo-Cson {
133114
# write out key name, if one was supplied from the parent object
134115
if ($name) {
135116
"$indention$(
136-
writePropertyName $name ($item -is [array])
137-
)$(
138-
if ($item -is [ValueType] -or $item -is [string]) {
117+
# if a property name is not all simple characters or start with numeric digit, it must be quoted and escaped
118+
if ($name -match '[^\p{L}\d_]|^\d') {
119+
# property name requires escaping
120+
$name | writeStringValue
121+
}
122+
else {
123+
$name
124+
}
125+
):$(
126+
if ($item -is [array]) {
127+
' ['
128+
}
129+
elseif ($item -is [ValueType] -or $item -is [string]) {
139130
" $(writeValue $item '')"
140131
}
141132
)"
@@ -173,10 +164,10 @@ function ConvertTo-Cson {
173164
}
174165
else {
175166
# exceeded maximum depth, convert object to string
176-
"$indention$(writeStringValue $item)"
167+
"$indention$($item | writeStringValue)"
177168
}
178169
}
179170

180171
# start writing the property list, the property list should be an object, has no name, and starts at base level
181-
(writeProperty $null $InputObject '' (-1)) -join $(if (-not $IsCoreCLR -or $IsWindows) {"`r`n"} else {"`n"})
172+
(writeProperty $null $InputObject '' (-1)) -join $(if (-not $IsCoreCLR -or $IsWindows) { "`r`n" } else { "`n" })
182173
}

0 commit comments

Comments
 (0)