-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nivalis-studio/slugify
feat(strings): add slugify fn
- Loading branch information
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { slugify } from './slugify'; | ||
|
||
describe('slugify', () => { | ||
test('should return the original string if it is already slug compliant', () => { | ||
expect(slugify('hello')).toBe('hello'); | ||
}); | ||
|
||
test('should replace spaces with hyphens', () => { | ||
expect(slugify('hello world')).toBe('hello-world'); | ||
}); | ||
|
||
test('should remove consecutive hyphens', () => { | ||
expect(slugify('hello world')).toBe('hello-world'); | ||
}); | ||
|
||
test('should remove left and right hyphens', () => { | ||
expect(slugify(' hello world ')).toBe('hello-world'); | ||
}); | ||
|
||
test('should remove accentued and special characters', () => { | ||
expect(slugify("Bonsoir l'Amérique")).toBe('bonsoir-lamerique'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Formats the given string in slug url compatible fashion | ||
* | ||
* slugify('hello world') -> 'hello-world' | ||
* slugify('va va_VOOM') -> 'va-va-voom' | ||
* slugify('bonjour l\'Amérique !') -> 'bonjour-lamerique' | ||
* slugify('helloWord 11') -> 'hello-word-11' | ||
* @param {string} str The String to format | ||
* @param text | ||
* @returns {string} The formatted String | ||
*/ | ||
export const slugify = (text: string) => { | ||
return text | ||
.normalize('NFD') | ||
.replaceAll(/[\u0300-\u036F]/g, '') | ||
.toLowerCase() | ||
.trim() | ||
.replaceAll(/\s+/g, '-') | ||
.replaceAll(/[^\w-]+/g, '') | ||
.replaceAll(/-{2,}/g, '-') | ||
.replace(/^-+/, '') | ||
.replace(/-+$/, ''); | ||
}; |