2018-05-15 17:03:41 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2019-11-01 15:56:18 +01:00
|
|
|
import cptable from "codepage";
|
2022-09-02 13:56:04 +02:00
|
|
|
import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs";
|
2018-05-15 17:03:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode text operation
|
|
|
|
*/
|
|
|
|
class EncodeText extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EncodeText constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Encode text";
|
2018-12-26 00:58:00 +01:00
|
|
|
this.module = "Encodings";
|
2018-05-15 17:03:41 +02:00
|
|
|
this.description = [
|
|
|
|
"Encodes text into the chosen character encoding.",
|
|
|
|
"<br><br>",
|
|
|
|
"Supported charsets are:",
|
|
|
|
"<ul>",
|
2022-09-02 13:56:04 +02:00
|
|
|
Object.keys(CHR_ENC_CODE_PAGES).map(e => `<li>${e}</li>`).join("\n"),
|
2018-05-15 17:03:41 +02:00
|
|
|
"</ul>",
|
|
|
|
].join("\n");
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
|
2018-05-15 17:03:41 +02:00
|
|
|
this.inputType = "string";
|
2019-07-29 18:09:46 +02:00
|
|
|
this.outputType = "ArrayBuffer";
|
2018-05-15 17:03:41 +02:00
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Encoding",
|
|
|
|
"type": "option",
|
2022-09-02 13:56:04 +02:00
|
|
|
"value": Object.keys(CHR_ENC_CODE_PAGES)
|
2018-05-15 17:03:41 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
2019-07-29 18:09:46 +02:00
|
|
|
* @returns {ArrayBuffer}
|
2018-05-15 17:03:41 +02:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
2022-09-02 13:56:04 +02:00
|
|
|
const format = CHR_ENC_CODE_PAGES[args[0]];
|
2019-07-29 18:09:46 +02:00
|
|
|
const encoded = cptable.utils.encode(format, input);
|
|
|
|
return new Uint8Array(encoded).buffer;
|
2018-05-15 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default EncodeText;
|