Added 'Decimal' option for toggleStrings. Closes #337.

This commit is contained in:
n1474335 2018-08-19 22:50:49 +01:00
parent c1b2fc9400
commit affe057cab
10 changed files with 51 additions and 21 deletions

37
src/core/lib/Decimal.mjs Normal file
View file

@ -0,0 +1,37 @@
/**
* Decimal functions.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils";
/**
* Convert a string of decimal values into a byte array.
*
* @param {string} data
* @param {string} [delim]
* @returns {byteArray}
*
* @example
* // returns [10,20,30]
* fromDecimal("10 20 30");
*
* // returns [10,20,30]
* fromDecimal("10:20:30", "Colon");
*/
export function fromDecimal(data, delim="Auto") {
delim = Utils.charRep(delim);
const output = [];
let byteStr = data.split(delim);
if (byteStr[byteStr.length-1] === "")
byteStr = byteStr.slice(0, byteStr.length-1);
for (let i = 0; i < byteStr.length; i++) {
output[i] = parseInt(byteStr[i], 10);
}
return output;
}

View file

@ -1,5 +1,5 @@
/**
* Byte representation functions.
* Hexadecimal functions.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
@ -83,8 +83,7 @@ export function toHexFast(data) {
* // returns [10,20,30]
* fromHex("0a:14:1e", "Colon");
*/
export function fromHex(data, delim, byteLen=2) {
delim = delim || "Auto";
export function fromHex(data, delim="Auto", byteLen=2) {
if (delim !== "None") {
const delimRegex = delim === "Auto" ? /[^a-f\d]/gi : Utils.regexRep(delim);
data = data.replace(delimRegex, "");