Skip to content

Commit

Permalink
Merge pull request #58 from rudashi/stringable-40
Browse files Browse the repository at this point in the history
Resolved #40
  • Loading branch information
rudashi committed Sep 4, 2024
2 parents dca9b0c + 26c022c commit e43d9c2
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ replaceArray('?', ['8:30', '9:00'], 'The event will take place between ? and ?')

// 'The event will take place between 8:30 and 9:00'
```

### replaceEnd

The `replaceEnd` function replaces the last occurrence of the given value only if the value appears at the start of the string:

```js
replaceEnd('World', 'Laravel', 'Hello World');

// Hello Laravel
```

### replaceFirst
The `replaceFirst` function replaces the first occurrence of a given value in a string:
```js
Expand All @@ -421,6 +432,16 @@ replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
// 'the quick brown fox jumps over a lazy dog'
```

### replaceStart

The `replaceStart` function replaces the first occurrence of the given value only if the value appears at the start of the string:

```js
replaceStart('Hello', 'Laravel', 'Hello World');

// Laravel World
```

You may also pass `false` as a third parameter to ignore case when removing strings.
### reverse
The `reverse` function reverses the given string:
Expand Down
22 changes: 22 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ Stringable.of('The event will take place between ? and ?').replaceArray('?', ['8

// 'The event will take place between 8:30 and 9:00'
```

### replaceEnd

The `replaceEnd` method replaces the last occurrence of the given value only if the value appears at the start of the string:

```js
Stringable.of('Hello World').replaceEnd('World', 'Laravel');

// Hello Laravel
```

### replaceFirst
The `replaceFirst` method replaces the first occurrence of a given value in a string:
```js
Expand All @@ -576,6 +587,17 @@ Stringable.of('the quick brown fox jumps over the lazy dog').replaceLast('the',

// 'the quick brown fox jumps over a lazy dog'
```

### replaceStart

The `replaceStart` method replaces the first occurrence of the given value only if the value appears at the start of the string:

```js
Stringable.of('Hello World').replaceStart('Hello', 'Laravel');

// Laravel World
```

### replaceMatches
The `replaceMatches` method replaces all portions of a string matching a pattern with the given replacement string:
```js
Expand Down
22 changes: 22 additions & 0 deletions docs/statics.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ Str.replace('6.x', '7.x', 'Laravel 6.x');

// 'Laravel 7.x'
```

### replaceEnd

The `replaceEnd` function replaces the last occurrence of the given value only if the value appears at the start of the string:

```js
Str.replaceEnd('World', 'Laravel', 'Hello World');

// Hello Laravel
```

### replaceFirst
The `replaceFirst` function replaces the first occurrence of a given value in a string:
```js
Expand All @@ -456,6 +467,17 @@ Str.replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');

// 'the quick brown fox jumps over a lazy dog'
```

### replaceStart

The `replaceStart` function replaces the first occurrence of the given value only if the value appears at the start of the string:

```js
Str.replaceStart('Hello', 'Laravel', 'Hello World');

// Laravel World
```

