-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new class with calculated properties example
- Loading branch information
Showing
1 changed file
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |