-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify string normalization using modern JS functions
Simplify the utility for normalizing strings now that `String.prototype.normalize` and `\p` escapes are widely available.
- Loading branch information
1 parent
1870fa8
commit afa68e9
Showing
4 changed files
with
47 additions
and
89 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 was deleted.
Oops, something went wrong.
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,28 @@ | ||
/** | ||
* Convert a `camelCase` or `CapitalCase` string to `kebab-case` | ||
*/ | ||
export function hyphenate(name: string) { | ||
const uppercasePattern = /([A-Z])/g; | ||
return name.replace(uppercasePattern, '-$1').toLowerCase(); | ||
} | ||
|
||
/** Convert a `kebab-case` string to `camelCase` */ | ||
export function unhyphenate(name: string) { | ||
const idx = name.indexOf('-'); | ||
if (idx === -1) { | ||
return name; | ||
} else { | ||
const ch = (name[idx + 1] || '').toUpperCase(); | ||
return unhyphenate(name.slice(0, idx) + ch + name.slice(idx + 2)); | ||
} | ||
} | ||
|
||
/** | ||
* Convert a string into NFKD normalization form and remove marks (accents etc.) | ||
* | ||
* This function is used to normalize strings before search to ignore | ||
* differences in accents etc. | ||
*/ | ||
export function stripMarks(str: string) { | ||
return str.normalize('NFKD').replace(/\p{M}/gu, ''); | ||
} |