fast-boolean-array 1.4.3 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +11 -0
- package/README.md +22 -25
- package/SECURITY.md +5 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +52 -49
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "npm" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
package/README.md
CHANGED
|
@@ -2,16 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
In JavaScript, when working with large arrays of boolean values, a common challenge is efficiently indexing and retrieving these values. Using a regular JavaScript array to store booleans is straightforward, but it is memory-inefficient. While booleans are conceptually 1-bit values, JavaScript engines, like V8 (in Chrome and Node.js), allocate 1 byte (8 bits) per boolean for optimization purposes. This can waste a significant amount of memory when dealing with large arrays.
|
|
4
4
|
|
|
5
|
-
Fast Boolean Array solves this issue by utilizing bit manipulation to store booleans in a compact format. It uses a Uint8Array and stores each boolean as a single bit within the 8 bits of each byte. This allows you to index and retrieve boolean values by integers (e.g., the 0th boolean, 1st boolean, etc.) while only using a fraction of the memory.
|
|
5
|
+
Fast Boolean Array solves this issue by utilizing bit manipulation to store booleans in a compact format. It uses a Uint8Array and stores each boolean as a single bit within the 8 bits of each byte. This allows you to index and retrieve boolean values by integers (e.g., the 0th boolean, 1st boolean, etc.) while only using a fraction of the memory.
|
|
6
6
|
|
|
7
7
|
For detailed benchmark results, see below.
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
+
|
|
11
12
|
- **Memory Efficiency**: Stores booleans in bits rather than bytes, drastically reducing memory usage.
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
13
|
+
- **Fast Set Performance**: Up to 10x faster sets for fast data re-mapping.
|
|
14
|
+
- **Familiar API**: `Map` and `Set` like API for minimal learning curve. Intuitive helper functions, and works in `for.. of`.
|
|
15
|
+
- **Array like interface**: call `.accessLikeArray()` to access it like an array`someArray[index]` (beware of the performance penalty caused by the Proxy)!
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Why Use Fast Boolean Array?
|
|
14
19
|
|
|
20
|
+
- **Easy to Use**: Familiar `Map` and `Set` like API for minimal learning curve.
|
|
21
|
+
- **Scalable**: Suitable for high-performance, memory-sensitive projects.
|
|
15
22
|
---
|
|
16
23
|
|
|
17
24
|
## Installation
|
|
@@ -19,7 +26,8 @@ For detailed benchmark results, see below.
|
|
|
19
26
|
Install the package via npm:
|
|
20
27
|
|
|
21
28
|
```bash
|
|
22
|
-
npm install fast-boolean-array
|
|
29
|
+
npm install fast-boolean-array --save-dev
|
|
30
|
+
pnpm install fast-boolean-array --save-dev
|
|
23
31
|
```
|
|
24
32
|
|
|
25
33
|
---
|
|
@@ -30,7 +38,7 @@ Here's how to use the Fast Boolean Array:
|
|
|
30
38
|
|
|
31
39
|
```javascript
|
|
32
40
|
import BooleanArray from 'fast-boolean-array';
|
|
33
|
-
// const BooleanArray = require('fast-boolean-array'); works too
|
|
41
|
+
// const BooleanArray = require('fast-boolean-array'); commonjs works too.
|
|
34
42
|
|
|
35
43
|
// Create a new BooleanArray with the desired size
|
|
36
44
|
const booleans = new BooleanArray(2);
|
|
@@ -81,14 +89,6 @@ Gets the boolean value at the specified index.
|
|
|
81
89
|
|
|
82
90
|
---
|
|
83
91
|
|
|
84
|
-
## Why Use Fast Boolean Array?
|
|
85
|
-
|
|
86
|
-
- **Efficient Memory Usage**: Ideal for applications handling large boolean arrays, such as data compression or state tracking.
|
|
87
|
-
- **Easy to Use**: Familiar `Map` and `Set` like API for minimal learning curve.
|
|
88
|
-
- **Scalable**: Suitable for high-performance, memory-sensitive projects.
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
92
|
# Performance Breakdown
|
|
93
93
|
|
|
94
94
|
Our benchmark compares the performance of our Fast Boolean Array library against Vanilla JavaScript arrays in terms of get and set operations across varying numbers of Booleans. The results below reflect an updated benchmark algorithm that performs 1,000 runs for each x amount of Booleans to better simulate real-world scenarios.
|
|
@@ -104,7 +104,7 @@ Our benchmark compares the performance of our Fast Boolean Array library against
|
|
|
104
104
|
| Get Vanilla Array | 0.00053000 | N/A | 1 |
|
|
105
105
|
| Get Fast Boolean Array | 0.00291000 | N/A | 1 |
|
|
106
106
|
|
|
107
|
-
|
|
107
|
+
As useless as having just 1 boolean stored in an array might be, For **1 boolean**, the **Fast Boolean Array** is **1.8x slower** for **Set** and **5.5x slower** for **Get** operations. Memory usage is nearly identical, with a small advantage for the **Fast Boolean Array** in terms of bytes.
|
|
108
108
|
|
|
109
109
|
---
|
|
110
110
|
|
|
@@ -130,7 +130,7 @@ Our benchmark compares the performance of our Fast Boolean Array library against
|
|
|
130
130
|
| Get Vanilla Array | 0.01261000 | N/A | 1000 |
|
|
131
131
|
| Get Fast Boolean Array | 0.05476000 | N/A | 1000 |
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
For **1,000 booleans**, **Fast Boolean Array** is **2.1x faster** for **Set** operations and uses **7.7x less memory**. However, **Fast Boolean Array**'s' **Get** operation is **4.3x slower** . compared to the **Vanilla Array**.
|
|
134
134
|
|
|
135
135
|
---
|
|
136
136
|
|
|
@@ -156,7 +156,7 @@ Our benchmark compares the performance of our Fast Boolean Array library against
|
|
|
156
156
|
| Get Vanilla Array | 0.04920000 | N/A | 100000 |
|
|
157
157
|
| Get Fast Boolean Array | 0.07523000 | N/A | 100000 |
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
For **100,000 booleans**, **Fast Boolean Array** is **10.9x faster** in **Set** operations and uses **8x less memory**. However, **Fast Boolean Array**'s' **Get** operation is **1.5x slower** .
|
|
160
160
|
|
|
161
161
|
---
|
|
162
162
|
|
|
@@ -169,7 +169,7 @@ Our benchmark compares the performance of our Fast Boolean Array library against
|
|
|
169
169
|
| Get Vanilla Array | 0.49531000 | N/A | 1000000 |
|
|
170
170
|
| Get Fast Boolean Array | 0.74617000 | N/A | 1000000 |
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
For **1,000,000 booleans**, the **Fast Boolean Array** is **5.9x faster** for **Set** operations and uses **8x less memory**. However, **Fast Boolean Array**'s' **Get** operation is **1.5x slower** .
|
|
173
173
|
|
|
174
174
|
---
|
|
175
175
|
|
|
@@ -182,22 +182,19 @@ Our benchmark compares the performance of our Fast Boolean Array library against
|
|
|
182
182
|
| Get Vanilla Array | 4.90792000 | N/A | 10000000 |
|
|
183
183
|
| Get Fast Boolean Array | 7.57038000 | N/A | 10000000 |
|
|
184
184
|
|
|
185
|
-
|
|
185
|
+
For **10,000,000 booleans**, **Fast Boolean Array** is **7.5x faster** for **Set** operations and uses **8x less memory**. However, **Fast Boolean Array**'s' **Get** operation is **1.5x slower** .
|
|
186
186
|
|
|
187
187
|
---
|
|
188
188
|
|
|
189
189
|
### Summary
|
|
190
190
|
|
|
191
191
|
- The **Fast Boolean Array** is significantly more efficient in terms of memory usage and **Set** operation speeds, particularly as the number of booleans increases.
|
|
192
|
-
- For example, at **10,000,000 booleans**, it is **7.5x faster** in **Set** operations and uses **8x less memory**.
|
|
193
|
-
- The **Get** operation, however, tends to be slower
|
|
194
|
-
- If memory efficiency and **Set** performance are priorities, the **Fast Boolean Array** is clearly advantageous, but if **Get** performance is more important, the **Vanilla Array** may still offer better results for certain use cases.
|
|
195
|
-
|
|
196
|
-
---
|
|
192
|
+
- For example, as stated previously, at **10,000,000 booleans**, it is **7.5x faster** in **Set** operations and uses **8x less memory**.
|
|
193
|
+
- The **Get** operation, however, tends to be slower on a **Fast Boolean Array**, with it being up to **1.5x slower** compared to the regular vanilla **boolean[]** in larger datasets.
|
|
197
194
|
|
|
198
|
-
|
|
195
|
+
If memory efficiency and **Set** performance are priorities, the **Fast Boolean Array** is clearly advantageous, but if **Get** performance is more important, the **Vanilla Array** may still offer better results for certain use cases.
|
|
199
196
|
|
|
200
|
-
|
|
197
|
+
We are still working on improving the performance of the toArray function, but you can already use it today to convert back to a vanilla array, though generating one from scratch is currently more effecient.
|
|
201
198
|
|
|
202
199
|
---
|
|
203
200
|
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Reporting a Vulnerability
|
|
4
|
+
|
|
5
|
+
If you discover a vulnerability, please contact a maintainer directly. Avoid creating an issue or starting a discussion. If you're able to identify the vulnerability, you may also be able to submit a pull request. We encourage you to do so. We will merge your fix and publish it as soon as possible to all package registries.
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var a=Object.defineProperty;var
|
|
1
|
+
var a=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var h=(n,e)=>{for(var r in e)a(n,r,{get:e[r],enumerable:!0})},b=(n,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of l(e))!f.call(n,s)&&s!==r&&a(n,s,{get:()=>e[s],enumerable:!(t=u(e,s))||t.enumerable});return n};var y=n=>b(a({},"__esModule",{value:!0}),n);var g={};h(g,{default:()=>i});module.exports=y(g);var o="Index out of bounds",i=class n{size;buffer;constructor(e){if(!e)throw new Error("FastBooleanArray must have a size greater than 0");this.size=e,this.buffer=new Uint8Array(Math.ceil(e/8))}set(e,r){return r?(this.buffer[e>>3]|=1<<(e&7),!0):(this.buffer[e>>3]&=~(1<<(e&7)),!1)}setSafe(e,r){if(e<0||e>=this.size)throw new RangeError(o);return this.set(e,r)}setAll(e){this.buffer.fill(e?255:0)}get(e){let r=e>>>3,t=e&7;return(this.buffer[r]&1<<t)!==0}getSafe(e){if(e<0||e>=this.size)throw new RangeError(o);return this.get(e)}resize(e){let r=new Uint8Array(Math.ceil(e/8));r.set(this.buffer.subarray(0,Math.min(this.buffer.length,r.length))),this.size=e,this.buffer=r}get length(){return this.size}equals(e){return this.size!==e.size?!1:this.buffer.every((r,t)=>r===e.buffer[t])}*[Symbol.iterator](){for(let e=0;e<this.size;e++)yield this.get(e)}toArray(){let e=new Array(this.size);for(let r=0;r<this.size;r++)e[r]=this.get(r);return e}accessLikeArray(){return new Proxy(this,{get:(e,r)=>{if(typeof r=="string"&&!isNaN(Number(r))){let t=Number(r);return e.get(t)}return e[r]},set:(e,r,t)=>{if(typeof r=="string"&&!isNaN(Number(r))){let s=Number(r);e.set(s,t)}else e[r]=t;return!0}})}forEach(e){for(let r=0;r<this.size;r++)e(this.get(r),r,this)}map(e){let r=new Array(this.size);for(let t=0;t<this.size;t++)r[t]=e(this.get(t),t,this);return r}filter(e){let r=[];for(let t=0;t<this.size;t++){let s=this.get(t);e(s,t,this)&&r.push(s)}return r}some(e){for(let r=0;r<this.size;r++)if(e(this.get(r),r,this))return!0;return!1}every(e){for(let r=0;r<this.size;r++)if(!e(this.get(r),r,this))return!1;return!0}reduce(e,r){let t=r;for(let s=0;s<this.size;s++)t=e(t,this.get(s),s,this);return t}toString(){let e="";for(let r=0;r<this.size;r++)e+=this.get(r)?"1":"0";return e}static fromString(e){let r=new n(e.length);for(let t=0;t<e.length;t++)r.set(t,e[t]=="1");return r}static fromArray(e){let r=new n(e.length);for(let t=0;t<e.length;t++)r.set(t,!!e[t]);return r}static from(e){return typeof e=="string"?n.fromString(e):n.fromArray(e)}};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tif (!size) {\r\n\t\t\tthrow new Error('FastBooleanArray must have a size greater than 0');\r\n\t\t}\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\t/**\r\n\t * Sets a boolean value at the specified index.\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t */\r\n\tset(index: number, value: boolean) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n\t\t\treturn true;\r\n\t\t} else {\r\n\t\t\tthis.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tsetSafe(index: number, value: boolean) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.set(index, value);\r\n\t}\r\n\r\n\t/**\r\n\t * Sets all bits to the specified boolean value.\r\n\t * @param {boolean} value - The boolean value to set all bits to.\r\n\t */\r\n\tsetAll(value: boolean) {\r\n\t\tthis.buffer.fill(value ? 0xff : 0x00);\r\n\t}\r\n\r\n\t/**\r\n\t * Gets a boolean value at the specified index.\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tget(index: number) {\r\n\t\tconst byteIndex = index >>> 3; // index divided by 8 (shifted)\r\n\t\tconst bitIndex = index & 7; // index modulo 8 (masking)\r\n\t\treturn (this.buffer[byteIndex] & (1 << bitIndex)) !== 0; // Direct comparison is faster than double negation !!\r\n\t}\r\n\r\n\t/**\r\n\t * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tgetSafe(index: number) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.get(index);\r\n\t}\r\n\r\n\t/**\r\n\t * Resizes the array to a new size, preserving existing data.\r\n\t * @param {number} newSize - The new size of the array.\r\n\t */\r\n\tresize(newSize: number) {\r\n\t\tconst newBuffer = new Uint8Array(Math.ceil(newSize / 8));\r\n\t\tnewBuffer.set(this.buffer.subarray(0, Math.min(this.buffer.length, newBuffer.length)));\r\n\t\tthis.buffer = newBuffer;\r\n\t\tthis.size = newSize;\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\t/**\r\n\t * Compares this FastBooleanArray with another for equality.\r\n\t * @param {FastBooleanArray} other - The other FastBooleanArray to compare.\r\n\t * @returns {boolean} True if both arrays are equal, false otherwise.\r\n\t */\r\n\tequals(other: FastBooleanArray) {\r\n\t\tif (this.size !== other.size) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\treturn this.buffer.every((byte, i) => byte === other.buffer[i]);\r\n\t}\r\n\r\n\t/**\r\n\t * Makes the array iterable using `for...of`.\r\n\t */\r\n\t*[Symbol.iterator]() {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tyield this.get(i);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Converts to a standard JavaScript array.\r\n\t */\r\n\ttoArray() {\r\n\t\tconst arr = new Array(this.size);\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tarr[i] = this.get(i);\r\n\t\t}\r\n\t\treturn arr;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a proxy to the FastBooleanArray instance\r\n\t * With this you can access indexes like on an actual array.\r\n\t */\r\n\taccessLikeArray() {\r\n\t\treturn new Proxy(this, {\r\n\t\t\tget: (target, prop) => {\r\n\t\t\t\tif (typeof prop === 'string' && !isNaN(Number(prop))) {\r\n\t\t\t\t\tconst index = Number(prop);\r\n\t\t\t\t\treturn target.get(index);\r\n\t\t\t\t}\r\n\t\t\t\treturn target[prop as keyof FastBooleanArray];\r\n\t\t\t},\r\n\t\t\tset: (target, prop, value) => {\r\n\t\t\t\tif (typeof prop === 'string' && !isNaN(Number(prop))) {\r\n\t\t\t\t\tconst index = Number(prop);\r\n\t\t\t\t\treturn target.set(index, value as never);\r\n\t\t\t\t}\r\n\r\n\t\t\t\treturn (target[prop as Exclude<keyof FastBooleanArray, 'length'>] = value);\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n\r\n\t/**\r\n\t * Applies a callback function to each element in the array.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t */\r\n\tforEach(callback: (value: boolean, index: number, array: FastBooleanArray) => void) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tcallback(this.get(i), i, this);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Creates a new array with the results of calling a provided function on every element.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t */\r\n\tmap(callback: (value: boolean, index: number, array: FastBooleanArray) => never) {\r\n\t\tconst result = new Array(this.size);\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tresult[i] = callback(this.get(i), i, this);\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Creates a new array with all elements that pass the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tfilter(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tconst result: boolean[] = [];\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tconst value = this.get(i);\r\n\t\t\tif (callback(value, i, this)) {\r\n\t\t\t\tresult.push(value);\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Tests whether at least one element in the array passes the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tsome(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tif (callback(this.get(i), i, this)) {\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn false;\r\n\t}\r\n\r\n\t/**\r\n\t * Tests whether all elements in the array pass the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tevery(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tif (!callback(this.get(i), i, this)) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\r\n\t/**\r\n\t * Reduces the array to a single value by applying a function to each element.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t * @param {any} initialValue - The initial value for the accumulator.\r\n\t */\r\n\treduce<T>(\r\n\t\tcallback: (accumulator: T, value: boolean, index: number, array: FastBooleanArray) => T,\r\n\t\tinitialValue: T\r\n\t): T {\r\n\t\tlet accumulator = initialValue;\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\taccumulator = callback(accumulator, this.get(i), i, this);\r\n\t\t}\r\n\t\treturn accumulator;\r\n\t}\r\n\r\n\t/**\r\n\t * Generates a string where every boolean is either a 0 or 1.\r\n\t */\r\n\ttoString() {\r\n\t\tlet result = ``;\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tresult += this.get(i) ? '1' : '0';\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from .toString() output\r\n\t */\r\n\tstatic fromString(value: string) {\r\n\t\tconst array = new FastBooleanArray(value.length);\r\n\t\tfor (let i = 0; i < value.length; i++) {\r\n\t\t\tarray.set(i, value[i] == '1');\r\n\t\t}\r\n\t\treturn array;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from a boolean array or number array\r\n\t */\r\n\tstatic fromArray(value: boolean[] | number[]) {\r\n\t\tconst array = new FastBooleanArray(value.length);\r\n\t\tfor (let i = 0; i < value.length; i++) {\r\n\t\t\tarray.set(i, !!value[i]);\r\n\t\t}\r\n\t\treturn array;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from a string, boolean array or number array\r\n\t */\r\n\tstatic from(value: string | boolean[] | number[]) {\r\n\t\tif (typeof value === 'string') {\r\n\t\t\treturn FastBooleanArray.fromString(value);\r\n\t\t}\r\n\r\n\t\treturn FastBooleanArray.fromArray(value);\r\n\t}\r\n}\r\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAqBE,EAArB,MAAqBE,CAAiB,CAC9B,KACC,OAER,YAAYC,EAAc,CACzB,GAAI,CAACA,EACJ,MAAM,IAAI,MAAM,kDAAkD,EAEnE,KAAK,KAAOA,EACZ,KAAK,OAAS,IAAI,WAAW,KAAK,KAAKA,EAAO,CAAC,CAAC,CACjD,CAQA,IAAIC,EAAeC,EAAgB,CAClC,OAAIA,GACH,KAAK,OAAOD,GAAS,CAAC,GAAK,IAAMA,EAAQ,GAClC,KAEP,KAAK,OAAOA,GAAS,CAAC,GAAK,EAAE,IAAMA,EAAQ,IACpC,GAET,CASA,QAAQA,EAAeC,EAAgB,CACtC,GAAID,EAAQ,GAAKA,GAAS,KAAK,K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ACI,CACJ,IAAIC,EAAcD,EAClB,QAASN,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC9BO,EAAcH,EAASG,EAAa,KAAK,IAAIP,CAAC,EAAGA,EAAG,IAAI,EAEzD,OAAOO,CACR,CAKA,UAAW,CACV,IAAIF,EAAS,GACb,QAASL,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC9BK,GAAU,KAAK,IAAIL,CAAC,EAAI,IAAM,IAE/B,OAAOK,CACR,CAKA,OAAO,WAAWZ,EAAe,CAChC,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IACjCQ,EAAM,IAAIR,EAAGP,EAAMO,CAAC,GAAK,GAAG,EAE7B,OAAOQ,CACR,CAKA,OAAO,UAAUf,EAA6B,CAC7C,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IACjCQ,EAAM,IAAIR,EAAG,CAAC,CAACP,EAAMO,CAAC,CAAC,EAExB,OAAOQ,CACR,CAKA,OAAO,KAAKf,EAAsC,CACjD,OAAI,OAAOA,GAAU,SACbH,EAAiB,WAAWG,CAAK,EAGlCH,EAAiB,UAAUG,CAAK,CACxC,CACD","names":["src_exports","__export","FastBooleanArray","__toCommonJS","_FastBooleanArray","size","index","value","byteIndex","bitIndex","newSize","newBuffer","other","byte","i","arr","target","prop","callback","result","initialValue","accumulator","array"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["const ERROR_OUT_OF_BOUNDS = \"Index out of bounds\";\r\n\r\nexport default class FastBooleanArray {\r\n public size: number;\r\n private buffer: Uint8Array;\r\n\r\n constructor(size: number) {\r\n if (!size) {\r\n throw new Error(\"FastBooleanArray must have a size greater than 0\");\r\n }\r\n this.size = size;\r\n this.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n }\r\n\r\n /**\r\n * Sets a boolean value at the specified index.\r\n * @param {number} index - The index to set the boolean value at.\r\n * @param {number} value - The boolean value to set the `index`.\r\n * @returns {boolean} The boolean value that was set.\r\n */\r\n set(index: number, value: boolean) {\r\n if (value) {\r\n this.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n return true;\r\n } else {\r\n this.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n * @param {number} index - The index to set the boolean value at.\r\n * @param {number} value - The boolean value to set the `index`.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n setSafe(index: number, value: boolean) {\r\n if (index < 0 || index >= this.size) {\r\n throw new RangeError(ERROR_OUT_OF_BOUNDS);\r\n }\r\n return this.set(index, value);\r\n }\r\n\r\n /**\r\n * Sets all bits to the specified boolean value.\r\n * @param {boolean} value - The boolean value to set all bits to.\r\n */\r\n setAll(value: boolean) {\r\n this.buffer.fill(value ? 0xff : 0x00);\r\n }\r\n\r\n /**\r\n * Gets a boolean value at the specified index.\r\n * @param {number} index - The index to get the boolean value of.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n get(index: number) {\r\n const byteIndex = index >>> 3; // index divided by 8 (shifted)\r\n const bitIndex = index & 7; // index modulo 8 (masking)\r\n return (this.buffer[byteIndex] & (1 << bitIndex)) !== 0; // Direct comparison is faster than double negation !!\r\n }\r\n\r\n /**\r\n * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n * @param {number} index - The index to get the boolean value of.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n getSafe(index: number) {\r\n if (index < 0 || index >= this.size) {\r\n throw new RangeError(ERROR_OUT_OF_BOUNDS);\r\n }\r\n return this.get(index);\r\n }\r\n\r\n /**\r\n * Resizes the array to a new size, preserving existing data.\r\n * @param {number} newSize - The new size of the array.\r\n */\r\n resize(newSize: number) {\r\n const newBuffer = new Uint8Array(Math.ceil(newSize / 8));\r\n newBuffer.set(\r\n this.buffer.subarray(0, Math.min(this.buffer.length, newBuffer.length))\r\n );\r\n this.size = newSize;\r\n this.buffer = newBuffer;\r\n }\r\n\r\n get length() {\r\n return this.size;\r\n }\r\n\r\n /**\r\n * Compares this FastBooleanArray with another for equality.\r\n * @param {FastBooleanArray} other - The other FastBooleanArray to compare.\r\n * @returns {boolean} True if both arrays are equal, false otherwise.\r\n */\r\n equals(other: FastBooleanArray) {\r\n if (this.size !== other.size) {\r\n return false;\r\n }\r\n return this.buffer.every((byte, i) => byte === other.buffer[i]);\r\n }\r\n\r\n /**\r\n * Makes the array iterable using `for...of`.\r\n */\r\n *[Symbol.iterator]() {\r\n for (let i = 0; i < this.size; i++) {\r\n yield this.get(i);\r\n }\r\n }\r\n\r\n /**\r\n * Converts to a standard JavaScript array.\r\n */\r\n toArray() {\r\n const arr = new Array(this.size);\r\n for (let i = 0; i < this.size; i++) {\r\n arr[i] = this.get(i);\r\n }\r\n return arr;\r\n }\r\n\r\n /**\r\n * Returns a proxy to the FastBooleanArray instance\r\n * With this you can access indexes like on an actual array.\r\n */\r\n accessLikeArray() {\r\n return new Proxy(this, {\r\n get: (target, prop) => {\r\n if (typeof prop === \"string\" && !isNaN(Number(prop))) {\r\n const index = Number(prop);\r\n return target.get(index);\r\n }\r\n return target[prop as keyof FastBooleanArray];\r\n },\r\n set: (target, prop, value) => {\r\n if (typeof prop === \"string\" && !isNaN(Number(prop))) {\r\n const index = Number(prop);\r\n target.set(index, value as never);\r\n } else {\r\n target[prop as Exclude<keyof FastBooleanArray, \"length\">] = value;\r\n }\r\n\r\n return true;\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Applies a callback function to each element in the array.\r\n * @param {Function} callback - The function to execute on each element.\r\n */\r\n forEach(\r\n callback: (value: boolean, index: number, array: FastBooleanArray) => void\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n callback(this.get(i), i, this);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a new array with the results of calling a provided function on every element.\r\n * @param {Function} callback - The function to execute on each element.\r\n */\r\n map(\r\n callback: (value: boolean, index: number, array: FastBooleanArray) => never\r\n ) {\r\n const result = new Array(this.size);\r\n for (let i = 0; i < this.size; i++) {\r\n result[i] = callback(this.get(i), i, this);\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Creates a new array with all elements that pass the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n filter(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n const result: boolean[] = [];\r\n for (let i = 0; i < this.size; i++) {\r\n const value = this.get(i);\r\n if (callback(value, i, this)) {\r\n result.push(value);\r\n }\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Tests whether at least one element in the array passes the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n some(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n if (callback(this.get(i), i, this)) {\r\n return true;\r\n }\r\n }\r\n return false;\r\n }\r\n\r\n /**\r\n * Tests whether all elements in the array pass the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n every(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n if (!callback(this.get(i), i, this)) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n\r\n /**\r\n * Reduces the array to a single value by applying a function to each element.\r\n * @param {Function} callback - The function to execute on each element.\r\n * @param {any} initialValue - The initial value for the accumulator.\r\n */\r\n reduce<T>(\r\n callback: (\r\n accumulator: T,\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => T,\r\n initialValue: T\r\n ): T {\r\n let accumulator = initialValue;\r\n for (let i = 0; i < this.size; i++) {\r\n accumulator = callback(accumulator, this.get(i), i, this);\r\n }\r\n return accumulator;\r\n }\r\n\r\n /**\r\n * Generates a string where every boolean is either a 0 or 1.\r\n */\r\n toString() {\r\n let result = ``;\r\n for (let i = 0; i < this.size; i++) {\r\n result += this.get(i) ? \"1\" : \"0\";\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from .toString() output\r\n */\r\n static fromString(value: string) {\r\n const array = new FastBooleanArray(value.length);\r\n for (let i = 0; i < value.length; i++) {\r\n array.set(i, value[i] == \"1\");\r\n }\r\n return array;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from a boolean array or number array\r\n */\r\n static fromArray(value: boolean[] | number[]) {\r\n const array = new FastBooleanArray(value.length);\r\n for (let i = 0; i < value.length; i++) {\r\n array.set(i, !!value[i]);\r\n }\r\n return array;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from a string, boolean array or number array\r\n */\r\n static from(value: string | boolean[] | number[]) {\r\n if (typeof value === \"string\") {\r\n return FastBooleanArray.fromString(value);\r\n }\r\n\r\n return FastBooleanArray.fromArray(value);\r\n }\r\n}\r\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAMI,EAAsB,sBAEPF,EAArB,MAAqBG,CAAiB,CAC7B,KACC,OAER,YAAYC,EAAc,CACxB,GAAI,CAACA,EACH,MAAM,IAAI,MAAM,kDAAkD,EAEpE,KAAK,KAAOA,EACZ,KAAK,OAAS,IAAI,WAAW,KAAK,KAAKA,EAAO,CAAC,CAAC,CAClD,CAQA,IAAIC,EAAeC,EAAgB,CACjC,OAAIA,GACF,KAAK,OAAOD,GAAS,CAAC,GAAK,IAAMA,EAAQ,GAClC,KAE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AAI,CAACI,EAAS,KAAK,IAAIJ,CAAC,EAAGA,EAAG,IAAI,EAChC,MAAO,GAGX,MAAO,EACT,CAOA,OACEI,EAMAE,EACG,CACH,IAAIC,EAAcD,EAClB,QAASN,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC7BO,EAAcH,EAASG,EAAa,KAAK,IAAIP,CAAC,EAAGA,EAAG,IAAI,EAE1D,OAAOO,CACT,CAKA,UAAW,CACT,IAAIF,EAAS,GACb,QAASL,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC7BK,GAAU,KAAK,IAAIL,CAAC,EAAI,IAAM,IAEhC,OAAOK,CACT,CAKA,OAAO,WAAWZ,EAAe,CAC/B,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IAChCQ,EAAM,IAAIR,EAAGP,EAAMO,CAAC,GAAK,GAAG,EAE9B,OAAOQ,CACT,CAKA,OAAO,UAAUf,EAA6B,CAC5C,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IAChCQ,EAAM,IAAIR,EAAG,CAAC,CAACP,EAAMO,CAAC,CAAC,EAEzB,OAAOQ,CACT,CAKA,OAAO,KAAKf,EAAsC,CAChD,OAAI,OAAOA,GAAU,SACZH,EAAiB,WAAWG,CAAK,EAGnCH,EAAiB,UAAUG,CAAK,CACzC,CACF","names":["index_exports","__export","FastBooleanArray","__toCommonJS","ERROR_OUT_OF_BOUNDS","_FastBooleanArray","size","index","value","byteIndex","bitIndex","newSize","newBuffer","other","byte","i","arr","target","prop","callback","result","initialValue","accumulator","array"]}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var i=class n{size;buffer;constructor(e){if(!e)throw new Error("FastBooleanArray must have a size greater than 0");this.size=e,this.buffer=new Uint8Array(Math.ceil(e/8))}set(e,r){return r?(this.buffer[e>>3]|=1<<(e&7),!0):(this.buffer[e>>3]&=~(1<<(e&7)),!1)}setSafe(e,r){if(e<0||e>=this.size)throw new RangeError(
|
|
1
|
+
var a="Index out of bounds",i=class n{size;buffer;constructor(e){if(!e)throw new Error("FastBooleanArray must have a size greater than 0");this.size=e,this.buffer=new Uint8Array(Math.ceil(e/8))}set(e,r){return r?(this.buffer[e>>3]|=1<<(e&7),!0):(this.buffer[e>>3]&=~(1<<(e&7)),!1)}setSafe(e,r){if(e<0||e>=this.size)throw new RangeError(a);return this.set(e,r)}setAll(e){this.buffer.fill(e?255:0)}get(e){let r=e>>>3,t=e&7;return(this.buffer[r]&1<<t)!==0}getSafe(e){if(e<0||e>=this.size)throw new RangeError(a);return this.get(e)}resize(e){let r=new Uint8Array(Math.ceil(e/8));r.set(this.buffer.subarray(0,Math.min(this.buffer.length,r.length))),this.size=e,this.buffer=r}get length(){return this.size}equals(e){return this.size!==e.size?!1:this.buffer.every((r,t)=>r===e.buffer[t])}*[Symbol.iterator](){for(let e=0;e<this.size;e++)yield this.get(e)}toArray(){let e=new Array(this.size);for(let r=0;r<this.size;r++)e[r]=this.get(r);return e}accessLikeArray(){return new Proxy(this,{get:(e,r)=>{if(typeof r=="string"&&!isNaN(Number(r))){let t=Number(r);return e.get(t)}return e[r]},set:(e,r,t)=>{if(typeof r=="string"&&!isNaN(Number(r))){let s=Number(r);e.set(s,t)}else e[r]=t;return!0}})}forEach(e){for(let r=0;r<this.size;r++)e(this.get(r),r,this)}map(e){let r=new Array(this.size);for(let t=0;t<this.size;t++)r[t]=e(this.get(t),t,this);return r}filter(e){let r=[];for(let t=0;t<this.size;t++){let s=this.get(t);e(s,t,this)&&r.push(s)}return r}some(e){for(let r=0;r<this.size;r++)if(e(this.get(r),r,this))return!0;return!1}every(e){for(let r=0;r<this.size;r++)if(!e(this.get(r),r,this))return!1;return!0}reduce(e,r){let t=r;for(let s=0;s<this.size;s++)t=e(t,this.get(s),s,this);return t}toString(){let e="";for(let r=0;r<this.size;r++)e+=this.get(r)?"1":"0";return e}static fromString(e){let r=new n(e.length);for(let t=0;t<e.length;t++)r.set(t,e[t]=="1");return r}static fromArray(e){let r=new n(e.length);for(let t=0;t<e.length;t++)r.set(t,!!e[t]);return r}static from(e){return typeof e=="string"?n.fromString(e):n.fromArray(e)}};export{i as default};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tif (!size) {\r\n\t\t\tthrow new Error('FastBooleanArray must have a size greater than 0');\r\n\t\t}\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\t/**\r\n\t * Sets a boolean value at the specified index.\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t */\r\n\tset(index: number, value: boolean) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n\t\t\treturn true;\r\n\t\t} else {\r\n\t\t\tthis.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tsetSafe(index: number, value: boolean) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.set(index, value);\r\n\t}\r\n\r\n\t/**\r\n\t * Sets all bits to the specified boolean value.\r\n\t * @param {boolean} value - The boolean value to set all bits to.\r\n\t */\r\n\tsetAll(value: boolean) {\r\n\t\tthis.buffer.fill(value ? 0xff : 0x00);\r\n\t}\r\n\r\n\t/**\r\n\t * Gets a boolean value at the specified index.\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tget(index: number) {\r\n\t\tconst byteIndex = index >>> 3; // index divided by 8 (shifted)\r\n\t\tconst bitIndex = index & 7; // index modulo 8 (masking)\r\n\t\treturn (this.buffer[byteIndex] & (1 << bitIndex)) !== 0; // Direct comparison is faster than double negation !!\r\n\t}\r\n\r\n\t/**\r\n\t * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tgetSafe(index: number) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.get(index);\r\n\t}\r\n\r\n\t/**\r\n\t * Resizes the array to a new size, preserving existing data.\r\n\t * @param {number} newSize - The new size of the array.\r\n\t */\r\n\tresize(newSize: number) {\r\n\t\tconst newBuffer = new Uint8Array(Math.ceil(newSize / 8));\r\n\t\tnewBuffer.set(this.buffer.subarray(0, Math.min(this.buffer.length, newBuffer.length)));\r\n\t\tthis.buffer = newBuffer;\r\n\t\tthis.size = newSize;\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\t/**\r\n\t * Compares this FastBooleanArray with another for equality.\r\n\t * @param {FastBooleanArray} other - The other FastBooleanArray to compare.\r\n\t * @returns {boolean} True if both arrays are equal, false otherwise.\r\n\t */\r\n\tequals(other: FastBooleanArray) {\r\n\t\tif (this.size !== other.size) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\treturn this.buffer.every((byte, i) => byte === other.buffer[i]);\r\n\t}\r\n\r\n\t/**\r\n\t * Makes the array iterable using `for...of`.\r\n\t */\r\n\t*[Symbol.iterator]() {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tyield this.get(i);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Converts to a standard JavaScript array.\r\n\t */\r\n\ttoArray() {\r\n\t\tconst arr = new Array(this.size);\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tarr[i] = this.get(i);\r\n\t\t}\r\n\t\treturn arr;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a proxy to the FastBooleanArray instance\r\n\t * With this you can access indexes like on an actual array.\r\n\t */\r\n\taccessLikeArray() {\r\n\t\treturn new Proxy(this, {\r\n\t\t\tget: (target, prop) => {\r\n\t\t\t\tif (typeof prop === 'string' && !isNaN(Number(prop))) {\r\n\t\t\t\t\tconst index = Number(prop);\r\n\t\t\t\t\treturn target.get(index);\r\n\t\t\t\t}\r\n\t\t\t\treturn target[prop as keyof FastBooleanArray];\r\n\t\t\t},\r\n\t\t\tset: (target, prop, value) => {\r\n\t\t\t\tif (typeof prop === 'string' && !isNaN(Number(prop))) {\r\n\t\t\t\t\tconst index = Number(prop);\r\n\t\t\t\t\treturn target.set(index, value as never);\r\n\t\t\t\t}\r\n\r\n\t\t\t\treturn (target[prop as Exclude<keyof FastBooleanArray, 'length'>] = value);\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n\r\n\t/**\r\n\t * Applies a callback function to each element in the array.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t */\r\n\tforEach(callback: (value: boolean, index: number, array: FastBooleanArray) => void) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tcallback(this.get(i), i, this);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Creates a new array with the results of calling a provided function on every element.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t */\r\n\tmap(callback: (value: boolean, index: number, array: FastBooleanArray) => never) {\r\n\t\tconst result = new Array(this.size);\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tresult[i] = callback(this.get(i), i, this);\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Creates a new array with all elements that pass the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tfilter(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tconst result: boolean[] = [];\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tconst value = this.get(i);\r\n\t\t\tif (callback(value, i, this)) {\r\n\t\t\t\tresult.push(value);\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Tests whether at least one element in the array passes the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tsome(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tif (callback(this.get(i), i, this)) {\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn false;\r\n\t}\r\n\r\n\t/**\r\n\t * Tests whether all elements in the array pass the test implemented by the provided function.\r\n\t * @param {Function} callback - The function to test each element.\r\n\t */\r\n\tevery(callback: (value: boolean, index: number, array: FastBooleanArray) => boolean) {\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tif (!callback(this.get(i), i, this)) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\r\n\t/**\r\n\t * Reduces the array to a single value by applying a function to each element.\r\n\t * @param {Function} callback - The function to execute on each element.\r\n\t * @param {any} initialValue - The initial value for the accumulator.\r\n\t */\r\n\treduce<T>(\r\n\t\tcallback: (accumulator: T, value: boolean, index: number, array: FastBooleanArray) => T,\r\n\t\tinitialValue: T\r\n\t): T {\r\n\t\tlet accumulator = initialValue;\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\taccumulator = callback(accumulator, this.get(i), i, this);\r\n\t\t}\r\n\t\treturn accumulator;\r\n\t}\r\n\r\n\t/**\r\n\t * Generates a string where every boolean is either a 0 or 1.\r\n\t */\r\n\ttoString() {\r\n\t\tlet result = ``;\r\n\t\tfor (let i = 0; i < this.size; i++) {\r\n\t\t\tresult += this.get(i) ? '1' : '0';\r\n\t\t}\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from .toString() output\r\n\t */\r\n\tstatic fromString(value: string) {\r\n\t\tconst array = new FastBooleanArray(value.length);\r\n\t\tfor (let i = 0; i < value.length; i++) {\r\n\t\t\tarray.set(i, value[i] == '1');\r\n\t\t}\r\n\t\treturn array;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from a boolean array or number array\r\n\t */\r\n\tstatic fromArray(value: boolean[] | number[]) {\r\n\t\tconst array = new FastBooleanArray(value.length);\r\n\t\tfor (let i = 0; i < value.length; i++) {\r\n\t\t\tarray.set(i, !!value[i]);\r\n\t\t}\r\n\t\treturn array;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a FastBooleanArray from a string, boolean array or number array\r\n\t */\r\n\tstatic from(value: string | boolean[] | number[]) {\r\n\t\tif (typeof value === 'string') {\r\n\t\t\treturn FastBooleanArray.fromString(value);\r\n\t\t}\r\n\r\n\t\treturn FastBooleanArray.fromArray(value);\r\n\t}\r\n}\r\n"],"mappings":"AAAA,IAAqBA,EAArB,MAAqBC,CAAiB,CAC9B,KACC,OAER,YAAYC,EAAc,CACzB,GAAI,CAACA,EACJ,MAAM,IAAI,MAAM,kDAAkD,EAEnE,KAAK,KAAOA,EACZ,KAAK,OAAS,IAAI,WAAW,KAAK,KAAKA,EAAO,CAAC,CAAC,CAC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,CAAC,EAAGA,EAAG,IAAI,EAChC,MAAO,GAGT,MAAO,EACR,CAMA,MAAMI,EAA+E,CACpF,QAASJ,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC9B,GAAI,CAACI,EAAS,KAAK,IAAIJ,CAAC,EAAGA,EAAG,IAAI,EACjC,MAAO,GAGT,MAAO,EACR,CAOA,OACCI,EACAE,EACI,CACJ,IAAIC,EAAcD,EAClB,QAASN,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC9BO,EAAcH,EAASG,EAAa,KAAK,IAAIP,CAAC,EAAGA,EAAG,IAAI,EAEzD,OAAOO,CACR,CAKA,UAAW,CACV,IAAIF,EAAS,GACb,QAASL,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC9BK,GAAU,KAAK,IAAIL,CAAC,EAAI,IAAM,IAE/B,OAAOK,CACR,CAKA,OAAO,WAAWZ,EAAe,CAChC,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IACjCQ,EAAM,IAAIR,EAAGP,EAAMO,CAAC,GAAK,GAAG,EAE7B,OAAOQ,CACR,CAKA,OAAO,UAAUf,EAA6B,CAC7C,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IACjCQ,EAAM,IAAIR,EAAG,CAAC,CAACP,EAAMO,CAAC,CAAC,EAExB,OAAOQ,CACR,CAKA,OAAO,KAAKf,EAAsC,CACjD,OAAI,OAAOA,GAAU,SACbH,EAAiB,WAAWG,CAAK,EAGlCH,EAAiB,UAAUG,CAAK,CACxC,CACD","names":["FastBooleanArray","_FastBooleanArray","size","index","value","byteIndex","bitIndex","newSize","newBuffer","other","byte","i","arr","target","prop","callback","result","initialValue","accumulator","array"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["const ERROR_OUT_OF_BOUNDS = \"Index out of bounds\";\r\n\r\nexport default class FastBooleanArray {\r\n public size: number;\r\n private buffer: Uint8Array;\r\n\r\n constructor(size: number) {\r\n if (!size) {\r\n throw new Error(\"FastBooleanArray must have a size greater than 0\");\r\n }\r\n this.size = size;\r\n this.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n }\r\n\r\n /**\r\n * Sets a boolean value at the specified index.\r\n * @param {number} index - The index to set the boolean value at.\r\n * @param {number} value - The boolean value to set the `index`.\r\n * @returns {boolean} The boolean value that was set.\r\n */\r\n set(index: number, value: boolean) {\r\n if (value) {\r\n this.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n return true;\r\n } else {\r\n this.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n * @param {number} index - The index to set the boolean value at.\r\n * @param {number} value - The boolean value to set the `index`.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n setSafe(index: number, value: boolean) {\r\n if (index < 0 || index >= this.size) {\r\n throw new RangeError(ERROR_OUT_OF_BOUNDS);\r\n }\r\n return this.set(index, value);\r\n }\r\n\r\n /**\r\n * Sets all bits to the specified boolean value.\r\n * @param {boolean} value - The boolean value to set all bits to.\r\n */\r\n setAll(value: boolean) {\r\n this.buffer.fill(value ? 0xff : 0x00);\r\n }\r\n\r\n /**\r\n * Gets a boolean value at the specified index.\r\n * @param {number} index - The index to get the boolean value of.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n get(index: number) {\r\n const byteIndex = index >>> 3; // index divided by 8 (shifted)\r\n const bitIndex = index & 7; // index modulo 8 (masking)\r\n return (this.buffer[byteIndex] & (1 << bitIndex)) !== 0; // Direct comparison is faster than double negation !!\r\n }\r\n\r\n /**\r\n * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n * @param {number} index - The index to get the boolean value of.\r\n * @returns {boolean} The boolean value that was set.\r\n * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n */\r\n getSafe(index: number) {\r\n if (index < 0 || index >= this.size) {\r\n throw new RangeError(ERROR_OUT_OF_BOUNDS);\r\n }\r\n return this.get(index);\r\n }\r\n\r\n /**\r\n * Resizes the array to a new size, preserving existing data.\r\n * @param {number} newSize - The new size of the array.\r\n */\r\n resize(newSize: number) {\r\n const newBuffer = new Uint8Array(Math.ceil(newSize / 8));\r\n newBuffer.set(\r\n this.buffer.subarray(0, Math.min(this.buffer.length, newBuffer.length))\r\n );\r\n this.size = newSize;\r\n this.buffer = newBuffer;\r\n }\r\n\r\n get length() {\r\n return this.size;\r\n }\r\n\r\n /**\r\n * Compares this FastBooleanArray with another for equality.\r\n * @param {FastBooleanArray} other - The other FastBooleanArray to compare.\r\n * @returns {boolean} True if both arrays are equal, false otherwise.\r\n */\r\n equals(other: FastBooleanArray) {\r\n if (this.size !== other.size) {\r\n return false;\r\n }\r\n return this.buffer.every((byte, i) => byte === other.buffer[i]);\r\n }\r\n\r\n /**\r\n * Makes the array iterable using `for...of`.\r\n */\r\n *[Symbol.iterator]() {\r\n for (let i = 0; i < this.size; i++) {\r\n yield this.get(i);\r\n }\r\n }\r\n\r\n /**\r\n * Converts to a standard JavaScript array.\r\n */\r\n toArray() {\r\n const arr = new Array(this.size);\r\n for (let i = 0; i < this.size; i++) {\r\n arr[i] = this.get(i);\r\n }\r\n return arr;\r\n }\r\n\r\n /**\r\n * Returns a proxy to the FastBooleanArray instance\r\n * With this you can access indexes like on an actual array.\r\n */\r\n accessLikeArray() {\r\n return new Proxy(this, {\r\n get: (target, prop) => {\r\n if (typeof prop === \"string\" && !isNaN(Number(prop))) {\r\n const index = Number(prop);\r\n return target.get(index);\r\n }\r\n return target[prop as keyof FastBooleanArray];\r\n },\r\n set: (target, prop, value) => {\r\n if (typeof prop === \"string\" && !isNaN(Number(prop))) {\r\n const index = Number(prop);\r\n target.set(index, value as never);\r\n } else {\r\n target[prop as Exclude<keyof FastBooleanArray, \"length\">] = value;\r\n }\r\n\r\n return true;\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * Applies a callback function to each element in the array.\r\n * @param {Function} callback - The function to execute on each element.\r\n */\r\n forEach(\r\n callback: (value: boolean, index: number, array: FastBooleanArray) => void\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n callback(this.get(i), i, this);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a new array with the results of calling a provided function on every element.\r\n * @param {Function} callback - The function to execute on each element.\r\n */\r\n map(\r\n callback: (value: boolean, index: number, array: FastBooleanArray) => never\r\n ) {\r\n const result = new Array(this.size);\r\n for (let i = 0; i < this.size; i++) {\r\n result[i] = callback(this.get(i), i, this);\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Creates a new array with all elements that pass the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n filter(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n const result: boolean[] = [];\r\n for (let i = 0; i < this.size; i++) {\r\n const value = this.get(i);\r\n if (callback(value, i, this)) {\r\n result.push(value);\r\n }\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Tests whether at least one element in the array passes the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n some(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n if (callback(this.get(i), i, this)) {\r\n return true;\r\n }\r\n }\r\n return false;\r\n }\r\n\r\n /**\r\n * Tests whether all elements in the array pass the test implemented by the provided function.\r\n * @param {Function} callback - The function to test each element.\r\n */\r\n every(\r\n callback: (\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => boolean\r\n ) {\r\n for (let i = 0; i < this.size; i++) {\r\n if (!callback(this.get(i), i, this)) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n\r\n /**\r\n * Reduces the array to a single value by applying a function to each element.\r\n * @param {Function} callback - The function to execute on each element.\r\n * @param {any} initialValue - The initial value for the accumulator.\r\n */\r\n reduce<T>(\r\n callback: (\r\n accumulator: T,\r\n value: boolean,\r\n index: number,\r\n array: FastBooleanArray\r\n ) => T,\r\n initialValue: T\r\n ): T {\r\n let accumulator = initialValue;\r\n for (let i = 0; i < this.size; i++) {\r\n accumulator = callback(accumulator, this.get(i), i, this);\r\n }\r\n return accumulator;\r\n }\r\n\r\n /**\r\n * Generates a string where every boolean is either a 0 or 1.\r\n */\r\n toString() {\r\n let result = ``;\r\n for (let i = 0; i < this.size; i++) {\r\n result += this.get(i) ? \"1\" : \"0\";\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from .toString() output\r\n */\r\n static fromString(value: string) {\r\n const array = new FastBooleanArray(value.length);\r\n for (let i = 0; i < value.length; i++) {\r\n array.set(i, value[i] == \"1\");\r\n }\r\n return array;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from a boolean array or number array\r\n */\r\n static fromArray(value: boolean[] | number[]) {\r\n const array = new FastBooleanArray(value.length);\r\n for (let i = 0; i < value.length; i++) {\r\n array.set(i, !!value[i]);\r\n }\r\n return array;\r\n }\r\n\r\n /**\r\n * Returns a FastBooleanArray from a string, boolean array or number array\r\n */\r\n static from(value: string | boolean[] | number[]) {\r\n if (typeof value === \"string\") {\r\n return FastBooleanArray.fromString(value);\r\n }\r\n\r\n return FastBooleanArray.fromArray(value);\r\n }\r\n}\r\n"],"mappings":"AAAA,IAAMA,EAAsB,sBAEPC,EAArB,MAAqBC,CAAiB,CAC7B,KACC,OAER,YAAYC,EAAc,CACxB,GAAI,CAACA,EACH,MAAM,IAAI,MAAM,kDAAkD,EAEpE,KAAK,KAAOA,EACZ,KAAK,OAAS,IAA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EAAS,KAAK,IAAIJ,CAAC,EAAGA,EAAG,IAAI,EAC/B,MAAO,GAGX,MAAO,EACT,CAMA,MACEI,EAKA,CACA,QAASJ,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC7B,GAAI,CAACI,EAAS,KAAK,IAAIJ,CAAC,EAAGA,EAAG,IAAI,EAChC,MAAO,GAGX,MAAO,EACT,CAOA,OACEI,EAMAE,EACG,CACH,IAAIC,EAAcD,EAClB,QAASN,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC7BO,EAAcH,EAASG,EAAa,KAAK,IAAIP,CAAC,EAAGA,EAAG,IAAI,EAE1D,OAAOO,CACT,CAKA,UAAW,CACT,IAAIF,EAAS,GACb,QAASL,EAAI,EAAGA,EAAI,KAAK,KAAMA,IAC7BK,GAAU,KAAK,IAAIL,CAAC,EAAI,IAAM,IAEhC,OAAOK,CACT,CAKA,OAAO,WAAWZ,EAAe,CAC/B,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IAChCQ,EAAM,IAAIR,EAAGP,EAAMO,CAAC,GAAK,GAAG,EAE9B,OAAOQ,CACT,CAKA,OAAO,UAAUf,EAA6B,CAC5C,IAAMe,EAAQ,IAAIlB,EAAiBG,EAAM,MAAM,EAC/C,QAASO,EAAI,EAAGA,EAAIP,EAAM,OAAQO,IAChCQ,EAAM,IAAIR,EAAG,CAAC,CAACP,EAAMO,CAAC,CAAC,EAEzB,OAAOQ,CACT,CAKA,OAAO,KAAKf,EAAsC,CAChD,OAAI,OAAOA,GAAU,SACZH,EAAiB,WAAWG,CAAK,EAGnCH,EAAiB,UAAUG,CAAK,CACzC,CACF","names":["ERROR_OUT_OF_BOUNDS","FastBooleanArray","_FastBooleanArray","size","index","value","byteIndex","bitIndex","newSize","newBuffer","other","byte","i","arr","target","prop","callback","result","initialValue","accumulator","array"]}
|
package/package.json
CHANGED
|
@@ -1,50 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "fast-boolean-array",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"homepage": "https://github.com/UltraCakeBakery/FastBooleanArray",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/UltraCakeBakery/FastBooleanArray.git"
|
|
8
|
-
},
|
|
9
|
-
"bugs": {
|
|
10
|
-
"url": "https://github.com/UltraCakeBakery/FastBooleanArray/issues",
|
|
11
|
-
"email": "fast-boolean-array@managing.software"
|
|
12
|
-
},
|
|
13
|
-
"type": "module",
|
|
14
|
-
"main": "./dist/index.cjs",
|
|
15
|
-
"module": "./dist/index.js",
|
|
16
|
-
"types": "./dist/index.d.ts",
|
|
17
|
-
"exports": {
|
|
18
|
-
"import": {
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
|
-
"import": "./dist/index.js"
|
|
21
|
-
},
|
|
22
|
-
"require": {
|
|
23
|
-
"types": "./dist/index.d.cts",
|
|
24
|
-
"require": "./dist/index.cjs"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"scripts": {
|
|
28
|
-
"check": "npm run check:types",
|
|
29
|
-
"check:
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "fast-boolean-array",
|
|
3
|
+
"version": "1.4.4",
|
|
4
|
+
"homepage": "https://github.com/UltraCakeBakery/FastBooleanArray",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/UltraCakeBakery/FastBooleanArray.git"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/UltraCakeBakery/FastBooleanArray/issues",
|
|
11
|
+
"email": "fast-boolean-array@managing.software"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/index.d.cts",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"check": "npm run check:types ; npm run check:test",
|
|
29
|
+
"check:test": "vitest",
|
|
30
|
+
"check:types": "npx --yes @arethetypeswrong/cli --pack .",
|
|
31
|
+
"build": "tsup src/index.ts --dts --format cjs,esm --clean --sourcemap --minify",
|
|
32
|
+
"publish": "npm publish"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"boolean",
|
|
36
|
+
"array",
|
|
37
|
+
"fast",
|
|
38
|
+
"efficient",
|
|
39
|
+
"map"
|
|
40
|
+
],
|
|
41
|
+
"author": "Jack van der Bil <jack@managing.software> (https://jackvanderbilt.nl)",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"description": "",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=0.10.3"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^22.10.2",
|
|
49
|
+
"tsup": "^8.3.5",
|
|
50
|
+
"typescript": "^5.7.2",
|
|
51
|
+
"vitest": "^2.1.8"
|
|
52
|
+
}
|
|
50
53
|
}
|