Skip to content

Commit

Permalink
Improve localeCompare implementation (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored May 9, 2024
1 parent 147cb14 commit 9839339
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Jint.Tests/Runtime/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public void TemplateLiteralAsObjectKey()
Assert.Equal("value", result["key"]);
}

[Fact]
public void ShouldCompareWithLocale()
{
var engine = new Engine();
Assert.Equal(1, engine.Evaluate("'王五'.localeCompare('张三')").AsInteger());
Assert.Equal(-1, engine.Evaluate("'王五'.localeCompare('张三', 'zh-CN')").AsInteger());
}

public static TheoryData GetLithuaniaTestsData()
{
return new StringTetsLithuaniaData().TestData();
Expand Down
9 changes: 8 additions & 1 deletion Jint/Native/String/StringPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,14 @@ private JsValue LocaleCompare(JsValue thisObject, JsValue[] arguments)
var s = TypeConverter.ToString(thisObject);
var that = TypeConverter.ToString(arguments.At(0));

return string.CompareOrdinal(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD));
var culture = Engine.Options.Culture;

if (arguments.Length > 1 && arguments[1].IsString())
{
culture = CultureInfo.GetCultureInfo(arguments.At(1).AsString());
}

return culture.CompareInfo.Compare(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD));
}

/// <summary>
Expand Down

0 comments on commit 9839339

Please sign in to comment.