From 161abe2220a0be92526b4423e57669d61d96fdbf Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 18 Dec 2022 11:38:41 +0100 Subject: [PATCH] `Test-IsNumericType`: Now handles arrays correctly (#90) --- CHANGELOG.md | 12 +++++++++- README.md | 24 +++++++++++++++---- build.yaml | 1 + source/Public/Test-IsNumericType.ps1 | 17 ++++++++++++- .../Unit/Public/Test-IsNumericType.Tests.ps1 | 20 ++++++++++++++++ 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81e46d8..7da7906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- DscResource.Common + - Now builds the module into a separate folder `output/builtModule`. + +### Fixed + +- `Test-IsNumericType` + - Now handles arrays correctly. + ## [0.13.0] - 2022-12-17 ### Added -- Added public function `Test-IsNumericType` that returns whether the specified +- Added public function `Test-IsNumericType` that returns whether the specified object is of a numeric type - [Issue #87](https://github.com/dsccommunity/DscResource.Common/issues/87) - Related to SqlServerDsc [Issue #1795](https://github.com/dsccommunity/SqlServerDsc/issues/1795). diff --git a/README.md b/README.md index 8278e63..a4a291a 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ the parameters `Parameter1` and `Parameter2`. Assert that the user has elevated the PowerShell session. -`Assert-ElevatedUser` will throw a statement-terminating error if the +`Assert-ElevatedUser` will throw a statement-terminating error if the script is not run from an elevated session. #### Syntax @@ -100,7 +100,7 @@ None. `Assert-ElevatedUser -ErrorAction 'Stop'` ``` -This example stops the entire script if it is not run from an +This example stops the entire script if it is not run from an elevated PowerShell session. ### `Assert-IPAddress` @@ -201,7 +201,7 @@ Compare-DscParameterState [-CurrentValues] [-DesiredValues] #### Outputs -Returns an array containing a psobject with metadata for each property +Returns an array containing a PSObject with metadata for each property that was evaluated. Metadata Name | Type | Description @@ -216,6 +216,7 @@ ActualValue | `[System.PsObject]` | Return the value of current object. #### Example ##### Example 1 + ```powershell $currentValues = @{ String = 'This is a string' @@ -947,6 +948,7 @@ if ((Test-IsNanoServer)) { ### `Test-IsNumericType` Returns whether the specified object is of a numeric type: + - [System.Byte] - [System.Int16] - [System.Int32] @@ -973,10 +975,22 @@ Test-IsNumericType [[-Object] ] [] #### Example -```PowerShell +```powershell Test-IsNumericType -Object ([System.UInt32] 3) ``` -```PowerShell + +Returns `$true` since the value is a numeric type. + +```powershell ([System.String] 'a') | Test-IsNumericType ``` + +Returns `$false` since the value is not a numeric type. + +```powershell +('a', 2, 'b') | Test-IsNumericType +``` + +Returns `$true` since one of the values passed in the pipeline is of a +numeric type. diff --git a/build.yaml b/build.yaml index 12c63c0..c954a63 100644 --- a/build.yaml +++ b/build.yaml @@ -8,6 +8,7 @@ prefix: prefix.ps1 suffix: suffix.ps1 Encoding: UTF8 VersionedOutputDirectory: true +BuiltModuleSubdirectory: builtModule #################################################### # Sampler Pipeline Configuration # diff --git a/source/Public/Test-IsNumericType.ps1 b/source/Public/Test-IsNumericType.ps1 index 50e5c50..68793c2 100644 --- a/source/Public/Test-IsNumericType.ps1 +++ b/source/Public/Test-IsNumericType.ps1 @@ -13,8 +13,17 @@ Returns $true since the object passed is of a numeric type. + .EXAMPLE + ('a', 2, 'b') | Test-IsNumericType + + Returns $true since one of the values in the array is of a numeric type. + .OUTPUTS [System.Boolean] + + .NOTES + When passing in an array of values from the pipeline, the command will return + $true if any of the values in the array is numeric. #> function Test-IsNumericType { @@ -27,10 +36,13 @@ function Test-IsNumericType $Object ) - process + begin { $isNumeric = $false + } + process + { if ( $Object -is [System.Byte] -or $Object -is [System.Int16] -or @@ -47,7 +59,10 @@ function Test-IsNumericType { $isNumeric = $true } + } + end + { return $isNumeric } } diff --git a/tests/Unit/Public/Test-IsNumericType.Tests.ps1 b/tests/Unit/Public/Test-IsNumericType.Tests.ps1 index bd0fc43..06dbf38 100644 --- a/tests/Unit/Public/Test-IsNumericType.Tests.ps1 +++ b/tests/Unit/Public/Test-IsNumericType.Tests.ps1 @@ -85,5 +85,25 @@ Describe 'Test-IsNumericType' -Tag 'Public' { } } } + + Context 'When type is an array with no numeric values' { + It 'Should return the correct value' { + InModuleScope -ScriptBlock { + $result = ('a', 'b') | Test-IsNumericType + + $result | Should -BeFalse + } + } + } + + Context 'When type is an array with a numeric value' { + It 'Should return the correct value' { + InModuleScope -ScriptBlock { + $result = ('a', 1, 'b') | Test-IsNumericType + + $result | Should -BeTrue + } + } + } } }