-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes errors caused by special characters (#17)
* Add `cleanString` function * Update yarn.lock
- Loading branch information
Showing
6 changed files
with
137 additions
and
7 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,74 @@ | ||
import replaceSpecialCharacters from "replace-special-characters"; | ||
|
||
export default function cleanString(string: string): string { | ||
// ‘, ’, ‚ | ||
let newString = string.replace(/[\u2018\u2019\u201A]/g, "'"); | ||
// “, ”, „ | ||
newString = newString.replace(/[\u201C\u201D\u201E]/g, '"'); | ||
// … | ||
newString = newString.replace(/\u2026/g, "..."); | ||
// – | ||
newString = newString.replace(/\u2013/g, "-"); | ||
// — | ||
newString = newString.replace(/\u2014/g, "--"); | ||
// •, · | ||
newString = newString.replace(/[\u2022\u00B7]/g, "*"); | ||
// « | ||
newString = newString.replace(/\u00AB/g, "<<"); | ||
// » | ||
newString = newString.replace(/\u00BB/g, ">>"); | ||
// ‹ | ||
newString = newString.replace(/\u2039/g, "<"); | ||
// › | ||
newString = newString.replace(/\u203A/g, ">"); | ||
// ¢ | ||
newString = newString.replace(/\u00A2/g, "c"); | ||
// © | ||
newString = newString.replace(/\u00A9/g, "(C)"); | ||
// ™ | ||
newString = newString.replace(/\u2122/g, "(TM)"); | ||
// ® | ||
newString = newString.replace(/\u00AE/g, "(R)"); | ||
// ° | ||
newString = newString.replace(/\u00B0/g, "(deg)"); | ||
// ± | ||
newString = newString.replace(/\u00B1/g, "+/-"); | ||
// × | ||
newString = newString.replace(/\u00D7/g, "*"); | ||
// ÷ | ||
newString = newString.replace(/\u00F7/g, "/"); | ||
// ¼ | ||
newString = newString.replace(/\u00BC/g, "1/4"); | ||
// ½ | ||
newString = newString.replace(/\u00BD/g, "1/2"); | ||
// ¾ | ||
newString = newString.replace(/\u00BE/g, "3/4"); | ||
// √ | ||
newString = newString.replace(/\u221A/g, "sqrt"); | ||
// ¹ | ||
newString = newString.replace(/\u00B9/g, "^1"); | ||
// ² | ||
newString = newString.replace(/\u00B2/g, "^2"); | ||
// ³ | ||
newString = newString.replace(/\u00B3/g, "^3"); | ||
// ⁿ | ||
newString = newString.replace(/\u207F/g, "^n"); | ||
// ¿ | ||
newString = newString.replace(/\u00BF/g, "?"); | ||
// ¡ | ||
newString = newString.replace(/\u00A1/g, "!"); | ||
// ≥ | ||
newString = newString.replace(/\u2265/g, ">="); | ||
// ≤ | ||
newString = newString.replace(/\u2264/g, "<="); | ||
// ≠ | ||
newString = newString.replace(/\u2260/g, "!="); | ||
|
||
// replace special characters e.g., é with e | ||
newString = replaceSpecialCharacters(newString); | ||
|
||
// remove non-ascii characters | ||
newString = newString.replace(/[^\x00-\x7F]/g, ""); | ||
|
||
return newString; | ||
} |
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,40 @@ | ||
import { module, test } from "qunit"; | ||
import cleanString from "hermes/utils/clean-string"; | ||
|
||
module("Unit | Utility | clean-string", function () { | ||
test("it correctly cleans strings", function (assert) { | ||
assert.equal(cleanString("\u2018\u2019\u201A"), "'''"); | ||
assert.equal(cleanString("\u201C\u201D\u201E"), '"""'); | ||
assert.equal(cleanString("\u2026"), "..."); | ||
assert.equal(cleanString("\u2013"), "-"); | ||
assert.equal(cleanString("\u2014"), "--"); | ||
assert.equal(cleanString("\u2022\u00B7"), "**"); | ||
assert.equal(cleanString("\u00AB"), "<<"); | ||
assert.equal(cleanString("\u00BB"), ">>"); | ||
assert.equal(cleanString("\u2039"), "<"); | ||
assert.equal(cleanString("\u203A"), ">"); | ||
assert.equal(cleanString("\u00A2"), "c"); | ||
assert.equal(cleanString("\u00A9"), "(C)"); | ||
assert.equal(cleanString("\u2122"), "(TM)"); | ||
assert.equal(cleanString("\u00AE"), "(R)"); | ||
assert.equal(cleanString("\u00B0"), "(deg)"); | ||
assert.equal(cleanString("\u00B1"), "+/-"); | ||
assert.equal(cleanString("\u00D7"), "*"); | ||
assert.equal(cleanString("\u00F7"), "/"); | ||
assert.equal(cleanString("\u00BC"), "1/4"); | ||
assert.equal(cleanString("\u00BD"), "1/2"); | ||
assert.equal(cleanString("\u00BE"), "3/4"); | ||
assert.equal(cleanString("\u221A"), "sqrt"); | ||
assert.equal(cleanString("\u00B9"), "^1"); | ||
assert.equal(cleanString("\u00B2"), "^2"); | ||
assert.equal(cleanString("\u00B3"), "^3"); | ||
assert.equal(cleanString("\u207F"), "^n"); | ||
assert.equal(cleanString("\u00BF"), "?"); | ||
assert.equal(cleanString("\u00A1"), "!"); | ||
assert.equal(cleanString("\u2265"), ">="); | ||
assert.equal(cleanString("\u2264"), "<="); | ||
assert.equal(cleanString("\u2260"), "!="); | ||
assert.equal(cleanString("pøkémöñ"), "pokemon"); | ||
assert.equal(cleanString("∑π¬µ∫≈π†ºª¶§∞£"), ""); | ||
}); | ||
}); |
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