diff --git a/src/lib.ts b/src/lib.ts index 4e19ade..5fb035e 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -678,7 +678,8 @@ namespace __String { } export function endsWith(this: string, searchString: string, endPosition = this.length): boolean { - if (!searchString) + // a null check: "" is falsy, and an empty search string is found + if (searchString == null) { return false; } @@ -703,7 +704,8 @@ namespace __String { } export function indexOf(this: string, searchString: string, position = 0): int { - if (!searchString) + // a null check: "" is falsy, and an empty search string is found + if (searchString == null) { return -1; } @@ -989,7 +991,8 @@ namespace __String { } export function startsWith(this: string, searchString: string, position = 0): boolean { - if (!searchString) + // a null check: "" is falsy, and an empty search string is found + if (searchString == null) { return false; } diff --git a/tests/string_empty_search.ts b/tests/string_empty_search.ts new file mode 100644 index 0000000..2c2e00b --- /dev/null +++ b/tests/string_empty_search.ts @@ -0,0 +1,31 @@ +// an empty search string is found, as in JavaScript. The guard on it was `!searchString`, a null +// check written as truthiness: once "" was falsy (TypeScriptCompiler #509) it returned "not found". +function main() { + const s = "abc"; + + assert(s.endsWith(""), "endsWith('')"); + assert(s.endsWith("", 1), "endsWith('', 1)"); + assert(s.endsWith("bc"), "endsWith('bc')"); + assert(!s.endsWith("x"), "endsWith('x')"); + + assert(s.startsWith(""), "startsWith('')"); + assert(s.startsWith("", 3), "startsWith('', 3)"); + assert(s.startsWith("ab"), "startsWith('ab')"); + assert(!s.startsWith("x"), "startsWith('x')"); + + assert(s.indexOf("") == 0, "indexOf('')"); + assert(s.indexOf("", 2) == 2, "indexOf('', 2)"); + assert(s.indexOf("", 5) == 3, "indexOf('', 5)"); + assert(s.indexOf("c") == 2, "indexOf('c')"); + assert(s.indexOf("x") == -1, "indexOf('x')"); + + assert(s.lastIndexOf("") == 3, "lastIndexOf('')"); + assert(s.includes(""), "includes('')"); + + const empty = ""; + assert(empty.endsWith(""), "''.endsWith('')"); + assert(empty.startsWith(""), "''.startsWith('')"); + assert(empty.indexOf("") == 0, "''.indexOf('')"); + + console.log("ALL DONE"); +}