2019-11-25 22:53:31 +01:00
|
|
|
/**
|
|
|
|
* @author Matthieu [m@tthieu.xyz]
|
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2019-12-20 17:05:24 +01:00
|
|
|
import {UNICODE_NORMALISATION_FORMS} from "../lib/ChrEnc.mjs";
|
2019-11-25 22:53:31 +01:00
|
|
|
import unorm from "unorm";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalise Unicode operation
|
|
|
|
*/
|
|
|
|
class NormaliseUnicode extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NormaliseUnicode constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Normalise Unicode";
|
2019-12-20 17:05:24 +01:00
|
|
|
this.module = "Encodings";
|
|
|
|
this.description = "Transform Unicode characters to one of the Normalisation Forms";
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Unicode_equivalence#Normal_forms";
|
2019-11-25 22:53:31 +01:00
|
|
|
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;
|
|
|
|
|
2019-12-20 17:05:24 +01:00
|
|
|
switch (normalForm) {
|
|
|
|
case "NFD":
|
|
|
|
return unorm.nfd(input);
|
|
|
|
case "NFC":
|
|
|
|
return unorm.nfc(input);
|
|
|
|
case "NFKD":
|
|
|
|
return unorm.nfkd(input);
|
|
|
|
case "NFKC":
|
2020-05-12 22:30:11 +02:00
|
|
|
return unorm.nfkc(input);
|
2019-12-20 17:05:24 +01:00
|
|
|
default:
|
|
|
|
throw new OperationError("Unknown Normalisation Form");
|
|
|
|
}
|
2019-11-25 22:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default NormaliseUnicode;
|