Skip to content

Commit

Permalink
Merge pull request #57 from rudashi/str-46
Browse files Browse the repository at this point in the history
feat(#46): Added `position`
  • Loading branch information
rudashi authored Sep 2, 2024
2 parents f88c957 + 66ebe14 commit dca9b0c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
padLeft,
padRight,
parseCallback,
position,
repeat,
replaceArray,
replace,
Expand Down Expand Up @@ -183,6 +184,7 @@ export const Str = {
.map(() => characters[Math.floor(crypto.randomInt(random) % characters.length)])
.join('');
},
position,
random: (length: number = 16): string => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const size = characters.length;
Expand Down
4 changes: 4 additions & 0 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ export class Stringable {
return this;
}

public position = (needle: string, offset: number = 0): number|boolean => {
return Str.position(this._value, needle, offset);
}

public prepend = (...values: string[]): this => {
this._value = values.join('') + this._value;

Expand Down
6 changes: 6 additions & 0 deletions src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ export const parseCallback = (callback: string, method: string | null = null): A
: [callback, method];
}

export const position = (haystack: string, needle: string, offset: number = 0): number|boolean => {
const index = haystack.indexOf(needle, offset);

return index < 0 ? false : index;
}

export const repeat = (string: string, times: number): string => {
return string.repeat(times)
}
Expand Down
54 changes: 54 additions & 0 deletions tests/position.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

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

it('returns a position of the first occurrence of a given substring in the given string', () => {
expect(Stringable.of('Hello, World!').position('W'))
.toBe(7);

expect(Stringable.of('This is a test string.').position('test'))
.toBe(10);

expect(Stringable.of('This is a test string, test again.').position('test', 15))
.toBe(23);

expect(Stringable.of('Hello, World!').position('Hello'))
.toBe(0);

expect(Stringable.of('Hello, World!').position('World!'))
.toBe(7);

expect(Stringable.of('This is a tEsT string.').position('tEsT'))
.toBe(10);

expect(Stringable.of('Hello, World!').position('W', -6))
.toBe(7);

expect(Stringable.of('Äpfel, Birnen und Kirschen').position('Kirschen', -10))
.toBe(18);

expect(Stringable.of('@%€/=!"][$').position('$'))
.toBe(9);

expect(Str.position('Hello, World!', 'W'))
.toBe(7);

expect(position('Hello, World!', 'W'))
.toBe(7);
});

it('should return false when substring not exists in the given string', () => {
expect(Stringable.of('Hello, World!').position('w'))
.toBe(false);

expect(Stringable.of('Hello, World!').position('X'))
.toBe(false);

expect(Stringable.of('').position('test'))
.toBe(false);

expect(Stringable.of('Hello, World!').position('X'))
.toBe(false);
});

0 comments on commit dca9b0c

Please sign in to comment.