Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -90,7 +90,7 @@ public string Separator
/// Gets or sets the input object to join into text.
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { get; set; }
public PSObject[] InputObject { get; set; }

/// <inheritdoc />
protected override void BeginProcessing()
Expand All @@ -106,40 +106,46 @@ protected override void BeginProcessing()
/// <inheritdoc />
protected override void ProcessRecord()
{
if (InputObject != null && InputObject != AutomationNull.Value)
if (InputObject != null)
Comment thread
TravisEz13 marked this conversation as resolved.
Resolved
{
var inputValue = Property == null
? InputObject
: Property.GetValues(InputObject, false, true).FirstOrDefault()?.Result;

// conversion to string always succeeds.
if (!LanguagePrimitives.TryConvertTo<string>(inputValue, _cultureInfo, out var stringValue))
{
throw new PSInvalidCastException("InvalidCastFromAnyTypeToString", ExtendedTypeSystem.InvalidCastCannotRetrieveString, null);
}

if (_firstInputObject)
{
_firstInputObject = false;
}
else
{
_outputBuilder.Append(Separator);
}

if (_quoteChar != char.MinValue)
{
_outputBuilder.Append(_quoteChar);
_outputBuilder.Append(stringValue);
_outputBuilder.Append(_quoteChar);
}
else if (string.IsNullOrEmpty(FormatString))
{
_outputBuilder.Append(stringValue);
}
else
foreach (PSObject inputObject in InputObject)
{
_outputBuilder.AppendFormat(_cultureInfo, FormatString, inputValue);
if (inputObject != null && inputObject != AutomationNull.Value)
{
var inputValue = Property == null
? inputObject
: Property.GetValues(inputObject, false, true).FirstOrDefault()?.Result;

// conversion to string always succeeds.
if (!LanguagePrimitives.TryConvertTo<string>(inputValue, _cultureInfo, out var stringValue))
{
throw new PSInvalidCastException("InvalidCastFromAnyTypeToString", ExtendedTypeSystem.InvalidCastCannotRetrieveString, null);
}

if (_firstInputObject)
{
_firstInputObject = false;
}
else
{
_outputBuilder.Append(Separator);
}

if (_quoteChar != char.MinValue)
{
_outputBuilder.Append(_quoteChar);
_outputBuilder.Append(stringValue);
_outputBuilder.Append(_quoteChar);
}
else if (string.IsNullOrEmpty(FormatString))
{
_outputBuilder.Append(stringValue);
}
else
{
_outputBuilder.AppendFormat(_cultureInfo, FormatString, inputValue);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Describe "Join-String" -Tags "CI" {
{ Join-String -InputObject $testObject } | Should -Not -Throw
}

It "'Input | Join-String' should be equal to 'Join-String -InputObject Input'" {
$result1 = $testObject | Join-String
$result2 = Join-String -InputObject $testObject
$result1 | Should -BeExactly $result2
}

It "Should return a single string" {
$actual = $testObject | Join-String

Expand Down