Skip to content

Commit

Permalink
Merge pull request #24 from rudashi/str-21
Browse files Browse the repository at this point in the history
Resolves #21
  • Loading branch information
rudashi authored Mar 6, 2023
2 parents e5ddf06 + c3316c7 commit 0709c9d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
13 changes: 9 additions & 4 deletions dist/Stringable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Stringable.js.map

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,15 +669,22 @@ export class Stringable {
return match;
}

public slug = (separator: string = '-', language: string | null = 'en'): this => {
public slug = (separator: string = '-', language: string | null = 'en', dictionary: Record<string, string> = {'@': 'at'}): this => {

if (language) {
this.ascii();
}

this.replace(['-', '_'], separator)
.replace('@', separator + 'at' + separator)
.snake(separator).trim();
this.replace(['-', '_'], separator);

Object.keys(dictionary).map((key: string) => {
dictionary[key] = separator + dictionary[key] + separator;
});

this.replace(Object.keys(dictionary), Object.values(dictionary))
.lower()
.replaceMatches(/\s/, separator)
.replaceMatches('('+separator+')(?=\\1)', '');

return this;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/slug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ it('returns the string as a URL friendly', () => {
expect(Stringable.of('').slug().toString())
.toBe('');

expect(Stringable.of('500$ bill').slug('-', 'en', {'$': 'dollar'}).toString())
.toBe('500-dollar-bill');

expect(Stringable.of('500--$----bill').slug('-', 'en', {'$': 'dollar'}).toString())
.toBe('500-dollar-bill');

expect(Stringable.of('500-$-bill').slug('-', 'en', {'$': 'dollar'}).toString())
.toBe('500-dollar-bill');

expect(Stringable.of('500$--bill').slug('-', 'en', {'$': 'dollar'}).toString())
.toBe('500-dollar-bill');

expect(Stringable.of('500-$--bill').slug('-', 'en', {'$': 'dollar'}).toString())
.toBe('500-dollar-bill');

});

0 comments on commit 0709c9d

Please sign in to comment.