Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS/TS] OrdinalIgnoreCase overload for .EndsWith #3892

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,6 @@ let implementedStringFunctions =
[|
"Compare"
"CompareTo"
"EndsWith"
"Format"
"IndexOfAny"
"Insert"
Expand Down Expand Up @@ -1503,12 +1502,15 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt

let left = Helper.InstanceCall(c, "indexOf", Int32.Number, [ arg ])
makeEqOp r left (makeIntConst 0) BinaryGreaterOrEqual |> Some
| "StartsWith", Some c, [ _str ] ->
let left = Helper.InstanceCall(c, "indexOf", Int32.Number, args)
makeEqOp r left (makeIntConst 0) BinaryEqual |> Some
| "StartsWith", Some c, [ _str ] -> Helper.InstanceCall(c, "startsWith", Boolean, args) |> Some
| "StartsWith", Some c, [ _str; _comp ] ->
Helper.LibCall(com, "String", "startsWith", t, args, i.SignatureArgTypes, thisArg = c, ?loc = r)
|> Some
| "EndsWith", Some c, [ _str ] -> Helper.InstanceCall(c, "endsWith", Boolean, args) |> Some
| "EndsWith", Some c, [ _str; _comp ] ->
Helper.LibCall(com, "String", "endsWith", t, args, i.SignatureArgTypes, thisArg = c, ?loc = r)
|> Some

| ReplaceName [ "ToUpper", "toLocaleUpperCase"
"ToUpperInvariant", "toUpperCase"
"ToLower", "toLocaleLowerCase"
Expand Down
17 changes: 13 additions & 4 deletions src/fable-library-ts/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,25 @@ export function compareTo(x: string, y: string) {
}

export function startsWith(str: string, pattern: string, ic: number) {
if (ic=== StringComparison.Ordinal){ // to avoid substring allocation
return str.startsWith(pattern) ;
}
if (str.length >= pattern.length) {
return cmp(str.substr(0, pattern.length), pattern, ic) === 0;
}
return false;
}

export function endsWith(str: string, pattern: string, ic: number) {
if (ic=== StringComparison.Ordinal){ // to avoid substring allocation
return str.endsWith(pattern) ;
}
if (str.length >= pattern.length) {
return cmp(str.substr(str.length-pattern.length, pattern.length), pattern, ic) === 0;
}
return false;
}

export function indexOfAny(str: string, anyOf: string[], ...args: number[]) {
if (str == null || str === "") {
return -1;
Expand Down Expand Up @@ -374,10 +387,6 @@ export function format(str: string | object, ...args: any[]) {
});
}

export function endsWith(str: string, search: string) {
const idx = str.lastIndexOf(search);
return idx >= 0 && idx === str.length - search.length;
}

export function initialize(n: number, f: (i: number) => string) {
if (n < 0) {
Expand Down
15 changes: 10 additions & 5 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -815,23 +815,28 @@ let tests = testList "Strings" [
"abcdbcebc".IndexOfAny([|'c';'b'|]) |> equal 1

testCase "String.StartsWith works" <| fun () ->
let args = [("ab", true); ("cd", false); ("abcdx", false)]
let args = [("ab", true); ("bc", false); ("cd", false); ("abcdx", false); ("abcd", true)]
for arg in args do
"abcd".StartsWith(fst arg)
|> equal (snd arg)

testCase "String.StartsWith with StringComparison works" <| fun () ->
let args = [("ab", true); ("cd", false); ("abcdx", false)]
testCase "String.StartsWith with OrdinalIgnoreCase works" <| fun () ->
let args = [("ab", true); ("AB", true); ("BC", false); ("cd", false); ("abcdx", false); ("abcd", true)]
for arg in args do
"ABCD".StartsWith(fst arg, StringComparison.OrdinalIgnoreCase)
|> equal (snd arg)


testCase "String.EndsWith works" <| fun () ->
let args = [("ab", false); ("cd", true); ("abcdx", false)]
let args = [("ab", false); ("cd", true); ("bc", false); ("abcdx", false); ("abcd", true)]
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)

testCase "String.Trim works" <| fun () ->
" abc ".Trim()
Expand Down
Loading