Skip to content

Commit

Permalink
feat: add string utils
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Dec 6, 2021
1 parent cb3fa2b commit f950774
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './date';
export * from './pluralize';
export * from './predicates';
export * from './round';
export * from './string';
32 changes: 32 additions & 0 deletions src/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {firstLine, includesIgnoreCase} from './string';

describe('includesIgnoreCase', () => {
it('finds exact match', () => {
expect(includesIgnoreCase('find me', 'search and find me')).toBeTruthy();
expect(includesIgnoreCase('find it', 'search and find me again')).toBeFalsy();
expect(includesIgnoreCase('fist part', 'first part')).toBeFalsy();
});

it('finds no match', () => {
expect(includesIgnoreCase('find it', 'search and find me again')).toBeFalsy();
});

it('finds match', () => {
expect(includesIgnoreCase('find MÖ', 'search and find mö')).toBeTruthy();
expect(includesIgnoreCase('find mö', 'search and find MÖ')).toBeTruthy();
});
});

describe('firstLine', () => {
it('gets the first line of a multiline string', () => {
expect(firstLine('foo\nbar')).toEqual('foo');
});

it('gets the first line of a one line string', () => {
expect(firstLine('foo')).toEqual('foo');
});

it('returns an empty string for empty strings', () => {
expect(firstLine('')).toEqual('');
});
});
14 changes: 14 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function includesIgnoreCase(
needle: string,
haystack: string | Array<string>,
): boolean {
if (haystack instanceof Array) {
return haystack.some((hay) => includesIgnoreCase(needle, hay));
}

return haystack.toLowerCase().includes(needle.toLowerCase());
}

export function firstLine(multilineText: string): string {
return multilineText.split('\n', 1)[0];
}

0 comments on commit f950774

Please sign in to comment.