Skip to content

Commit

Permalink
OrdinalIgnoreCase for EndsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
goswinr committed Sep 18, 2024
1 parent 9cf4849 commit 3c9c5c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/fable-library-ts/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,11 @@ 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 endsWith(str: string, pattern: string, ic: number) {
if (str.length >= pattern.length) {
return cmp(str.substr(str.length-1-pattern.length, pattern.length), pattern, ic) === 0;
}
return false;
}

export function initialize(n: number, f: (i: number) => string) {
Expand Down
11 changes: 9 additions & 2 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,8 @@ let tests = testList "Strings" [
"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 StringComparison.OrdinalIgnoreCase works" <| fun () ->
let args = [("ab", true); ("AB", true); ("cd", false); ("abcd", false)]
for arg in args do
"ABCD".StartsWith(fst arg, StringComparison.OrdinalIgnoreCase)
|> equal (snd arg)
Expand All @@ -833,6 +833,13 @@ let tests = testList "Strings" [
"abcd".EndsWith(fst arg)
|> equal (snd arg)
testCase "String.EndsWith with StringComparison.OrdinalIgnoreCase works" <| fun () ->
let args = [("ab", false); ("CD", true); ("cd", true); ("abcd", false)]
for arg in args do
"ABCD".EndsWith(fst arg, StringComparison.OrdinalIgnoreCase)
|> equal (snd arg)
testCase "String.Trim works" <| fun () ->
" abc ".Trim()
|> equal "abc"
Expand Down

0 comments on commit 3c9c5c0

Please sign in to comment.