From b0ab7a6e7607680d3bbeb6ab057338f1a0a4d71b Mon Sep 17 00:00:00 2001 From: Isak W Date: Tue, 2 Jul 2024 22:34:11 +0200 Subject: [PATCH] Inline util functions --- src/BitArray.js | 36 +++++++++--------------- src/BitField.js | 75 +++++++++++++++++++++++++++++++------------------ src/BitFlags.js | 1 + src/Enum.js | 1 + src/EnumLike.js | 2 ++ src/util.js | 48 ------------------------------- 6 files changed, 66 insertions(+), 97 deletions(-) delete mode 100644 src/util.js diff --git a/src/BitArray.js b/src/BitArray.js index 63b65e5..bb0981c 100644 --- a/src/BitArray.js +++ b/src/BitArray.js @@ -1,5 +1,3 @@ -import { assertTrue, isInteger } from './util'; - /** * A {@link BitSet} implementation with no limit due to bits being stored in an array. Also known as bit set, bit map * or bit vector. This implementation will never throw out of bounds errors. @@ -24,8 +22,9 @@ class BitArray { * @throws {Error} In case 'minLength' is equals to or smaller than zero. */ constructor(minLength) { - assertTrue(minLength === undefined || minLength > 0, - 'Illegal argument: parameter \'minLength\' must be larger than 0'); + if (minLength !== undefined && minLength <= 0) { + throw Error('Illegal argument: parameter \'minLength\' must be larger than 0'); + } minLength = minLength || 1; @@ -118,15 +117,13 @@ class BitArray { } get(index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - return this.value[index] || false; } getRange(from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } const length = to - from; const bitArray = new BitArray(); @@ -168,7 +165,6 @@ class BitArray { } testAt(value, index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); if (index > this.length) { return Boolean(value) === false; @@ -212,8 +208,6 @@ class BitArray { } setAt(value, index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - while (index >= this.length) { this.value.push(false); } @@ -224,9 +218,9 @@ class BitArray { } setRange(value, from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } for (let i = from; i < to; i++) { this.setAt(value, i); @@ -258,8 +252,6 @@ class BitArray { } flipAt(index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - while (index >= this.length) { this.value.push(false); } @@ -270,9 +262,9 @@ class BitArray { } flipRange(from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } for (let i = from; i < to; i++) { this.flipAt(i); @@ -337,13 +329,13 @@ class BitArray { * @returns {BitArray} A new instance. */ static deserialize(input) { - const array = input.split(''); + const array = input.split('').map(Number); if (array.some(isNaN)) { throw new Error('Failed to deserialize input'); } - return BitArray.fromArray(array.map(Number)); + return BitArray.fromArray(array); } clone() { diff --git a/src/BitField.js b/src/BitField.js index 804ec05..65d43ec 100644 --- a/src/BitField.js +++ b/src/BitField.js @@ -1,5 +1,3 @@ -import { assertTrue, isInteger, withinRange } from './util'; - /** * A {@link BitSet} implementation limited to 31 bits due to bits being stored in a Number type. * This implementation is about 25% faster than a BitArray. @@ -37,8 +35,9 @@ class BitField { * @throws {Error} In case 'minLength' is equals to or smaller than zero. */ constructor(minLength) { - assertTrue(minLength === undefined || minLength > 0, - 'Illegal argument: parameter \'minLength\' must be larger than 0'); + if (minLength !== undefined && minLength <= 0) { + throw Error('Illegal argument: parameter \'minLength\' must be larger than 0'); + } this.minLength = minLength || 1; @@ -143,18 +142,25 @@ class BitField { } get(index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds'); + if (index < 0 || index > 31) { + throw Error('Illegal argument: parameter \'index\' is out of bounds'); + } return Boolean((this.value >> index) & 1); } getRange(from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds'); - assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (from < 0 || from > 31) { + throw Error('Illegal argument: parameter \'from\' is out of bounds'); + } + + if (to < 0 || to > 31) { + throw Error('Illegal argument: parameter \'to\' is out of bounds'); + } + + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } const length = to - from; const mask = (1 << length) - 1; @@ -177,8 +183,9 @@ class BitField { } testAt(value, index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds'); + if (index < 0 || index > 31) { + throw Error('Illegal argument: parameter \'index\' is out of bounds'); + } return this.get(index) === Boolean(value); } @@ -220,8 +227,9 @@ class BitField { } setAt(value, index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds'); + if (index < 0 || index > 31) { + throw Error('Illegal argument: parameter \'index\' is out of bounds'); + } const mask = 1 << index; @@ -229,11 +237,17 @@ class BitField { } setRange(value, from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds'); - assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (from < 0 || from > 31) { + throw Error('Illegal argument: parameter \'from\' is out of bounds'); + } + + if (to < 0 || to > 31) { + throw Error('Illegal argument: parameter \'to\' is out of bounds'); + } + + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } let mask = (1 << (to - from)) - 1; @@ -257,8 +271,9 @@ class BitField { } flipAt(index) { - assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer'); - assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds'); + if (index < 0 || index > 31) { + throw Error('Illegal argument: parameter \'index\' is out of bounds'); + } const mask = 1 << index; @@ -266,11 +281,17 @@ class BitField { } flipRange(from, to) { - assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer'); - assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer'); - assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds'); - assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds'); - assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + if (from < 0 || from > 31) { + throw Error('Illegal argument: parameter \'from\' is out of bounds'); + } + + if (to < 0 || to > 31) { + throw Error('Illegal argument: parameter \'to\' is out of bounds'); + } + + if (to <= from) { + throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\''); + } let mask = (1 << (to - from)) - 1; diff --git a/src/BitFlags.js b/src/BitFlags.js index 49c2826..014a669 100644 --- a/src/BitFlags.js +++ b/src/BitFlags.js @@ -1,6 +1,7 @@ import BitArray from './BitArray'; import BitField from './BitField'; import EnumConstant from './EnumConstant'; +import EnumLike from './EnumLike'; /** * A BitFlags instance consists of a set of predefined values, each representing a binary digit that acts as a flag diff --git a/src/Enum.js b/src/Enum.js index 2c98c73..e59a4ae 100644 --- a/src/Enum.js +++ b/src/Enum.js @@ -1,4 +1,5 @@ import EnumConstant from './EnumConstant'; +import EnumLike from './EnumLike'; /** * An Enum consists of a set of predefined constants. These constants are accessible directly on the instance. diff --git a/src/EnumLike.js b/src/EnumLike.js index 3fbe7af..74d066b 100644 --- a/src/EnumLike.js +++ b/src/EnumLike.js @@ -7,6 +7,8 @@ */ /** + * The amount of values/constants in this instance. + * * @readonly * @member {Number} * @name EnumLike#length diff --git a/src/util.js b/src/util.js deleted file mode 100644 index 40f1e96..0000000 --- a/src/util.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @private - * @param {Boolean} expression - * @param {String} message Error description. - * @throws {Error} In case expression evaluates to false. - */ -export function assertTrue(expression, message) { - if (!expression) { - throw Error(message); - } -} - -/** - * Checks whether the input value is a number. Anything that could be parsed as a number will yield false. - * Example: The string '1' yields false. - * - * @private - * @param value - * @returns {Boolean} - */ -export function isNumber(value) { - return typeof value === 'number'; -} - -/** - * Checks whether the input value is a integer. Anything that could be parsed as a number will yield false. - * Example: The string '1' yields false. The number '1.0' yields true. The number '1.1' yields false. - * - * @private - * @param value - * @returns {Boolean} - */ -export function isInteger(value) { - return isNumber(value) && Math.floor(value) === value; -} - -/** - * Checks whether the input number is within a specific range. - * - * @private - * @param {Number} number - * @param {Number} from The inclusive lower bounds of the range. - * @param {Number} to The inclusive upper bounds of the range. - * @returns {Boolean} - */ -export function withinRange(number, from, to) { - return number >= from && number <= to; -}