From b48c16b4db221c3a4ea03c740b35a40b552424ee Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 1 Mar 2019 16:02:21 +0000 Subject: [PATCH] Refactor Dish _translate to handle sync and async depending on environment. --- Gruntfile.js | 3 - src/core/Dish.mjs | 326 +++++++----------- src/core/Utils.mjs | 20 +- .../dishTranslationTypes/DishArrayBuffer.mjs | 32 ++ .../dishTranslationTypes/DishBigNumber.mjs | 40 +++ src/core/dishTranslationTypes/DishFile.mjs | 44 +++ src/core/dishTranslationTypes/DishHTML.mjs | 35 ++ src/core/dishTranslationTypes/DishJSON.mjs | 34 ++ .../dishTranslationTypes/DishListFile.mjs | 42 +++ src/core/dishTranslationTypes/DishNumber.mjs | 34 ++ src/core/dishTranslationTypes/DishString.mjs | 34 ++ .../DishTranslationType.mjs | 39 +++ src/core/dishTranslationTypes/index.mjs | 26 ++ .../tests/ConvertCoordinateFormat.mjs | 2 +- .../tests/ToFromInsensitiveRegex.mjs | 2 +- tests/operations/tests/YARA.mjs | 2 +- 16 files changed, 492 insertions(+), 223 deletions(-) create mode 100644 src/core/dishTranslationTypes/DishArrayBuffer.mjs create mode 100644 src/core/dishTranslationTypes/DishBigNumber.mjs create mode 100644 src/core/dishTranslationTypes/DishFile.mjs create mode 100644 src/core/dishTranslationTypes/DishHTML.mjs create mode 100644 src/core/dishTranslationTypes/DishJSON.mjs create mode 100644 src/core/dishTranslationTypes/DishListFile.mjs create mode 100644 src/core/dishTranslationTypes/DishNumber.mjs create mode 100644 src/core/dishTranslationTypes/DishString.mjs create mode 100644 src/core/dishTranslationTypes/DishTranslationType.mjs create mode 100644 src/core/dishTranslationTypes/index.mjs diff --git a/Gruntfile.js b/Gruntfile.js index a14c3001..30b9fa8c 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -365,9 +365,6 @@ module.exports = function (grunt) { "./config/modules/OpModules": "./config/modules/Default" } }, - output: { - // globalObject: "this", - }, plugins: [ new webpack.DefinePlugin(BUILD_CONSTANTS), new HtmlWebpackPlugin({ diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index 64602181..452be80d 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -10,6 +10,17 @@ import DishError from "./errors/DishError"; import BigNumber from "bignumber.js"; import log from "loglevel"; +import { + DishArrayBuffer, + DishBigNumber, + DishFile, + DishHTML, + DishJSON, + DishListFile, + DishNumber, + DishString, +} from "./dishTranslationTypes"; + /** * The data being operated on by each operation. @@ -116,6 +127,8 @@ class Dish { /** * Returns the value of the data in the type format specified. * + * If running in a browser, get is asynchronous. + * * @param {number} type - The data type of value, see Dish enums. * @param {boolean} [notUTF8=false] - Do not treat strings as UTF8. * @returns {* | Promise} - (Broswer) A promise | (Node) value of dish in given type @@ -127,8 +140,13 @@ class Dish { if (this.type !== type) { + // Node environment => _translate is sync + if (Utils.isNode()) { + this._translate(type, notUTF8); + return this.value; + // Browser environment => _translate is async - if (Utils.isBrowser()) { + } else { return new Promise((resolve, reject) => { this._translate(type, notUTF8) .then(() => { @@ -136,11 +154,6 @@ class Dish { }) .catch(reject); }); - - // Node environment => _translate is sync - } else { - this._translate(type, notUTF8); - return this.value; } } @@ -308,6 +321,110 @@ class Dish { return newDish; } + + /** + * Translates the data to the given type format. + * + * If running in the browser, _translate is asynchronous. + * + * @param {number} toType - The data type of value, see Dish enums. + * @param {boolean} [notUTF8=false] - Do not treat strings as UTF8. + * @returns {Promise || undefined} + */ + _translate(toType, notUTF8=false) { + log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`); + + // Node environment => translate is sync + if (Utils.isNode()) { + this._toByteArray(); + this._fromByteArray(toType, notUTF8); + + // Browser environment => translate is async + } else { + return new Promise((resolve, reject) => { + this._toByteArray() + .then(() => this.type = Dish.BYTE_ARRAY) + .then(() => { + this._fromByteArray(toType); + resolve(); + }) + .catch(reject); + }); + } + + } + + /** + * Convert this.value to a ByteArray + * + * If running in a browser, _toByteArray is asynchronous. + * + * @returns {Promise || undefined} + */ + _toByteArray() { + // Using 'bind' here to allow this.value to be mutated within translation functions + const toByteArrayFuncs = { + browser: { + [Dish.STRING]: () => Promise.resolve(DishString.toByteArray.bind(this)()), + [Dish.NUMBER]: () => Promise.resolve(DishNumber.toByteArray.bind(this)()), + [Dish.HTML]: () => Promise.resolve(DishHTML.toByteArray.bind(this)()), + [Dish.ARRAY_BUFFER]: () => Promise.resolve(DishArrayBuffer.toByteArray.bind(this)()), + [Dish.BIG_NUMBER]: () => Promise.resolve(DishBigNumber.toByteArray.bind(this)()), + [Dish.JSON]: () => Promise.resolve(DishJSON.toByteArray.bind(this)()), + [Dish.FILE]: () => DishFile.toByteArray.bind(this)(), + [Dish.LIST_FILE]: () => DishListFile.toByteArray.bind(this)(), + [Dish.BYTE_ARRAY]: () => Promise.resolve(), + }, + node: { + [Dish.STRING]: () => DishString.toByteArray.bind(this)(), + [Dish.NUMBER]: () => DishNumber.toByteArray.bind(this)(), + [Dish.HTML]: () => DishHTML.toByteArray.bind(this)(), + [Dish.ARRAY_BUFFER]: () => DishArrayBuffer.toByteArray.bind(this)(), + [Dish.BIG_NUMBER]: () => DishBigNumber.toByteArray.bind(this)(), + [Dish.JSON]: () => DishJSON.toByteArray.bind(this)(), + [Dish.FILE]: () => DishFile.toByteArray.bind(this)(), + [Dish.LIST_FILE]: () => DishListFile.toByteArray.bind(this)(), + [Dish.BYTE_ARRAY]: () => {}, + } + }; + + try { + return toByteArrayFuncs[Utils.isNode() && "node" || "browser"][this.type](); + } catch (err) { + throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`); + } + } + + /** + * Convert this.value to the given type. + * + * @param {number} toType - the Dish enum to convert to + * @param {boolean} [notUTF8=false] - Do not treat strings as UTF8. + */ + _fromByteArray(toType, notUTF8) { + const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8; + + // Using 'bind' here to allow this.value to be mutated within translation functions + const toTypeFunctions = { + [Dish.STRING]: () => DishString.fromByteArray.bind(this)(byteArrayToStr), + [Dish.NUMBER]: () => DishNumber.fromByteArray.bind(this)(byteArrayToStr), + [Dish.HTML]: () => DishHTML.fromByteArray.bind(this)(byteArrayToStr), + [Dish.ARRAY_BUFFER]: () => DishArrayBuffer.fromByteArray.bind(this)(), + [Dish.BIG_NUMBER]: () => DishBigNumber.fromByteArray.bind(this)(byteArrayToStr), + [Dish.JSON]: () => DishJSON.fromByteArray.bind(this)(byteArrayToStr), + [Dish.FILE]: () => DishFile.fromByteArray.bind(this)(), + [Dish.LIST_FILE]: () => DishListFile.fromByteArray.bind(this)(), + [Dish.BYTE_ARRAY]: () => {}, + }; + + try { + toTypeFunctions[toType](); + this.type = toType; + } catch (err) { + throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`); + } + } + } @@ -367,201 +484,4 @@ Dish.FILE = 7; Dish.LIST_FILE = 8; - - -/** - * Translates the data to the given type format. - * - * @param {number} toType - The data type of value, see Dish enums. - * @param {boolean} [notUTF8=false] - Do not treat strings as UTF8. - */ -async function _asyncTranslate(toType, notUTF8=false) { - log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`); - const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8; - - // Convert data to intermediate byteArray type - try { - switch (this.type) { - case Dish.STRING: - this.value = this.value ? Utils.strToByteArray(this.value) : []; - break; - case Dish.NUMBER: - this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : []; - break; - case Dish.HTML: - this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : []; - break; - case Dish.ARRAY_BUFFER: - // Array.from() would be nicer here, but it's slightly slower - this.value = Array.prototype.slice.call(new Uint8Array(this.value)); - break; - case Dish.BIG_NUMBER: - this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : []; - break; - case Dish.JSON: - this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : []; - break; - case Dish.FILE: - this.value = await Utils.readFile(this.value); - this.value = Array.prototype.slice.call(this.value); - break; - case Dish.LIST_FILE: - this.value = await Promise.all(this.value.map(async f => Utils.readFile(f))); - this.value = this.value.map(b => Array.prototype.slice.call(b)); - this.value = [].concat.apply([], this.value); - break; - default: - break; - } - } catch (err) { - throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`); - } - - this.type = Dish.BYTE_ARRAY; - - // Convert from byteArray to toType - try { - switch (toType) { - case Dish.STRING: - case Dish.HTML: - this.value = this.value ? byteArrayToStr(this.value) : ""; - this.type = Dish.STRING; - break; - case Dish.NUMBER: - this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0; - this.type = Dish.NUMBER; - break; - case Dish.ARRAY_BUFFER: - this.value = new Uint8Array(this.value).buffer; - this.type = Dish.ARRAY_BUFFER; - break; - case Dish.BIG_NUMBER: - try { - this.value = new BigNumber(byteArrayToStr(this.value)); - } catch (err) { - this.value = new BigNumber(NaN); - } - this.type = Dish.BIG_NUMBER; - break; - case Dish.JSON: - this.value = JSON.parse(byteArrayToStr(this.value)); - this.type = Dish.JSON; - break; - case Dish.FILE: - this.value = new File(this.value, "unknown"); - break; - case Dish.LIST_FILE: - this.value = [new File(this.value, "unknown")]; - this.type = Dish.LIST_FILE; - break; - default: - break; - } - } catch (err) { - throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`); - } -} - - -/** - * Translates the data to the given type format. - * - * @param {number} toType - The data type of value, see Dish enums. - * @param {boolean} [notUTF8=false] - Do not treat strings as UTF8. - */ -function _translate(toType, notUTF8=false) { - log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`); - const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8; - - // Convert data to intermediate byteArray type - try { - switch (this.type) { - case Dish.STRING: - this.value = this.value ? Utils.strToByteArray(this.value) : []; - break; - case Dish.NUMBER: - this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : []; - break; - case Dish.HTML: - this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : []; - break; - case Dish.ARRAY_BUFFER: - // Array.from() would be nicer here, but it's slightly slower - this.value = Array.prototype.slice.call(new Uint8Array(this.value)); - break; - case Dish.BIG_NUMBER: - this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : []; - break; - case Dish.JSON: - this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : []; - break; - case Dish.FILE: - this.value = Utils.readFileSync(this.value); - this.value = Array.prototype.slice.call(this.value); - break; - case Dish.LIST_FILE: - this.value = this.value.map(f => Utils.readFileSync(f)); - this.value = this.value.map(b => Array.prototype.slice.call(b)); - this.value = [].concat.apply([], this.value); - break; - default: - break; - } - } catch (err) { - throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`); - } - - this.type = Dish.BYTE_ARRAY; - - // Convert from byteArray to toType - try { - switch (toType) { - case Dish.STRING: - case Dish.HTML: - this.value = this.value ? byteArrayToStr(this.value) : ""; - this.type = Dish.STRING; - break; - case Dish.NUMBER: - this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0; - this.type = Dish.NUMBER; - break; - case Dish.ARRAY_BUFFER: - this.value = new Uint8Array(this.value).buffer; - this.type = Dish.ARRAY_BUFFER; - break; - case Dish.BIG_NUMBER: - try { - this.value = new BigNumber(byteArrayToStr(this.value)); - } catch (err) { - this.value = new BigNumber(NaN); - } - this.type = Dish.BIG_NUMBER; - break; - case Dish.JSON: - this.value = JSON.parse(byteArrayToStr(this.value)); - this.type = Dish.JSON; - break; - case Dish.FILE: - this.value = new File(this.value, "unknown"); - break; - case Dish.LIST_FILE: - this.value = [new File(this.value, "unknown")]; - this.type = Dish.LIST_FILE; - break; - default: - break; - } - } catch (err) { - throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`); - } -} - - -if (Utils.isBrowser()) { - Dish.prototype._translate = _asyncTranslate; - -} else { - Dish.prototype._translate = _translate; -} - export default Dish; diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index c4cd14ab..934186ab 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -926,7 +926,11 @@ class Utils { * await Utils.readFile(new File(["hello"], "test")) */ static readFile(file) { - if (Utils.isBrowser()) { + + if (Utils.isNode()) { + return Buffer.from(file).buffer; + + } else { return new Promise((resolve, reject) => { const reader = new FileReader(); const data = new Uint8Array(file.size); @@ -954,17 +958,12 @@ class Utils { seek(); }); - - } else if (Utils.isNode()) { - return Buffer.from(file).buffer; } - - throw new Error("Unkown environment!"); } /** */ static readFileSync(file) { - if (Utils.isBrowser()) { + if (!Utils.isNode()) { throw new TypeError("Browser environment cannot support readFileSync"); } @@ -1065,13 +1064,6 @@ class Utils { }[token]; } - /** - * Check if code is running in a browser environment - */ - static isBrowser() { - return typeof window !== "undefined" && typeof window.document !== "undefined"; - } - /** * Check if code is running in a Node environment */ diff --git a/src/core/dishTranslationTypes/DishArrayBuffer.mjs b/src/core/dishTranslationTypes/DishArrayBuffer.mjs new file mode 100644 index 00000000..96a8b8e3 --- /dev/null +++ b/src/core/dishTranslationTypes/DishArrayBuffer.mjs @@ -0,0 +1,32 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; + +/** + * Translation methods for ArrayBuffer Dishes + */ +class DishArrayBuffer extends DishTranslationType { + + /** + * convert the given value to a ByteArray + */ + static toByteArray() { + DishArrayBuffer.checkForValue(this.value); + this.value = Array.prototype.slice.call(new Uint8Array(this.value)); + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray() { + DishArrayBuffer.checkForValue(this.value); + this.value = new Uint8Array(this.value).buffer; + } +} + +export default DishArrayBuffer; diff --git a/src/core/dishTranslationTypes/DishBigNumber.mjs b/src/core/dishTranslationTypes/DishBigNumber.mjs new file mode 100644 index 00000000..e2aae7b9 --- /dev/null +++ b/src/core/dishTranslationTypes/DishBigNumber.mjs @@ -0,0 +1,40 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; +import BigNumber from "bignumber.js"; + +/** + * translation methods for BigNumber Dishes + */ +class DishBigNumber extends DishTranslationType { + + /** + * convert the given value to a ByteArray + * @param {BigNumber} value + */ + static toByteArray() { + DishBigNumber.checkForValue(this.value); + this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : []; + } + + /** + * convert the given value from a ByteArray + * @param {ByteArray} value + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr) { + DishBigNumber.checkForValue(this.value); + try { + this.value = new BigNumber(byteArrayToStr(this.value)); + } catch (err) { + this.value = new BigNumber(NaN); + } + } +} + +export default DishBigNumber; diff --git a/src/core/dishTranslationTypes/DishFile.mjs b/src/core/dishTranslationTypes/DishFile.mjs new file mode 100644 index 00000000..11a9c871 --- /dev/null +++ b/src/core/dishTranslationTypes/DishFile.mjs @@ -0,0 +1,44 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; + +/** + * Translation methods for file Dishes + */ +class DishFile extends DishTranslationType { + + /** + * convert the given value to a ByteArray + * @param {File} value + */ + static toByteArray() { + DishFile.checkForValue(this.value); + if (Utils.isNode()) { + this.value = Array.prototype.slice.call(Utils.readFileSync(this.value)); + } else { + return new Promise((resolve, reject) => { + Utils.readFile(this.value) + .then(v => this.value = Array.prototype.slice.call(v)) + .then(resolve) + .catch(reject); + }); + } + } + + /** + * convert the given value from a ByteArray + * @param {ByteArray} value + * @param {function} byteArrayToStr + */ + static fromByteArray() { + DishFile.checkForValue(this.value); + this.value = new File(this.value, "unknown"); + } +} + +export default DishFile; diff --git a/src/core/dishTranslationTypes/DishHTML.mjs b/src/core/dishTranslationTypes/DishHTML.mjs new file mode 100644 index 00000000..f7a74d9e --- /dev/null +++ b/src/core/dishTranslationTypes/DishHTML.mjs @@ -0,0 +1,35 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; +import DishString from "./DishString"; + +/** + * Translation methods for HTML Dishes + */ +class DishHTML extends DishTranslationType { + + /** + * convert the given value to a ByteArray + * @param {String} value + */ + static toByteArray() { + DishHTML.checkForValue(this.value); + this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : []; + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr) { + DishHTML.checkForValue(this.value); + DishString.fromByteArray(this.value, byteArrayToStr); + } +} + +export default DishHTML; diff --git a/src/core/dishTranslationTypes/DishJSON.mjs b/src/core/dishTranslationTypes/DishJSON.mjs new file mode 100644 index 00000000..a2c20390 --- /dev/null +++ b/src/core/dishTranslationTypes/DishJSON.mjs @@ -0,0 +1,34 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; + +/** + * Translation methods for JSON dishes + */ +class DishJSON extends DishTranslationType { + + /** + * convert the given value to a ByteArray + */ + static toByteArray() { + DishJSON.checkForValue(this.value); + this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : []; + } + + /** + * convert the given value from a ByteArray + * @param {ByteArray} value + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr) { + DishJSON.checkForValue(this.value); + this.value = JSON.parse(byteArrayToStr(this.value)); + } +} + +export default DishJSON; diff --git a/src/core/dishTranslationTypes/DishListFile.mjs b/src/core/dishTranslationTypes/DishListFile.mjs new file mode 100644 index 00000000..678e2d59 --- /dev/null +++ b/src/core/dishTranslationTypes/DishListFile.mjs @@ -0,0 +1,42 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; + +/** + * Translation methods for ListFile Dishes + */ +class DishListFile extends DishTranslationType { + + /** + * convert the given value to a ByteArray + */ + static toByteArray() { + DishListFile.checkForValue(this.value); + if (Utils.isNode()) { + this.value = [].concat.apply([], this.value.map(f => Utils.readFileSync(f)).map(b => Array.prototype.slice.call(b))); + } else { + return new Promise((resolve, reject) => { + Promise.all(this.value.map(async f => Utils.readFile(f))) + .then(values => this.value = values.map(b => [].concat.apply([], Array.prototype.slice.call(b)))) + .then(resolve) + .catch(reject); + }); + } + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray() { + DishListFile.checkForValue(this.value); + this.value = [new File(this.value, "unknown")]; + } +} + +export default DishListFile; diff --git a/src/core/dishTranslationTypes/DishNumber.mjs b/src/core/dishTranslationTypes/DishNumber.mjs new file mode 100644 index 00000000..0cc97af0 --- /dev/null +++ b/src/core/dishTranslationTypes/DishNumber.mjs @@ -0,0 +1,34 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; + +/** + * Translation methods for number dishes + */ +class DishNumber extends DishTranslationType { + + /** + * convert the given value to a ByteArray + */ + static toByteArray() { + DishNumber.checkForValue(this.value); + this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : []; + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr) { + DishNumber.checkForValue(this.value); + this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0; + } +} + +export default DishNumber; diff --git a/src/core/dishTranslationTypes/DishString.mjs b/src/core/dishTranslationTypes/DishString.mjs new file mode 100644 index 00000000..78c273c6 --- /dev/null +++ b/src/core/dishTranslationTypes/DishString.mjs @@ -0,0 +1,34 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + + +import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils"; + +/** + * Translation methods for string dishes + */ +class DishString extends DishTranslationType { + + /** + * convert the given value to a ByteArray + */ + static toByteArray() { + DishString.checkForValue(this.value); + this.value = this.value ? Utils.strToByteArray(this.value) : []; + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr) { + DishString.checkForValue(this.value); + this.value = this.value ? byteArrayToStr(this.value) : ""; + } +} + +export default DishString; diff --git a/src/core/dishTranslationTypes/DishTranslationType.mjs b/src/core/dishTranslationTypes/DishTranslationType.mjs new file mode 100644 index 00000000..261f9bcd --- /dev/null +++ b/src/core/dishTranslationTypes/DishTranslationType.mjs @@ -0,0 +1,39 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + + +/** + * Abstract class for dish translation methods + */ +class DishTranslationType { + + /** + * Warn translations dont work without value from bind + */ + static checkForValue(value) { + if (value === undefined) { + throw new Error("only use translation methods with .bind"); + } + } + + /** + * convert the given value to a ByteArray + * @param {*} value + */ + static toByteArray() { + throw new Error("toByteArray has not been implemented"); + } + + /** + * convert the given value from a ByteArray + * @param {function} byteArrayToStr + */ + static fromByteArray(byteArrayToStr=undefined) { + throw new Error("toType has not been implemented"); + } +} + +export default DishTranslationType; diff --git a/src/core/dishTranslationTypes/index.mjs b/src/core/dishTranslationTypes/index.mjs new file mode 100644 index 00000000..8f6f920c --- /dev/null +++ b/src/core/dishTranslationTypes/index.mjs @@ -0,0 +1,26 @@ +/** + * @author d98762625 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + + +import DishArrayBuffer from "./DishArrayBuffer"; +import DishBigNumber from "./DishBigNumber"; +import DishFile from "./DishFile"; +import DishHTML from "./DishHTML"; +import DishJSON from "./DishJSON"; +import DishListFile from "./DishListFile"; +import DishNumber from "./DishNumber"; +import DishString from "./DishString"; + +export { + DishArrayBuffer, + DishBigNumber, + DishFile, + DishHTML, + DishJSON, + DishListFile, + DishNumber, + DishString, +}; diff --git a/tests/operations/tests/ConvertCoordinateFormat.mjs b/tests/operations/tests/ConvertCoordinateFormat.mjs index 1291aa4d..80a336b7 100644 --- a/tests/operations/tests/ConvertCoordinateFormat.mjs +++ b/tests/operations/tests/ConvertCoordinateFormat.mjs @@ -18,7 +18,7 @@ * UTM: 30N 699456 5709791, */ -import TestRegister from "../TestRegister"; +import TestRegister from "../../lib/TestRegister"; TestRegister.addTests([ { diff --git a/tests/operations/tests/ToFromInsensitiveRegex.mjs b/tests/operations/tests/ToFromInsensitiveRegex.mjs index fa191951..0aaf89e2 100644 --- a/tests/operations/tests/ToFromInsensitiveRegex.mjs +++ b/tests/operations/tests/ToFromInsensitiveRegex.mjs @@ -6,7 +6,7 @@ * @copyright Crown Copyright 2018 * @license Apache-2.0 */ -import TestRegister from "../TestRegister"; +import TestRegister from "../../lib/TestRegister"; TestRegister.addTests([ { diff --git a/tests/operations/tests/YARA.mjs b/tests/operations/tests/YARA.mjs index e3c28ef1..5495ca69 100644 --- a/tests/operations/tests/YARA.mjs +++ b/tests/operations/tests/YARA.mjs @@ -6,7 +6,7 @@ * @copyright Crown Copyright 2019 * @license Apache-2.0 */ -import TestRegister from "../TestRegister"; +import TestRegister from "../../lib/TestRegister"; TestRegister.addTests([ {