-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
1 parent
610d46a
commit a6fa062
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,60 @@ | ||
/** | ||
* @author Matthieu [m@tthieu.xyz] | ||
* @copyright Crown Copyright 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
import unorm from "unorm"; | ||
import {UNICODE_NORMALISATION_FORMS} from "../lib/ChrEnc"; | ||
|
||
/** | ||
* Normalise Unicode operation | ||
*/ | ||
class NormaliseUnicode extends Operation { | ||
|
||
/** | ||
* NormaliseUnicode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Normalise Unicode"; | ||
this.module = "UnicodeNormalisation"; | ||
this.description = "Transform Unicode to one of the Normalisation Form"; | ||
this.infoURL = "http://www.unicode.org/reports/tr15/"; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
name: "Normal Form", | ||
type: "option", | ||
value: UNICODE_NORMALISATION_FORMS | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
const [normalForm] = args; | ||
if (normalForm === "NFD") { | ||
return unorm.nfd(input); | ||
} else if (normalForm === "NFC") { | ||
return unorm.nfc(input); | ||
} else if (normalForm === "NFKD") { | ||
return unorm.nfkd(input); | ||
} else if (normalForm === "NFKC") { | ||
return unorm.nfc(input); | ||
} | ||
|
||
throw new OperationError("Unknown Normalisation Form"); | ||
} | ||
|
||
} | ||
|
||
export default NormaliseUnicode; |
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,54 @@ | ||
/** | ||
* Text Encoding Brute Force tests. | ||
* | ||
* @author Matthieu [m@tthieux.xyz] | ||
* | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Normalise Unicode - NFD", | ||
input: "\u00c7\u0043\u0327\u2160", | ||
expectedMatch: /C\u0327C\u0327\u2160/, | ||
recipeConfig: [ | ||
{ | ||
op: "Normalise Unicode", | ||
args: ["NFD"], | ||
}, | ||
], | ||
}, { | ||
name: "Normalise Unicode - NFC", | ||
input: "\u00c7\u0043\u0327\u2160", | ||
expectedMatch: /\u00C7\u00C7\u2160/, | ||
recipeConfig: [ | ||
{ | ||
op: "Normalise Unicode", | ||
args: ["NFC"], | ||
}, | ||
], | ||
}, { | ||
name: "Normalise Unicode - NFKD", | ||
input: "\u00c7\u0043\u0327\u2160", | ||
expectedMatch: /C\u0327C\u0327I/, | ||
recipeConfig: [ | ||
{ | ||
op: "Normalise Unicode", | ||
args: ["NFKD"], | ||
}, | ||
], | ||
}, { | ||
name: "Normalise Unicode - NFKC", | ||
input: "\u00c7\u0043\u0327\u2160", | ||
expectedMatch: /\u00C7\u00C7\u2160/, | ||
recipeConfig: [ | ||
{ | ||
op: "Normalise Unicode", | ||
args: ["NFKC"], | ||
}, | ||
], | ||
}, | ||
]); | ||
|