diff --git a/Pwsh/Types/StringCrumble-with-ScriptProperties.ps1 b/Pwsh/Types/StringCrumble-with-ScriptProperties.ps1 new file mode 100644 index 0000000..81fb1c0 --- /dev/null +++ b/Pwsh/Types/StringCrumble-with-ScriptProperties.ps1 @@ -0,0 +1,131 @@ +function String.GetCrumbs { + <# + .SYNOPSIS + Split a string into chunks.' + .EXAMPLE + GetStringCrumbs -InputText 'bat man 3.14 cat, n!-choose-r' -SplitBy '[ ]' + .EXAMPLE + [WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog', '\W+' ) + .EXAMPLE + $w1 = [WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog') + $w1.CrumbCount | Should -be 9 + $w1.String = 'foo bar! cat' + $w1.CrumbCount | Should -be 3 + $w1.String = 'foo bar' + $w1.CrumbCount | Should -be 2 + #> + [Alias( + 'GetStringCrumbs' + )] + param( + [string]$InputText, + + [ArgumentCompletions( + '\W+', + '\b', + '\s+', + '\s', + "'[ ]'", + "'[ ]+'" + + )] + [string]$SplitBy + ) + [WordCrumb]::new( $InputText, $SplitBy ) +} +class WordCrumb { + <# + .DESCRIPTION + Example of a pwsh class with getters/setters that mutate the state + modify properties 'c.String' or 'c.SplitBy' + #> + [string[]] + $Crumbs = @() + + [int] + $CrumbCount = 0 + + hidden [string] + $RawString + + [string] + $_SplitBy = '\W+' + + WordCrumb ( [string]$Text ) { + $This.RawString = $Text + $This.Crumbs = $Text -split $this._SplitBy + $this.CrumbCount = $This.Crumbs.Count + } + WordCrumb ( [string]$Text, [string]$SplitBy ) { + $This.RawString = $Text + $this.Crumbs = $Text -split $SplitBy + $this._SplitBy = $SplitBy + $this.CrumbCount = $This.Crumbs.Count + } + # rebuild + [void] Update () { # aka Recalculate() + # $this = [WordCrumb]::Parse( $This.RawString, $this._SplitBy ) + + $new = [WordCrumb]::Parse( $This.RawString, $this._SplitBy ) + $this.Crumbs = $New.Crumbs + $this.CrumbCount = $new.CrumbCount + $this.RawString = $new.RawString + $this._SplitBy = $new.SplitBy + } + static [WordCrumb] Parse( [string]$Text, [string]$SplitBy ) { + return [WordCrumb]::New( $Text, $SplitBy ) + } +} +$get_RawString = { + return $this.RawString +} +$set_RawString = { + param( [string]$NewText ) + $this.RawString = $NewText + $this.Update() +} + +$get_SplitBy = { + return $this._SplitBy +} +$set_SplitBy = { + param( [string]$SplitBy ) + $this._SplitBy = $SplitBy + $this.Update() +} + +$add_ScriptProperty = @{ + MemberType = 'ScriptProperty' + Force = $true + TypeName = 'WordCrumb' + # TypeConverter = '.' + # TypeAdapter = '.' + # TypeData = '' +} + +$updateTypeDataSplat = @{ + MemberName = 'String' + Value = $get_RawString + SecondValue = $set_RawString +} +Update-TypeData @updateTypeDataSplat @add_ScriptProperty + +$updateTypeDataSplat = @{ + MemberName = 'SplitBy' + Value = $get_SplitBy + SecondValue = $set_SplitBy +} +Update-TypeData @updateTypeDataSplat @add_ScriptProperty + +[WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog', '\W+' ) +[WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog') + +$w1 = [WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog') +$w1.CrumbCount | Should -be 9 +$w1.String = 'foo bar! cat' +$w1.CrumbCount | Should -be 3 +$w1.String = 'foo bar' +$w1.CrumbCount | Should -be 2 + +$w1.SplitBy = 'oo' +$w1.Crumbs | Should -BeExactly @('f', ' bar')