mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 23:36:16 -04:00
Add operation to normalise unicode
This commit is contained in:
parent
610d46a1a4
commit
a6fa0628f2
7 changed files with 134 additions and 0 deletions
60
src/core/operations/NormaliseUnicode.mjs
Normal file
60
src/core/operations/NormaliseUnicode.mjs
Normal file
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue