From eb392e257ed25b30154a0b78d13220acf49db312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20F=C3=A4cke?= Date: Tue, 15 Oct 2019 13:26:28 +0200 Subject: [PATCH] add typescript types --- .gitignore | 1 + .npmignore | 1 - .npmrc | 1 + lib/bit-array.js | 225 ------------------------------ lib/bit-typed-array.js | 309 +++++++++++++++++++++-------------------- package.json | 54 ++++--- test/bit-array-test.js | 88 ++++++------ types.d.ts | 24 ++++ 8 files changed, 263 insertions(+), 440 deletions(-) create mode 100644 .gitignore delete mode 100644 .npmignore create mode 100644 .npmrc delete mode 100644 lib/bit-array.js create mode 100644 types.d.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 40b878d..0000000 --- a/.npmignore +++ /dev/null @@ -1 +0,0 @@ -node_modules/ \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..9cf9495 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false \ No newline at end of file diff --git a/lib/bit-array.js b/lib/bit-array.js deleted file mode 100644 index fa88936..0000000 --- a/lib/bit-array.js +++ /dev/null @@ -1,225 +0,0 @@ -/** - * JavaScript BitArray - v0.2.0 - * - * Licensed under the revised BSD License. - * Copyright 2010-2012 Bram Stein - * All rights reserved. - */ - -/** - * Creates a new empty BitArray with the given length or initialises the BitArray with the given hex representation. - */ -var BitArray = function (size, hex) { - this.values = []; - - if (hex) { - hex = hex.slice(/^0x/.exec(hex) ? 2 : 0); - - if (hex.length * 4 > this.length) { - throw 'Hex value is too large for this bit array.' - } else if (hex.length * 4 < this.length) { - // pad it - while(hex.length * 4 < this.length) { - hex = '0' + hex; - } - } - - for (var i = 0; i < (hex.length / 8); i++) { - var slice = hex.slice(i * 8, i * 8 + 8); - this.values[i] = parseInt(slice, 16); - } - } else { - for (var i = 0; i < Math.ceil(size / 32); i += 1) { - this.values[i] = 0; - } - } -}; - -/** - * Returns the total number of bits in this BitArray. - */ -BitArray.prototype.size = function () { - return this.values.length * 32; -}; - -/** - * Sets the bit at index to a value (boolean.) - */ -BitArray.prototype.set = function (index, value) { - var i = Math.floor(index / 32); - // Since "undefined | 1 << index" is equivalent to "0 | 1 << index" we do not need to initialise the array explicitly here. - if (value) { - this.values[i] |= 1 << index - i * 32; - } else { - this.values[i] &= ~(1 << index - i * 32); - } - return this; -}; - -/** - * Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on. - */ -BitArray.prototype.toggle = function (index) { - var i = Math.floor(index / 32); - this.values[i] ^= 1 << index - i * 32; - return this; -}; - -/** - * Returns the value of the bit at index (boolean.) - */ -BitArray.prototype.get = function (index) { - var i = Math.floor(index / 32); - return !!(this.values[i] & (1 << index - i * 32)); -}; - -/** - * Resets the BitArray so that it is empty and can be re-used. - */ -BitArray.prototype.reset = function () { - this.values = []; - return this; -}; - -/** - * Returns a copy of this BitArray. - */ -BitArray.prototype.copy = function () { - var cp = new BitArray(); - cp.length = this.length; - cp.values = [].concat(this.values); - return cp; -}; - -/** - * Returns true if this BitArray equals another. Two BitArrays are considered - * equal if both have the same length and bit pattern. - */ -BitArray.prototype.equals = function (x) { - return this.values.length === x.values.length && - this.values.every(function (value, index) { - return value === x.values[index]; - }); -}; - -/** - * Returns the JSON representation of this BitArray. - */ -BitArray.prototype.toJSON = function () { - return JSON.stringify(this.values); -}; - -/** - * Returns a string representation of the BitArray with bits - * in logical order. - */ -BitArray.prototype.toString = function () { - return this.toArray().map(function (value) { - return value ? '1' : '0'; - }).join(''); -}; - -/** - * Returns a string representation of the BitArray with bits - * in mathemetical order. - */ -BitArray.prototype.toBinaryString = function () { - return this.toArray().map(function (value) { - return value ? '1' : '0'; - }).reverse().join(''); -}; - -/** - * Returns a hexadecimal string representation of the BitArray - * with bits in logical order. - */ -BitArray.prototype.toHexString = function () { - var result = []; - - for (var i = 0; i < this.values.length; i += 1) { - result.push(('00000000' + (this.values[i] >>> 0).toString(16)).slice(-8)); - } - return result.join(''); -}; - -/** - * Convert the BitArray to an Array of boolean values. - */ -BitArray.prototype.toArray = function () { - var result = []; - - for (var i = 0; i < this.values.length * 32; i += 1) { - result.push(this.get(i)); - } - return result; -}; - -/** - * Returns the total number of bits set to one in this BitArray. - */ -BitArray.prototype.count = function () { - var total = 0; - - // If we remove the toggle method we could efficiently cache the number of bits without calculating it on the fly. - this.values.forEach(function (x) { - // See: http://bits.stephan-brumme.com/countBits.html for an explanation - x = x - ((x >> 1) & 0x55555555); - x = (x & 0x33333333) + ((x >> 2) & 0x33333333); - x = x + (x >> 4); - x &= 0xF0F0F0F; - - total += (x * 0x01010101) >> 24; - }); - return total; -}; - -/** - * Inverts this BitArray. - */ -BitArray.prototype.not = function () { - this.values = this.values.map(function (v) { - return ~v; - }); - return this; -}; - -/** - * Bitwise OR on the values of this BitArray using BitArray x. - */ -BitArray.prototype.or = function (x) { - if (this.values.length !== x.values.length) { - throw 'Arguments must be of the same length.'; - } - this.values = this.values.map(function (v, i) { - return v | x.values[i]; - }); - return this; -}; - -/** - * Bitwise AND on the values of this BitArray using BitArray x. - */ -BitArray.prototype.and = function (x) { - if (this.values.length !== x.values.length) { - throw 'Arguments must be of the same length.'; - } - this.values = this.values.map(function (v, i) { - return v & x.values[i]; - }); - return this; -}; - -/** - * Bitwise XOR on the values of this BitArray using BitArray x. - */ -BitArray.prototype.xor = function (x) { - if (this.values.length !== x.values.length) { - throw 'Arguments must be of the same length.'; - } - this.values = this.values.map(function (v, i) { - return v ^ x.values[i]; - }); - return this; -}; - -module.exports = BitArray; diff --git a/lib/bit-typed-array.js b/lib/bit-typed-array.js index 5537857..e104889 100644 --- a/lib/bit-typed-array.js +++ b/lib/bit-typed-array.js @@ -9,239 +9,244 @@ /** * Creates a new empty BitArray with the given length or initialises the BitArray with the given hex representation. */ -var BitArray = function (size, hex) { +'use strict' + +class BitArray { + constructor(size, hex) { this.length = size; this.buffer = new ArrayBuffer(Math.ceil(this.length / 32) * 4); this.wordArray = new Uint32Array(this.buffer); if (hex) { - hex = hex.slice(/^0x/.exec(hex) ? 2 : 0); - - if (hex.length * 4 > this.length) { - throw 'Hex value is too large for this bit array.' - } else if (hex.length * 4 < this.length) { - // pad it - while(hex.length * 4 < this.length) { - hex = '0' + hex; - } + hex = hex.slice(/^0x/.exec(hex) ? 2 : 0); + + if (hex.length * 4 > this.length) { + throw 'Hex value is too large for this bit array.' + } else if (hex.length * 4 < this.length) { + // pad it + while (hex.length * 4 < this.length) { + hex = '0' + hex; } + } - for (var i = 0; i < (hex.length / 8); i++) { - var slice = hex.slice(i * 8, i * 8 + 8); - this.wordArray[i] = parseInt(slice, 16); - } + for (var i = 0; i < (hex.length / 8); i++) { + var slice = hex.slice(i * 8, i * 8 + 8); + this.wordArray[i] = parseInt(slice, 16); + } } -}; + } -/** - * Returns the total number of bits in this BitArray. - */ -BitArray.prototype.size = function() { + /** + * Returns the total number of bits in this BitArray. + */ + size() { return this.length; -}; + }; -/** - * Sets the bit at index to a value (boolean.) - */ -BitArray.prototype.set = function(index, value) { + /** + * Sets the bit at index to a value (boolean.) + */ + set(index, value) { if (arguments.length !== 2) { - throw 'Index and value are required arguments.'; + throw 'Index and value are required arguments.'; } if (index > this.length - 1) { - throw 'Index too large.' + index + ' ' + this.length; + throw 'Index too large.' + index + ' ' + this.length; } var wordOffset = Math.floor(index / 32); // The underlying byte buffer will be initialized to zeros. var bitOffset = index - wordOffset * 32; if (value) { - this.wordArray[wordOffset] |= (1 << bitOffset); + this.wordArray[wordOffset] |= (1 << bitOffset); } else { - this.wordArray[wordOffset] &= ~(1 << bitOffset); + this.wordArray[wordOffset] &= ~(1 << bitOffset); } return this; -}; + }; -/** - * Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on. - */ -BitArray.prototype.toggle = function(index) { + /** + * Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on. + */ + toggle(index) { if (index > this.length - 1) { - throw 'Index too large.'; + throw 'Index too large.'; } var wordOffset = Math.floor(index / 32); var bitOffset = index - wordOffset * 32; this.wordArray[wordOffset] ^= 1 << bitOffset; return this; -}; + }; -/** - * Returns the value of the bit at index (boolean.) - */ -BitArray.prototype.get = function(index) { + /** + * Returns the value of the bit at index (boolean.) + */ + get(index) { if (index > this.length - 1) { - throw 'Index too large.'; + throw 'Index too large.'; } var wordOffset = Math.floor(index / 32); var bitOffset = index - wordOffset * 32; - return !! (this.wordArray[wordOffset] & (1 << bitOffset)); -}; + return !!(this.wordArray[wordOffset] & (1 << bitOffset)); + }; -/** - * Resets the BitArray so that it is empty and can be re-used. - */ -BitArray.prototype.reset = function() { + /** + * Resets the BitArray so that it is empty and can be re-used. + */ + reset() { this.buffer = new ArrayBuffer(Math.ceil(this.length / 32) * 4); this.wordArray = new Uint32Array(this.buffer); return this; -}; + }; -/** - * Returns a copy of this BitArray. - */ -BitArray.prototype.copy = function() { + /** + * Returns a copy of this BitArray. + */ + copy() { var cp = new BitArray(this.length); for (var i = 0; i < this.wordArray.length; i++) { - cp.wordArray[i] = this.wordArray[i]; + cp.wordArray[i] = this.wordArray[i]; } return cp; -}; + }; -/** - * Returns true if this BitArray equals another. Two BitArrays are considered - * equal if both have the same length and bit pattern. - */ -BitArray.prototype.equals = function(x) { + /** + * Returns true if this BitArray equals another. Two BitArrays are considered + * equal if both have the same length and bit pattern. + */ + equals(x) { if (this.length !== x.length) { - return false; + return false; } for (var i = 0; i < this.wordArray.length; i++) { - if (this.wordArray[i] !== x.wordArray[i]) { - return false; - } + if (this.wordArray[i] !== x.wordArray[i]) { + return false; + } } return true; -}; + }; -/** - * Returns the JSON representation of this BitArray. - */ -BitArray.prototype.toJSON = function() { + /** + * Returns the JSON representation of this BitArray. + */ + toJSON() { return JSON.stringify(this.toArray()); -}; - -/** - * Returns a string representation of the BitArray with bits - * in mathemetical order. - */ -BitArray.prototype.toBinaryString = function () { - return this.toArray().map(function (value) { - return value ? '1' : '0'; - }).reverse().join(''); -}; - -/** - * Returns a hexadecimal string representation of the BitArray - * with bits in logical order. - */ -BitArray.prototype.toHexString = function () { - var result = []; - - for (var i = 0; i < this.wordArray.length; i += 1) { - //result.push(this.wordArray[i].toString(16)); - result.push(('00000000' + (this.wordArray[i] >>> 0).toString(16)).slice(-8)); + }; + + /** + * Returns a string representation of the BitArray with bits + * in mathemetical order. + */ + toBinaryString() { + return this.toArray().map(function (value) { + return value ? '1' : '0'; + }).reverse().join(''); + }; + + /** + * Returns a hexadecimal string representation of the BitArray + * with bits in logical order. + */ + toHexString() { + const result = []; + + for (let i = 0; i < this.wordArray.length; i += 1) { + //result.push(this.wordArray[i].toString(16)); + result.push(('00000000' + (this.wordArray[i] >>> 0).toString(16)).slice(-8)); } return result.join(''); -}; - -/** - * Returns a string representation of the BitArray with bits - * in logical order. - */ -BitArray.prototype.toString = function() { - return this.toArray().map(function(value) { - return value ? '1': '0'; + }; + + /** + * Returns a string representation of the BitArray with bits + * in logical order. + */ + toString() { + return this.toArray().map(function (value) { + return value ? '1' : '0'; }).join(''); -}; + }; -/** - * Convert the BitArray to an Array of boolean values (slow). - */ -BitArray.prototype.toArray = function() { - var result = []; - for (var i = 0; i < this.length; i++) { - result.push(Boolean(this.get(i))); + /** + * Convert the BitArray to an Array of boolean values (slow). + */ + toArray() { + const result = []; + for (let i = 0; i < this.length; i++) { + result.push(Boolean(this.get(i))); } return result; -}; + }; -/** - * Returns the total number of bits set to one in this BitArray. - */ -BitArray.prototype.count = function() { - var total = 0; + /** + * Returns the total number of bits set to one in this BitArray. + */ + count() { + let total = 0; for (var i = 0; i < this.wordArray.length; i++) { - x = this.wordArray[i]; - // count bits of each 2-bit chunk - x = x - ((x >> 1) & 0x55555555); - // count bits of each 4-bit chunk - x = (x & 0x33333333) + ((x >> 2) & 0x33333333); - // count bits of each 8-bit chunk - x = x + (x >> 4); - // mask out junk - x &= 0xF0F0F0F; - // add all four 8-bit chunks - total += (x * 0x01010101) >> 24; + let x = this.wordArray[i]; + // count bits of each 2-bit chunk + x = x - ((x >> 1) & 0x55555555); + // count bits of each 4-bit chunk + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + // count bits of each 8-bit chunk + x = x + (x >> 4); + // mask out junk + x &= 0xF0F0F0F; + // add all four 8-bit chunks + total += (x * 0x01010101) >> 24; } return total; -}; + }; -/** - * Inverts this BitArray. - */ -BitArray.prototype.not = function() { + /** + * Inverts this BitArray. + */ + not() { for (var i = 0; i < this.wordArray.length; i++) { - this.wordArray[i] = ~(this.wordArray[i]); + this.wordArray[i] = ~(this.wordArray[i]); } return this; -}; + }; -/** - * Bitwise OR on the values of this BitArray using BitArray x. - */ -BitArray.prototype.or = function(x) { + /** + * Bitwise OR on the values of this BitArray using BitArray x. + */ + or(x) { if (this.length !== x.length) { - throw 'Arguments must be of the same length.'; + throw 'Arguments must be of the same length.'; } for (var i = 0; i < this.wordArray.length; i++) { - this.wordArray[i] |= x.wordArray[i]; + this.wordArray[i] |= x.wordArray[i]; } return this; -}; + }; -/** - * Bitwise AND on the values of this BitArray using BitArray x. - */ -BitArray.prototype.and = function(x) { + /** + * Bitwise AND on the values of this BitArray using BitArray x. + */ + and(x) { if (this.length !== x.length) { - throw 'Arguments must be of the same length.'; + throw 'Arguments must be of the same length.'; } for (var i = 0; i < this.wordArray.length; i++) { - this.wordArray[i] &= x.wordArray[i]; + this.wordArray[i] &= x.wordArray[i]; } return this; -}; + }; -/** - * Bitwise XOR on the values of this BitArray using BitArray x. - */ -BitArray.prototype.xor = function(x) { + /** + * Bitwise XOR on the values of this BitArray using BitArray x. + */ + xor(x) { if (this.length !== x.length) { - throw 'Arguments must be of the same length.'; + throw 'Arguments must be of the same length.'; } for (var i = 0; i < this.wordArray.length; i++) { - this.wordArray[i] ^= x.wordArray[i]; + this.wordArray[i] ^= x.wordArray[i]; } return this; -}; + }; +} -module.exports = BitArray; +module.exports = BitArray +module.exports.default = BitArray diff --git a/package.json b/package.json index 9b2b0a1..8ff4650 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,34 @@ { - "name": "bit-array", - "description": "JavaScript implementation of bit arrays", - "version": "0.2.2", - "author": "Bram Stein (http://www.bramstein.com)", - "repository": { - "type": "git", - "url": "git://github.com/bramstein/bit-array.git" - }, - "keywords": ["bit array", "bit-array", "bit vector", "bitset", "bitmap", "bitstring"], - "main": "./lib/bit-typed-array.js", - "directories": { - "lib": "./lib" - }, - "devDependencies": { - "mocha": "=1.10.0", - "expect.js": "=0.2.0" - }, - "scripts": { - "test": "mocha --reporter spec" - } -} + "name": "bit-array", + "description": "JavaScript implementation of bit arrays", + "version": "0.2.2", + "author": "Bram Stein (http://www.bramstein.com)", + "repository": { + "type": "git", + "url": "git://github.com/bramstein/bit-array.git" + }, + "keywords": [ + "bit array", + "bit-array", + "bit vector", + "bitset", + "bitmap", + "bitstring" + ], + "main": "./lib/bit-typed-array.js", + "devDependencies": { + "expect.js": "^0.3.1", + "mocha": "^6.2.1" + }, + "scripts": { + "test": "mocha --reporter spec" + }, + "engines": { + "node": ">=8.0.0" + }, + "types": "types.d.ts", + "files": [ + "lib", + "types.d.ts" + ] +} \ No newline at end of file diff --git a/test/bit-array-test.js b/test/bit-array-test.js index 7ff1317..f88cac4 100644 --- a/test/bit-array-test.js +++ b/test/bit-array-test.js @@ -1,5 +1,7 @@ -var BitArray = require('../lib/bit-typed-array.js'), - expect = require('expect.js'); +'use strict'; + +const BitArray = require('../lib/bit-typed-array.js'); +const expect = require('expect.js'); describe('BitArray', function () { it('should create an empty bit array', function () { @@ -8,40 +10,42 @@ describe('BitArray', function () { }); it('should parse hex given to the constructor', function () { - var b = new BitArray(32, 'deadbeef'); + const b = new BitArray(32, 'deadbeef'); expect(b.toBinaryString()).to.eql('11011110101011011011111011101111'); expect(b.toHexString()).to.eql('deadbeef'); - var c = new BitArray(64, '0000c0ffeec0ffee').and(new BitArray(64, 'ffffffffff000000')); + const c = new BitArray(64, '0000c0ffeec0ffee').and( + new BitArray(64, 'ffffffffff000000') + ); expect(c.toHexString()).to.eql('0000c0ffee000000'); }); it('should correctly pad hex given in the constructor', function () { - var b = new BitArray(32, 'f'); + const b = new BitArray(32, 'f'); - expect(b.toHexString()).to.eql('0000000f'); - expect(b.toBinaryString()).to.eql('00000000000000000000000000001111'); + expect(b.toHexString()).to.eql('0000000f'); + expect(b.toBinaryString()).to.eql('00000000000000000000000000001111'); }); it('should correctly convert a bit array to hex', function () { - var b = new BitArray(32); + const b = new BitArray(32); - b.set(5, true); - b.set(6, true); + b.set(5, true); + b.set(6, true); - expect(b.toHexString()).to.eql('00000060'); - expect(b.toBinaryString()).to.eql('00000000000000000000000001100000'); + expect(b.toHexString()).to.eql('00000060'); + expect(b.toBinaryString()).to.eql('00000000000000000000000001100000'); - var c = new BitArray(32, b.toHexString()); + const c = new BitArray(32, b.toHexString()); - expect(c.toHexString()).to.eql('00000060'); - expect(c.toBinaryString()).to.eql('00000000000000000000000001100000'); + expect(c.toHexString()).to.eql('00000060'); + expect(c.toBinaryString()).to.eql('00000000000000000000000001100000'); }); it('should set individual bits', function () { - var b = new BitArray(32); + const b = new BitArray(32); b.set(31, true); // 0 | 1 << 30 expect(b.size()).to.eql(32); @@ -61,27 +65,29 @@ describe('BitArray', function () { expect(b.toString()).to.eql('10000000000000000000000000000001'); // Reset bit 0 to false - b.set(0, false) // 0 | 1 << 31 + b.set(0, false); // 0 | 1 << 31 expect(b.size()).to.eql(32); expect(b.toString()).to.eql('00000000000000000000000000000001'); // Reset bit 31 to false - b.set(31, false) // 0 + b.set(31, false); // 0 expect(b.size()).to.eql(32); expect(b.toString()).to.eql('00000000000000000000000000000000'); }); it('should be able to set bits beyond a single integer', function () { - var b = new BitArray(64); + const b = new BitArray(64); b.set(32, true); expect(b.size()).to.eql(64); - expect(b.toString()).to.eql('0000000000000000000000000000000010000000000000000000000000000000'); + expect(b.toString()).to.eql( + '0000000000000000000000000000000010000000000000000000000000000000' + ); }); it('should get individual bits', function () { - var b = new BitArray(32); + const b = new BitArray(32); b.set(0, true); b.set(4, true); b.set(31, true); @@ -100,7 +106,7 @@ describe('BitArray', function () { }); it('should toggle individual bits', function () { - var b = new BitArray(32); + const b = new BitArray(32); b.set(0, true); b.set(31, true); @@ -110,7 +116,7 @@ describe('BitArray', function () { }); it('should toggle individual bits beyond a single integer', function () { - var b = new BitArray(64); + const b = new BitArray(64); b.set(32, true); expect(b.size()).to.eql(64); @@ -119,13 +125,13 @@ describe('BitArray', function () { }); it('should report the correct size', function () { - var b = new BitArray(224); + const b = new BitArray(224); b.set(200, true); // Math.floor(200 / 32) + 1 * 32; expect(b.size()).to.eql(224); }); it('should count the individual on bits', function () { - var b = new BitArray(72); + const b = new BitArray(72); b.set(32, true); b.set(70, true); @@ -136,10 +142,10 @@ describe('BitArray', function () { }); it('should be able to compare bit arrays', function () { - var a = new BitArray(224), - b = new BitArray(224), - c = new BitArray(224), - d = new BitArray(224); + const a = new BitArray(224); + const b = new BitArray(224); + const c = new BitArray(224); + const d = new BitArray(224); a.set(0, true); a.set(1, true); @@ -165,8 +171,8 @@ describe('BitArray', function () { }); it('should copy a bit array', function () { - var a = new BitArray(224), - b; + const a = new BitArray(224); + let b; a.set(0, true); a.set(1, true); @@ -178,8 +184,8 @@ describe('BitArray', function () { }); it('should negate bit arrays', function () { - var b = new BitArray(64), - a; + const b = new BitArray(64); + let a; b.set(2, true); b.set(7, true); @@ -189,7 +195,7 @@ describe('BitArray', function () { a = b.toString(); a = a.replace(/0/g, 'x'); - a = a.replace(/1/g, '0'); + a = a.replace(/1/g, '0'); a = a.replace(/x/g, '1'); b.not(); @@ -197,10 +203,10 @@ describe('BitArray', function () { }); it('should or bit arrays', function () { - var a = new BitArray(32), - b = new BitArray(32); + const a = new BitArray(32); + const b = new BitArray(32); - a.set(0, true);//1110 + a.set(0, true); //1110 a.set(1, false); a.set(2, true); a.set(3, false); @@ -214,8 +220,8 @@ describe('BitArray', function () { }); it('should and bit arrays', function () { - var a = new BitArray(32), - b = new BitArray(32); + const a = new BitArray(32); + const b = new BitArray(32); a.set(0, true); a.set(1, false); @@ -231,8 +237,8 @@ describe('BitArray', function () { }); it('should xor bit arrays', function () { - var a = new BitArray(32), - b = new BitArray(32); + const a = new BitArray(32); + const b = new BitArray(32); a.set(0, true); a.set(1, false); diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..79ee868 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,24 @@ +declare module 'bit-array' { + class BitArray { + constructor(size: number, hex?: boolean) + size(): number + set(index: number, value: boolean): BitArray + toggle(index: number): BitArray + get(index: number): boolean + reset(): BitArray + copy(): BitArray + equals(x: BitArray): boolean + toJSON(): string + toString(): string + toBinaryString(): string + toHexString(): string + toArray(): boolean[] + count(): number + not(): BitArray + or(x: BitArray): BitArray + and(x: BitArray): BitArray + xor(x: BitArray): BitArray + } +} + +export default BitArray \ No newline at end of file