Skip to content
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
9 changes: 6 additions & 3 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/string_empty_search.ts
Original file line number Diff line number Diff line change
@@ -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");
}