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";
|
2018-05-15 17:03:41 +02:00
|
|
|
import cptable from "../vendor/js-codepage/cptable.js";
|
2019-07-09 13:23:59 +02:00
|
|
|
import {IO_FORMAT} 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>",
|
|
|
|
Object.keys(IO_FORMAT).map(e => `<li>${e}</li>`).join("\n"),
|
|
|
|
"</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";
|
|
|
|
this.outputType = "byteArray";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Encoding",
|
|
|
|
"type": "option",
|
|
|
|
"value": Object.keys(IO_FORMAT)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {byteArray}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const format = IO_FORMAT[args[0]];
|
|
|
|
let encoded = cptable.utils.encode(format, input);
|
|
|
|
encoded = Array.from(encoded);
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default EncodeText;
|