Skip to content

Commit

Permalink
feat(#44): Add Str::unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Jan 29, 2024
1 parent c4c7dd9 commit 7a61588
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
- [wordWrap](#wordwrap)
- [words](#words)
- [wrap](#wrap)
- [unwrap](#unwrap)
- [value](#value)

## Fluent Strings
Expand Down Expand Up @@ -374,7 +375,7 @@ Stringable.of('{first: "John", last: "Doe"}').isJson();
### isUrl
The `isUrl` method determines if a given string is a valid URL:
```js
Stringable.of('http://example.com').isUrl();
Stringable.of('https://example.com').isUrl();

// true

Expand Down Expand Up @@ -1040,6 +1041,13 @@ Stringable.of('is').wrap('This ', ' me!');

// 'This is me!'
```
### unwrap
The `unwrap` method removes the specified strings from the beginning and end of a given string:
```js
Stringable.of('-Laravel-').unwrap('- ');

// 'Laravel'
```
### value
The `value` method returns the underlying string value.
```js
Expand Down
8 changes: 8 additions & 0 deletions docs/statics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
- [wordWrap](#wordwrap)
- [words](#words)
- [wrap](#wrap)
- [unwrap](#unwrap)
- [preg_quote](#preg_quote)
- [random](#random)
- [createRandomStringsUsing](#createrandomstringsusing)
Expand Down Expand Up @@ -230,6 +231,13 @@ Str.wrap('is', 'This ', ' me!');

// 'This is me!'
```
### unwrap
The `unwrap` function removes the specified strings from the beginning and end of a given string:
```js
Str.unwrap('-Laravel-', '- ');

// 'Laravel'
```
### is
The `is` function determines if a given string matches a given pattern. Asterisks may be used as wildcard values
```js
Expand Down
2 changes: 2 additions & 0 deletions src/Str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
wordCount,
wordWrap,
preg_quote,
unwrap,
ExcerptOptions,
} from './methods';
import Stringable from './Stringable';
Expand Down Expand Up @@ -294,6 +295,7 @@ export const Str = {
return Stringable.of(ulid());
},
preg_quote,
unwrap,
flushCache(): void {
this._camelCache = {};
this._snakeCache = {};
Expand Down
6 changes: 6 additions & 0 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ export class Stringable {
return this;
}

public unwrap = (before: string, after?: string): this => {
this._value = Str.unwrap(this._value, before, after);

return this;
}

public toHtmlString = (): Element | Node | null => {
const template = document.createElement('template');

Expand Down
12 changes: 12 additions & 0 deletions src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,18 @@ export const ucsplit = (string: string): Array<string> => {
return string.split(/(?=\p{Lu})/u).map(i => i.trim());
}

export const unwrap = (value: string, before: string, after?: string): string => {
if (value.startsWith(before)) {
value = value.substring(before.length);
}

if (value.endsWith(after ??= before)) {
value = value.slice(0, -after.length);
}

return value;
}

export const wordCount = (value: string): number => {
return value.trim().split(/\s+/).length;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/unwrap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

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

it('returns an unwrapped string', () => {
expect(Stringable.of('-Laravel-').unwrap('-').toString())
.toBe('Laravel');

expect(Stringable.of('{framework: "Laravel"}').unwrap('{', '}').toString())
.toBe('framework: "Laravel"');

expect(Stringable.of('"value').unwrap('"').toString())
.toBe('value');

expect(Stringable.of('value"').unwrap('"').toString())
.toBe('value');

expect(Stringable.of('foo-bar-baz').unwrap('foo-', '-baz').toString())
.toBe('bar');

expect(Stringable.of('{some: "json"}').unwrap('{', '}').toString())
.toBe('some: "json"');

expect(Stringable.of('"value"').unwrap('"').toString())
.toBe('value');

expect(Str.unwrap('"value"', '"'))
.toBe('value');

expect(unwrap('"value"', '"'))
.toBe('value');
});

0 comments on commit 7a61588

Please sign in to comment.