-
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.
- Loading branch information
Showing
2 changed files
with
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './hash'; | ||
export * from './slugify'; |
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,21 @@ | ||
/** | ||
* Simple utility function to convert any string into an URL-safe version | ||
* @param val Any string | ||
* @returns URL-safe version of the value | ||
*/ | ||
export function slugify(val: string): string { | ||
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'; | ||
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'; | ||
const p = new RegExp(a.split('').join('|'), 'g'); | ||
|
||
return val | ||
.toString() | ||
.toLowerCase() | ||
.replace(/\s+/g, '-') // Replace spaces with - | ||
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters | ||
.replace(/&/g, '-and-') // Replace & with 'and' | ||
.replace(/[^\w\-]+/g, '') // Remove all non-word characters | ||
.replace(/\-\-+/g, '-') // Replace multiple - with single - | ||
.replace(/^-+/, '') // Trim - from start of text | ||
.replace(/-+$/, ''); // Trim - from end of text | ||
} |