Skip to content

Commit

Permalink
feat(string): add before
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Aug 26, 2023
1 parent 563868b commit 1a17b22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { capitalize, ensureStartsWith, ensureEndsWith, toForwardSlashes, template, toBackSlashes, replaceFirst, replaceLast } from './string'
import { capitalize, ensureStartsWith, ensureEndsWith, toForwardSlashes, template, toBackSlashes, replaceFirst, replaceLast, before } from './string'

it('template', () => {
expect(
Expand Down Expand Up @@ -136,15 +136,20 @@ it('replaceFirst', () => {
})

it('replaceLast', () => {
// $this->assertSame('foobar fooqux', Str::replaceLast('bar', 'qux', 'foobar foobar'));
// $this->assertSame('foo/bar? foo/qux?', Str::replaceLast('bar?', 'qux?', 'foo/bar? foo/bar?'));
// $this->assertSame('foobar foo', Str::replaceLast('bar', '', 'foobar foobar'));
// $this->assertSame('foobar foobar', Str::replaceLast('xxx', 'yyy', 'foobar foobar'));
// $this->assertSame('foobar foobar', Str::replaceLast('', 'yyy', 'foobar foobar'));

expect(replaceLast('bar', 'qux', 'foobar foobar')).toEqual('foobar fooqux')
expect(replaceLast('bar?', 'qux?', 'foo/bar? foo/bar?')).toEqual('foo/bar? foo/qux?')
expect(replaceLast('bar', '', 'foobar foobar')).toEqual('foobar foo')
expect(replaceLast('xxx', 'yyy', 'foobar foobar')).toEqual('foobar foobar')
expect(replaceLast('', 'yyy', 'foobar foobar')).toEqual('foobar foobar')
})

it('before', () => {
expect(before('hannah', 'nah')).toEqual('han')
expect(before('hannah', 'n')).toEqual('ha')
expect(before('ééé hannah', 'han')).toEqual('ééé ')
expect(before('hannah', 'xxxx')).toEqual('hannah')
expect(before('hannah', '')).toEqual('hannah')
expect(before('han0nah', '0')).toEqual('han')
expect(before('han0nah', 0)).toEqual('han')
expect(before('han2nah', 2)).toEqual('han')
})
22 changes: 22 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,28 @@ export function ensureStartsWith(prefix: string, str: string) {
return str
}

/**
* Returns everything before the given value.
*
* @category String
*/
export function before(str: string, separator: string | number) {
if (separator.toString() === '') {
return str
}

const index = str.indexOf(separator.toString())
if (index === -1) {
return str
}

return str.slice(0, index)
}

/**
* Replaces the first occurrence of a given value in a string.
*
* @category String
*/
export function replaceFirst(search: string | number, replace: string | number, str: string) {
if (search.toString() === '') {
Expand All @@ -44,6 +64,8 @@ export function replaceFirst(search: string | number, replace: string | number,

/**
* Replaces the last occurrence of a given value in a string.
*
* @category String
*/
export function replaceLast(search: string | number, replace: string | number, str: string) {
if (search.toString() === '') {
Expand Down

0 comments on commit 1a17b22

Please sign in to comment.