Skip to content

Commit 843703a

Browse files
committed
🐛 Fix bugs and add demo animation
1 parent 804960a commit 843703a

4 files changed

Lines changed: 107 additions & 2 deletions

File tree

images/demo.gif

488 KB
Loading

images/demo.tape

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ Enter
77
Type "Clear-Host"
88
Enter
99
Show
10-
Type "#TODO"
10+
Type `'{"":3.14}' |Select-Json /`
11+
Enter
12+
Sleep 2s
13+
Type `'{"a":{"b":true}}' |Select-Json /a/*`
14+
Enter
15+
Sleep 2s
16+
Type "'[1, 2, 3]' |Set-Json /1 0"
17+
Enter
18+
Sleep 2s
19+
Type `'{"list":[1, 2]}' |Set-Json /list/- 3`
20+
Enter
21+
Sleep 2s
22+
Type "'{a:1}' |Resolve-JsonPointer /*"
23+
Enter
24+
Sleep 2s
25+
Type `'{"a":1,"b":{"u":3},"c":{"v":5}}','{"a":{"w":8},"b":2,"c":{"x":6}}' |Merge-Json`
26+
Enter
27+
Sleep 2s
28+
Type `'{d:{a:{b:1,c:{"$ref":"#/d/two"}},two:2}}' |Export-Json /d/a`
1129
Enter
1230
Sleep 9s

src/private/Merge-PSObject.ps1

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<#
2+
.SYNOPSIS
3+
Create a new PSObject by recursively combining the properties of PSObjects.
4+
5+
.INPUTS
6+
System.Management.Automation.PSObject to combine.
7+
8+
.OUTPUTS
9+
System.Management.Automation.PSObject combining the inputs.
10+
11+
.FUNCTIONALITY
12+
PowerShell
13+
14+
.LINK
15+
Get-Member
16+
17+
.LINK
18+
Add-Member
19+
20+
.EXAMPLE
21+
Merge-PSObject ([pscustomobject]@{a=1;b=2}) ([pscustomobject]@{b=0;c=3})
22+
23+
a b c
24+
- - -
25+
1 2 3
26+
27+
.EXAMPLE
28+
Merge-PSObject ([pscustomobject]@{a=1;b=2}) ([pscustomobject]@{b=0;c=3}) -Force
29+
30+
a b c
31+
- - -
32+
1 0 3
33+
34+
.EXAMPLE
35+
'{"a":1,"b":{"u":3},"c":{"v":5}}','{"a":{"w":8},"b":2,"c":{"x":6}}' |ConvertFrom-Json |Merge-PSObject -Accumulate -Force |select -Last 1 |ConvertTo-Json
36+
37+
{
38+
"a": {
39+
"w": 8
40+
},
41+
"b": 2,
42+
"c": {
43+
"v": 5,
44+
"x": 6
45+
}
46+
}
47+
#>
48+
49+
[CmdletBinding()][OutputType([PSObject])] Param(
50+
# Initial PSObject to combine.
51+
[Parameter(Position=0)][PSObject] $ReferenceObject = [pscustomobject]@{},
52+
<#
53+
PSObjects to combine. PSObject descendant properties are recursively merged.
54+
Primitive values are overwritten by any matching ones in the new PSObject.
55+
#>
56+
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true)][PSObject] $InputObject,
57+
# Continue merging each pipeline object's properties into the same accumulator object.
58+
[switch] $Accumulate,
59+
# Overwrite existing properties.
60+
[switch] $Force
61+
)
62+
Begin {if($Accumulate) {$value = $ReferenceObject.PSObject.Copy()}}
63+
Process
64+
{
65+
if(!$Accumulate) {$value = $ReferenceObject.PSObject.Copy()}
66+
foreach($p in $InputObject |Get-Member -Type Properties)
67+
{
68+
$name,$type = $p.Name,$p.MemberType
69+
$newvalue = $InputObject.$name
70+
if(!($value |Get-Member $name -Type $type))
71+
{
72+
$value |Add-Member $name -Type $type -Value $newvalue
73+
}
74+
elseif($Force)
75+
{
76+
$currentvalue = $value.$name
77+
$value.$name =
78+
if($currentvalue -isnot [PSObject] -or $newvalue -isnot [PSObject]) {$newvalue}
79+
else {Merge-PSObject $currentvalue $newvalue}
80+
}
81+
elseif($value.$name -is [PSObject] -and $newvalue -is [PSObject])
82+
{
83+
$value.$name = Merge-PSObject $value.$name $newvalue
84+
}
85+
}
86+
return $value
87+
}

src/public/Merge-Json.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ Primitive values are overwritten by any matching ones in the new JSON string.
4343
)
4444
Begin {$value = [pscustomobject]@{}}
4545
#TODO: Add or replace dependency.
46-
Process {$value = $value,($InputObject |ConvertFrom-Json) |Merge-PSObject.ps1}
46+
Process {$value = $value,($InputObject |ConvertFrom-Json) |Merge-PSObject}
4747
End {$value |ConvertTo-Json -Compress:$Compress}

0 commit comments

Comments
 (0)