mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 15:17:38 -05:00
fix: beforeFirst now returns the input when needle is not present.
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
10
src/index.ts
10
src/index.ts
@@ -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(" ");
|
||||
|
||||
Reference in New Issue
Block a user