### remove
The `remove` function removes the given value or array of values from the string:
```js
Expand Down
4 changes: 4 additions & 0 deletions src/Str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import {
repeat,
replaceArray,
replace,
replaceEnd,
replaceFirst,
replaceLast,
replaceStart,
remove,
reverse,
start,
Expand Down Expand Up @@ -230,8 +232,10 @@ export const Str = {
repeat,
replaceArray,
replace,
replaceEnd,
replaceFirst,
replaceLast,
replaceStart,
remove,
reverse,
start,
Expand Down
12 changes: 12 additions & 0 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ export class Stringable {
return this;
}

public replaceEnd = (search: string, replace: string): this => {
this._value = Str.replaceEnd(search, replace, this._value);

return this;
}

public replaceFirst = (search: string, replace: string): this => {
this._value = Str.replaceFirst(search, replace, this._value);

Expand All @@ -334,6 +340,12 @@ export class Stringable {
return this;
}

public replaceStart = (search: string, replace: string): this => {
this._value = Str.replaceStart(search, replace, this._value);

return this;
}

public replaceMatches = (pattern: RegExp | string, replace: string): this => {
this._value = this._value.replace(new RegExp(pattern, 'g'), replace);

Expand Down
24 changes: 24 additions & 0 deletions src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,18 @@ export const replace = (search: string | string[], replace: string | string[], s
return subject;
}

export const replaceEnd = (search: string, replace: string, subject: string): string => {
if (search === '') {
return subject;
}

if (endsWith(subject, search)) {
return replaceLast(search, replace, subject);
}

return subject;
}

export const replaceFirst = (search: string, replace: string, subject: string): string => {
if (search === '') {
return subject;
Expand All @@ -460,6 +472,18 @@ export const replaceLast = (search: string, replace: string, subject: string): s
return subject;
}

export const replaceStart = (search: string, replace: string, subject: string): string => {
if (search === '') {
return subject;
}

if (startsWith(subject, search)) {
return replaceFirst(search, replace, subject);
}

return subject;
}

export const remove = (search: string | string[], subject: string, caseSensitive: boolean = true): string => {
search = search instanceof Array ? search : [search];

Expand Down
35 changes: 35 additions & 0 deletions tests/replaceEnd.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const {Stringable} = require('../src/Stringable');
const {Str} = require('../src/Str');
const {replaceEnd} = require('../src/methods');

test.each([
['World', 'Laravel', 'Hello World', 'Hello Laravel'],
['Hello', 'Laravel', 'Hello World', 'Hello World'],
['bar', 'qux', 'foobar foobar', 'foobar fooqux'],
['bar?', 'qux?', 'foo/bar? foo/bar?', 'foo/bar? foo/qux?'],
['bar', '', 'foobar foobar', 'foobar foo'],
['xxx', 'yyy', 'foobar foobar', 'foobar foobar'],
['', 'yyy', 'foobar foobar', 'foobar foobar'],
['xxx', 'yyy', 'fooxxx foobar', 'fooxxx foobar'],
['ö', 'xxx', 'Malmö Jönköping', 'Malmö Jönköping'],
['öping', 'yyy', 'Malmö Jönköping', 'Malmö Jönkyyy'],
])('.replaceEnd from %p search %p and replace with %p then returns %p',
/**
* @param {string} search
* @param {string} replace
* @param {string} string
* @param {string} expected
*/
(search, replace, string, expected) => {
expect(Stringable.of(string).replaceEnd(search, replace).toString())
.toBe(expected);

expect(Str.replaceEnd(search, replace, string))
.toBe(expected);

expect(replaceEnd(search, replace, string))
.toBe(expected);
}
);
35 changes: 35 additions & 0 deletions tests/replaceStart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const {Stringable} = require('../src/Stringable');
const {Str} = require('../src/Str');
const {replaceStart} = require('../src/methods');

test.each([
['Hello', 'Laravel', 'Hello World', 'Laravel World'],
['World', 'Laravel', 'Hello World', 'Hello World'],
['bar', 'qux', 'foobar foobar', 'foobar foobar'],
['bar?', 'qux?', 'foo/bar? foo/bar?', 'foo/bar? foo/bar?'],
['foo', 'qux', 'foobar foobar', 'quxbar foobar'],
['foo/bar?', 'qux?', 'foo/bar? foo/bar?', 'qux? foo/bar?'],
['foo', '', 'foobar foobar', 'bar foobar'],
[0, '1', '0', '1'],
['Jö', 'xxx', 'Jönköping Malmö', 'xxxnköping Malmö'],
['', 'yyy', 'Jönköping Malmö', 'Jönköping Malmö'],
])('.replaceEnd from %p search %p and replace with %p then returns %p',
/**
* @param {string} search
* @param {string} replace
* @param {string} subject
* @param {string} expected
*/
(search, replace, subject, expected) => {
expect(Stringable.of(subject).replaceStart(search, replace).toString())
.toBe(expected);

expect(Str.replaceStart(search, replace, subject))
.toBe(expected);

expect(replaceStart(search, replace, subject))
.toBe(expected);
}
);

0 comments on commit e43d9c2

Please sign in to comment.