Skip to content

Commit

Permalink
Merge pull request #48 from rudashi/str-43
Browse files Browse the repository at this point in the history
resolve(#str-43): Added `take` method
  • Loading branch information
rudashi authored Jan 29, 2024
2 parents ebb6342 + 62650fd commit c4c7dd9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
- [substrCount](#substrcount)
- [substrReplace](#substrreplace)
- [swap](#swap)
- [take](#take)
- [tap](#tap)
- [test](#test)
- [title](#title)
Expand Down Expand Up @@ -779,6 +780,13 @@ Stringable.of('Tacos are great!').swap({

// 'Burritos are fantastic!'
```
### take
The `take` method returns a specified number of characters from the beginning of a string:
```js
Stringable.of('Build something amazing!').take(5);

// 'Build'
```
### tap
The `tap` method passes the string to the given closure, allowing you to examine and interact with the string while
not affecting the string itself. The original string is returned by the `tap` method regardless of what is returned by the closure:
Expand Down
7 changes: 7 additions & 0 deletions docs/statics.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,13 @@ Str.upper('laravel');

// 'LARAVEL'
```
### take
The `take` function returns a specified number of characters from the beginning of a string:
```js
Str.take('Build something amazing!', 5);

// 'Build'
```
### title
The `title` function converts the given string to `Title Case`:
```js
Expand Down
2 changes: 2 additions & 0 deletions src/Str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
substrCount,
substrReplace,
swap,
take,
trim,
ltrim,
rtrim,
Expand Down Expand Up @@ -232,6 +233,7 @@ export const Str = {
reverse,
start,
upper,
take,
title,
headline,
slug,
Expand Down
6 changes: 6 additions & 0 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ export class Stringable {
return this;
}

public take = (limit: number): this => {
this._value = Str.take(this._value, limit);

return this;
}

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

Expand Down
8 changes: 8 additions & 0 deletions src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ export const swap = (map: Record<string, string>, subject: string): string => {
.join('');
}

export const take = (value: string, limit: number): string => {
if (limit < 0) {
return substr(value, limit);
}

return substr(value, 0, limit);
}

export const trim = (value: string, characters?: string): string => {
return characters
? rtrim(ltrim(value, characters), characters)
Expand Down
22 changes: 22 additions & 0 deletions tests/take.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

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

it('returns a specified number of characters from the beginning of a string', () => {
expect(Stringable.of('Build something amazing!').take(5).toString())
.toBe('Build');

expect(Stringable.of('abcdef').take(2).toString())
.toBe('ab');

expect(Stringable.of('abcdef').take(-2).toString())
.toBe('ef');

expect(Str.take('abcdef', 2))
.toBe('ab');

expect(take('abcdef', 2))
.toBe('ab');
});

0 comments on commit c4c7dd9

Please sign in to comment.