Skip to content

Commit

Permalink
feat: Added SingleLineString
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Mar 19, 2024
1 parent 56f0e0e commit d720c7c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
60 changes: 60 additions & 0 deletions ValueTransformers-Test/SingleLineString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2021 DigiDNA - www.imazing.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ValueTransformers_Test
{
[ TestClass ]
public class SingleLineString
{
private readonly ValueTransformers.SingleLineString Converter = new ValueTransformers.SingleLineString();

[ TestMethod ]
public void TestNull()
{
Assert.IsNull( this.Converter.Convert( null, typeof( string ), null, null ) );
}

[ TestMethod ]
public void TestEmptyString()
{
Assert.AreEqual( this.Converter.Convert( "", typeof( string ), null, null ), "" );
}

[ TestMethod ]
public void TestString()
{
Assert.AreEqual( this.Converter.Convert( "hello, world", typeof( string ), null, null ), "hello, world" );
Assert.AreEqual( this.Converter.Convert( "hello, world\nhello universe", typeof( string ), null, null ), "hello, world" );
}

[ TestMethod ]
public void TestNonString()
{
Assert.IsNull( this.Converter.Convert( 42, typeof( string ), null, null ) );
}
}
}
59 changes: 59 additions & 0 deletions ValueTransformers/Converters/SingleLineString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2021 DigiDNA - www.imazing.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

using System;
using System.Windows.Data;
using System.Windows.Markup;

namespace ValueTransformers
{
[ MarkupExtensionReturnType( typeof( IValueConverter ) ) ]
[ ValueConversion( typeof( object ), typeof( string ) ) ]
public class SingleLineString: MarkupExtension, IValueConverter
{
public object? Convert( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture )
{
if( !( value is string str ) )
{
return null;
}

string[] lines = str.Split( '\n' );

return ( lines.Length > 0 ) ? lines[ 0 ] : null;
}

public object? ConvertBack( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture )
{
throw new NotSupportedException();
}

private static readonly Lazy< SingleLineString > Converter = new Lazy< SingleLineString >( () => new SingleLineString() );

public override object ProvideValue( IServiceProvider serviceProvider )
{
return Converter.Value;
}
}
}

0 comments on commit d720c7c

Please sign in to comment.