fix: beforeFirst now returns the input when needle is not present.

This commit is contained in:
Spencer Brower
2023-06-15 13:40:26 -04:00
parent 5e8084a169
commit ba9656059b
2 changed files with 9 additions and 5 deletions

View File

@@ -71,8 +71,8 @@ describe("strings", () => {
expect(beforeFirst(" ", "foo bar bat")).toBe("foo");
});
it("removes everythin when needle is not present", () => {
expect(beforeFirst("&", "foo bar bat")).toBe("");
it("returns the input when needle is not present", () => {
expect(beforeFirst("&", "foo bar bat")).toBe("foo bar bat");
});
});
// describe('afterFirstWord', () => {

View File

@@ -23,9 +23,13 @@ export const afterLast = curry((separator: string, str: string) =>
str.substring(str.lastIndexOf(separator) + 1, str.length)
);
export const beforeFirst = curry((separator: string, str: string) =>
str.substring(0, str.indexOf(separator))
);
export const beforeFirst = curry((separator: string, str: string) => {
const index = str.indexOf(separator);
return index === -1
? str
: str.substring(0, index)
});
// @todo Test
export const beforeFirstWord = beforeFirst(" ");