Skip to content

Commit

Permalink
[JS] Support string.normalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Oct 18, 2024
1 parent 573e28f commit 30488a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,34 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt
Helper.LibCall(com, "String", "concat", t, args, hasSpread = true, ?loc = r)
|> Some
| "CompareOrdinal", None, _ -> Helper.LibCall(com, "String", "compareOrdinal", t, args, ?loc = r) |> Some
| "Normalize", Some str, _ ->
match args with
| [] ->
Helper.InstanceCall(
str,
Naming.lowerFirst i.CompiledName,
t,
[ makeStrConst "NFC" ],
i.SignatureArgTypes,
genArgs = i.GenericArgs,
?loc = r
)
|> Some
| [ NormalizationFormEnumValue normalizationForm ] ->
Helper.InstanceCall(
str,
Naming.lowerFirst i.CompiledName,
t,
[ makeStrConst normalizationForm ],
i.SignatureArgTypes,
genArgs = i.GenericArgs,
?loc = r
)
|> Some
| _ ->
"Normalization expects an optional System.Text.NormalizationForm"
|> addErrorAndReturnNull com ctx.InlinePath r
|> Some
| Patterns.SetContains implementedStringFunctions, thisArg, args ->
Helper.LibCall(
com,
Expand Down
13 changes: 13 additions & 0 deletions src/Fable.Transforms/Transforms.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,19 @@ module AST =
Some()
| _ -> None

let (|NormalizationFormEnumValue|_|) e =
match e with
| Expr.Value(
kind = NumberConstant(NumberValue.Int32 value,
NumberInfo.IsEnum({ FullName = "System.Text.NormalizationForm" }))) ->
match value with
| 1 -> Some "NFC"
| 2 -> Some "NFD"
| 5 -> Some "NFKC"
| 6 -> Some "NFKD"
| _ -> None
| _ -> None

// TODO: Improve this, see https://github.com/fable-compiler/Fable/issues/1659#issuecomment-445071965
// This is mainly used for inlining so a computation or a reference to a mutable value are understood
// as a side effects too (because we don't want to duplicate or change the order of execution)
Expand Down
13 changes: 11 additions & 2 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,12 @@ let tests = testList "Strings" [
for arg in args do
"abcd".EndsWith(fst arg)
|> equal (snd arg)
testCase "String.EndsWith with OrdinalIgnoreCase works" <| fun () ->
let args = [("ab", false); ("CD", true); ("cd", true); ("bc", false); ("xabcd", false); ("abcd", true)]
for arg in args do
"ABCD".EndsWith(fst arg, StringComparison.OrdinalIgnoreCase)
|> equal (snd arg)
|> equal (snd arg)
testCase "String.Trim works" <| fun () ->
" abc ".Trim()
Expand Down Expand Up @@ -966,6 +966,15 @@ let tests = testList "Strings" [
String.Concat(seq { yield "a"; yield "b"; yield "c" })
|> equal "abc"
testCase "System.String.Normalize works" <| fun () ->
let name1 = "\u0041\u006d\u00e9\u006c\u0069\u0065";
let name2 = "\u0041\u006d\u0065\u0301\u006c\u0069\u0065";
notEqual name1 name2
let normalized1 = name1.Normalize System.Text.NormalizationForm.FormC
let normalized2 = name2.Normalize System.Text.NormalizationForm.FormC
equal normalized1 normalized2
testCase "System.String.Join with long array works" <| fun () ->
let n = 1_000_000
let a = Array.init n (fun _i -> "a")
Expand Down

0 comments on commit 30488a0

Please sign in to comment.