Skip to content

Commit

Permalink
refactor(Typed): override Equals method (#5293)
Browse files Browse the repository at this point in the history
* refactor: 重构代码

* test: 更新单元测试

* refactor: 更新代码
  • Loading branch information
ArgoZhang authored Feb 3, 2025
1 parent a2756ed commit 95711bb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 21 deletions.
26 changes: 7 additions & 19 deletions src/BootstrapBlazor/Components/Typed/Typed.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public partial class Typed
[Parameter]
public Func<Task>? OnCompleteAsync { get; set; }

private string? _lastOptions;

private string? _text;

private TypedOptions? _options;

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand All @@ -43,8 +43,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

if (firstRender)
{
_lastOptions = Options?.ToString();
_text = Text;
_options = Options;
}
else if (UpdateParameters())
{
Expand Down Expand Up @@ -82,24 +82,12 @@ private bool UpdateParameters()
return true;
}

var optionString = GetOptionsString();
if (string.Equals(optionString, _lastOptions, StringComparison.Ordinal))
if (Options?.Equals(_options) ?? false)
{
return false;
}

_lastOptions = optionString;
return true;
}

private string? GetOptionsString()
{
if (Options == null)
{
return null;
return true;
}

var textString = Options.Text == null ? "" : string.Join(",", Options.Text);
return $"{Options} {textString}";
_options = Options;
return false;
}
}
67 changes: 65 additions & 2 deletions src/BootstrapBlazor/Components/Typed/TypedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace BootstrapBlazor.Components;
/// <summary>
/// TypedJs 组件配置类
/// </summary>
public record TypedOptions
public class TypedOptions : IEquatable<TypedOptions>
{
/// <summary>
/// 获得/设置 要打字的字符串数组
/// </summary>
[JsonPropertyName("strings")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<string?>? Text { get; set; }
public List<string>? Text { get; set; }

/// <summary>
/// 获得/设置 打字速度 默认 null 未设置 单位毫秒
Expand Down Expand Up @@ -88,4 +88,67 @@ public record TypedOptions
[JsonPropertyName("contentType")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ContentType { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="option"></param>
/// <returns></returns>
public bool Equals(TypedOptions? option)
{
if (option == null)
{
return false;
}

return EqualText(option.Text) &&
TypeSpeed == option.TypeSpeed &&
BackSpeed == option.BackSpeed &&
SmartBackspace == option.SmartBackspace &&
Shuffle == option.Shuffle &&
BackDelay == option.BackDelay &&
Loop == option.Loop &&
LoopCount == option.LoopCount &&
ShowCursor == option.ShowCursor &&
CursorChar == option.CursorChar &&
ContentType == option.ContentType;
}

private bool EqualText(List<string>? text)
{
if (Text == null && text == null)
{
return true;
}
if (Text == null || text == null)
{
return false;
}
if (Text.Count != text.Count)
{
return false;
}
return Text.SequenceEqual(text);
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object? obj)
{
if (obj is TypedOptions option)
{
return Equals(option);
}

return false;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public override int GetHashCode() => base.GetHashCode();
}
17 changes: 17 additions & 0 deletions test/UnitTest/Components/TypedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,21 @@ public void TypedOptions_Ok()
Assert.Equal("|", options.CursorChar);
Assert.Equal("html", options.ContentType);
}

[Fact]
public void Equal_Ok()
{
var options = new TypedOptions() { Text = ["test1", "test2"], TypeSpeed = 70 };
Assert.False(options.Equals(null));

var options2 = new TypedOptions() { Text = ["test1", "test2", "test3"], TypeSpeed = 70 };
Assert.False(options.Equals(options2));

var options3 = new TypedOptions() { Text = ["test1", "test2"], TypeSpeed = 70 };
Assert.True(options.Equals(options3));

Assert.True(options.Equals((object)options3));
Assert.False(options.Equals(new object()));
Assert.True(options.GetHashCode() > 0);
}
}

0 comments on commit 95711bb

Please sign in to comment.