2018-05-06 14:18: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";
|
|
|
|
|
import Utils from "../Utils.mjs";
|
|
|
|
|
import { DELIM_OPTIONS } from "../lib/Delim.mjs";
|
|
|
|
|
import { isWorkerEnvironment } from "../Utils.mjs";
|
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2018-05-06 14:18:41 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* From Charcode operation
|
|
|
|
|
*/
|
|
|
|
|
class FromCharcode extends Operation {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* FromCharcode constructor
|
|
|
|
|
*/
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.name = "From Charcode";
|
|
|
|
|
this.module = "Default";
|
|
|
|
|
this.description = "Converts unicode character codes back into text.<br><br>e.g. <code>0393 03b5 03b9 03ac 20 03c3 03bf 03c5</code> becomes <code>Γειά σου</code>";
|
2018-08-21 20:07:13 +02:00
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Plane_(Unicode)";
|
2018-05-06 14:18:41 +02:00
|
|
|
|
this.inputType = "string";
|
2023-02-03 18:10:33 +01:00
|
|
|
|
this.outputType = "ArrayBuffer";
|
2018-05-06 14:18:41 +02:00
|
|
|
|
this.args = [
|
|
|
|
|
{
|
|
|
|
|
"name": "Delimiter",
|
|
|
|
|
"type": "option",
|
|
|
|
|
"value": DELIM_OPTIONS
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"name": "Base",
|
|
|
|
|
"type": "number",
|
|
|
|
|
"value": 16
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} input
|
|
|
|
|
* @param {Object[]} args
|
2023-02-03 18:10:33 +01:00
|
|
|
|
* @returns {ArrayBuffer}
|
2018-05-15 19:01:04 +02:00
|
|
|
|
*
|
|
|
|
|
* @throws {OperationError} if base out of range
|
2018-05-06 14:18:41 +02:00
|
|
|
|
*/
|
|
|
|
|
run(input, args) {
|
|
|
|
|
const delim = Utils.charRep(args[0] || "Space"),
|
|
|
|
|
base = args[1];
|
|
|
|
|
let bites = input.split(delim),
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
|
|
if (base < 2 || base > 36) {
|
2018-05-15 19:01:04 +02:00
|
|
|
|
throw new OperationError("Error: Base argument must be between 2 and 36");
|
2018-05-06 14:18:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.length === 0) {
|
2023-02-27 18:55:52 +01:00
|
|
|
|
return new ArrayBuffer;
|
2018-05-06 14:18:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 11:17:52 +02:00
|
|
|
|
if (base !== 16 && isWorkerEnvironment()) self.setOption("attemptHighlight", false);
|
2018-05-06 14:18:41 +02:00
|
|
|
|
|
|
|
|
|
// Split into groups of 2 if the whole string is concatenated and
|
|
|
|
|
// too long to be a single character
|
|
|
|
|
if (bites.length === 1 && input.length > 17) {
|
|
|
|
|
bites = [];
|
|
|
|
|
for (i = 0; i < input.length; i += 2) {
|
|
|
|
|
bites.push(input.slice(i, i+2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let latin1 = "";
|
|
|
|
|
for (i = 0; i < bites.length; i++) {
|
|
|
|
|
latin1 += Utils.chr(parseInt(bites[i], base));
|
|
|
|
|
}
|
2023-02-03 18:10:33 +01:00
|
|
|
|
return Utils.strToArrayBuffer(latin1);
|
2018-05-06 14:18:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FromCharcode;
|