Skip to content

Commit

Permalink
Merge pull request #2 from desmondinho/feat/merge-nullable
Browse files Browse the repository at this point in the history
feat: improve nullability support
  • Loading branch information
desmondinho authored Jun 11, 2024
2 parents cbc13a6 + 458d94d commit 9a6b62c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/TwMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ public TwMerge()
/// </summary>
/// <param name="classNames">The collection of CSS classes to be merged.</param>
/// <returns>A <see langword="string"/> of merged CSS classes.</returns>
public string Merge( params string[] classNames )
public string? Merge( params string?[] classNames )
{
var joinedClassNames = string.Join( ' ', classNames );

if( string.IsNullOrEmpty( joinedClassNames ) )
{
return null;
}

if( _cache.TryGetValue( joinedClassNames, out var cachedResult ) )
{
return cachedResult;
Expand All @@ -54,7 +59,7 @@ private string Merge( string classList )

var filteredClassNames = FilterConflictingClasses( classes );

return string.Join( " ", filteredClassNames );
return string.Join( ' ', filteredClassNames );
}

private ClassInfo GetClassInfo( string className )
Expand Down
2 changes: 1 addition & 1 deletion src/TwMergeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ private static readonly (string[] Items, Func<string, bool>[] Validators) _blur
* Grayscale
* See https://tailwindcss.com/docs/grayscale
*/
new ClassGroup( "grayscale", "grayscale", _zeroAndEmpty ),
new ClassGroup( "grayscale", "grayscale", _zeroAndEmpty, _arbitraryValue ),
/*
* Hue Rotate
* See https://tailwindcss.com/docs/hue-rotate
Expand Down
42 changes: 42 additions & 0 deletions tests/TwMergeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace TailwindMerge.Tests;

public class TwMergeTests
{
[Theory]
[InlineData( "mix-blend-normal mix-blend-multiply", "mix-blend-multiply" )]
[InlineData( "h-10 h-min", "h-min" )]
[InlineData( "stroke-black stroke-1", "stroke-black stroke-1" )]
[InlineData( "stroke-2 stroke-[3]", "stroke-[3]" )]
[InlineData( "outline-black outline-1", "outline-black outline-1" )]
[InlineData( "grayscale-0 grayscale-[50%]", "grayscale-[50%]" )]
[InlineData( "grow grow-[2]", "grow-[2]" )]
[InlineData( "", null )]
[InlineData( null, null )]
public void Merge_MergesCorrectly( string classList, string expected )
{
// Act
var actual = new TwMerge().Merge( classList );

// Assert
Assert.Equal( expected, actual );
}

[Fact]
public void Merge_SameValues_ReturnsCachedResult()
{
// Arrange
var twMerge = new TwMerge();

// Act
var actual = twMerge.Merge( "h-10 h-min" );

// Assert
Assert.Equal( "h-min", actual );

// Act
actual = twMerge.Merge( "h-10 h-min" );

// Assert
Assert.Equal( "h-min", actual );
}
}

0 comments on commit 9a6b62c

Please sign in to comment.