distillate 0.1.2 → 0.2.0

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.
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_serialize = require("../serialize-CHHDM4TQ.cjs");
3
+ const require_params = require("../params-J8p3bKq5.cjs");
3
4
  //#region src/blocked/blocked.ts
4
5
  const TYPE = 2;
5
6
  const SALT = Uint32Array.of(1203114875, 1150766481, 2284105051, 2729912477, 1884591559, 770785867, 2667333959, 1550580529);
@@ -45,6 +46,8 @@ var BlockedBloomFilter = class BlockedBloomFilter {
45
46
  * @returns A new, empty filter.
46
47
  */
47
48
  static create(n, epsilon) {
49
+ require_params.assertPositiveInt(n, "n");
50
+ require_params.assertProbability(epsilon, "epsilon");
48
51
  const t = Math.log10(1 / epsilon);
49
52
  const a = BlockedBloomFilter.#ANCHORS;
50
53
  let seg = a.findIndex((p) => t <= p[0]);
@@ -53,7 +56,7 @@ var BlockedBloomFilter = class BlockedBloomFilter {
53
56
  const [t1, b1] = a[seg] ?? [0, 0];
54
57
  const bitsPerKey = b0 + (b1 - b0) / (t1 - t0) * (t - t0);
55
58
  return new BlockedBloomFilter({
56
- bitsPerKey: Math.ceil(bitsPerKey),
59
+ bitsPerKey: Math.max(1, Math.ceil(bitsPerKey)),
57
60
  capacity: n
58
61
  });
59
62
  }
@@ -62,6 +65,9 @@ var BlockedBloomFilter = class BlockedBloomFilter {
62
65
  * {@link BlockedBloomFilter.create} unless restoring a specific configuration.
63
66
  */
64
67
  constructor({ bitsPerKey, capacity, seed = 0 }) {
68
+ require_params.assertPositiveFinite(bitsPerKey, "bitsPerKey");
69
+ require_params.assertPositiveInt(capacity, "capacity");
70
+ require_params.assertUint32(seed, "seed");
65
71
  this.#numBlocks = Math.max(1, Math.ceil(bitsPerKey * capacity / 256));
66
72
  this.#lanes = new Uint32Array(this.#numBlocks * 8);
67
73
  this.#seed = seed;
@@ -168,3 +174,4 @@ function fillBlock(key, numBlocks, seed, outWords, outBits) {
168
174
  //#endregion
169
175
  exports.BlockedBloomFilter = BlockedBloomFilter;
170
176
  exports.BlockedBloomParamMismatchError = BlockedBloomParamMismatchError;
177
+ exports.ParamError = require_params.ParamError;
@@ -1,4 +1,5 @@
1
1
  import { t as BytesLike } from "../bytes-DCuYtUVS.cjs";
2
+ import { t as ParamError } from "../params-DnqJBqLS.cjs";
2
3
  //#region src/blocked/blocked.d.ts
3
4
  /** Thrown when an operation requires two filters built with identical parameters. */
4
5
  declare class BlockedBloomParamMismatchError extends Error {
@@ -78,4 +79,4 @@ declare class BlockedBloomFilter {
78
79
  has(key: BytesLike): boolean;
79
80
  }
80
81
  //#endregion
81
- export { BlockedBloomFilter, BlockedBloomParamMismatchError, type BlockedBloomParams };
82
+ export { BlockedBloomFilter, BlockedBloomParamMismatchError, type BlockedBloomParams, ParamError };
@@ -1,4 +1,5 @@
1
1
  import { t as BytesLike } from "../bytes-DCuYtUVS.js";
2
+ import { t as ParamError } from "../params-DnqJBqLS.js";
2
3
  //#region src/blocked/blocked.d.ts
3
4
  /** Thrown when an operation requires two filters built with identical parameters. */
4
5
  declare class BlockedBloomParamMismatchError extends Error {
@@ -78,4 +79,4 @@ declare class BlockedBloomFilter {
78
79
  has(key: BytesLike): boolean;
79
80
  }
80
81
  //#endregion
81
- export { BlockedBloomFilter, BlockedBloomParamMismatchError, type BlockedBloomParams };
82
+ export { BlockedBloomFilter, BlockedBloomParamMismatchError, type BlockedBloomParams, ParamError };
@@ -1,4 +1,5 @@
1
1
  import { i as hash128KeyInto, n as readHeader, o as reduce, r as writeHeader } from "../serialize-5XQ5y_R-.js";
2
+ import { a as assertUint32, i as assertProbability, n as assertPositiveFinite, r as assertPositiveInt, t as ParamError } from "../params-ChTRNxM9.js";
2
3
  //#region src/blocked/blocked.ts
3
4
  const TYPE = 2;
4
5
  const SALT = Uint32Array.of(1203114875, 1150766481, 2284105051, 2729912477, 1884591559, 770785867, 2667333959, 1550580529);
@@ -44,6 +45,8 @@ var BlockedBloomFilter = class BlockedBloomFilter {
44
45
  * @returns A new, empty filter.
45
46
  */
46
47
  static create(n, epsilon) {
48
+ assertPositiveInt(n, "n");
49
+ assertProbability(epsilon, "epsilon");
47
50
  const t = Math.log10(1 / epsilon);
48
51
  const a = BlockedBloomFilter.#ANCHORS;
49
52
  let seg = a.findIndex((p) => t <= p[0]);
@@ -52,7 +55,7 @@ var BlockedBloomFilter = class BlockedBloomFilter {
52
55
  const [t1, b1] = a[seg] ?? [0, 0];
53
56
  const bitsPerKey = b0 + (b1 - b0) / (t1 - t0) * (t - t0);
54
57
  return new BlockedBloomFilter({
55
- bitsPerKey: Math.ceil(bitsPerKey),
58
+ bitsPerKey: Math.max(1, Math.ceil(bitsPerKey)),
56
59
  capacity: n
57
60
  });
58
61
  }
@@ -61,6 +64,9 @@ var BlockedBloomFilter = class BlockedBloomFilter {
61
64
  * {@link BlockedBloomFilter.create} unless restoring a specific configuration.
62
65
  */
63
66
  constructor({ bitsPerKey, capacity, seed = 0 }) {
67
+ assertPositiveFinite(bitsPerKey, "bitsPerKey");
68
+ assertPositiveInt(capacity, "capacity");
69
+ assertUint32(seed, "seed");
64
70
  this.#numBlocks = Math.max(1, Math.ceil(bitsPerKey * capacity / 256));
65
71
  this.#lanes = new Uint32Array(this.#numBlocks * 8);
66
72
  this.#seed = seed;
@@ -165,4 +171,4 @@ function fillBlock(key, numBlocks, seed, outWords, outBits) {
165
171
  }
166
172
  }
167
173
  //#endregion
168
- export { BlockedBloomFilter, BlockedBloomParamMismatchError };
174
+ export { BlockedBloomFilter, BlockedBloomParamMismatchError, ParamError };
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_serialize = require("../serialize-CHHDM4TQ.cjs");
3
+ const require_params = require("../params-J8p3bKq5.cjs");
3
4
  //#region src/core/bitset.ts
4
5
  var BitSetRangeError = class extends RangeError {
5
6
  name = "BitSetRangeError";
@@ -80,6 +81,8 @@ var BloomFilter = class BloomFilter {
80
81
  * @returns A new, empty filter.
81
82
  */
82
83
  static create(n, epsilon) {
84
+ require_params.assertPositiveInt(n, "n");
85
+ require_params.assertProbability(epsilon, "epsilon");
83
86
  const f = new BloomFilter(optimal(n, epsilon));
84
87
  f.#n = n;
85
88
  return f;
@@ -109,6 +112,9 @@ var BloomFilter = class BloomFilter {
109
112
  * {@link BloomFilter.create} unless restoring a specific configuration.
110
113
  */
111
114
  constructor({ m, k, seed = 0 }) {
115
+ require_params.assertPositiveInt(m, "m");
116
+ require_params.assertPositiveInt(k, "k");
117
+ require_params.assertUint32(seed, "seed");
112
118
  this.#bits = new BitSet(m);
113
119
  this.#m = m;
114
120
  this.#k = k;
@@ -184,3 +190,4 @@ var BloomFilter = class BloomFilter {
184
190
  //#endregion
185
191
  exports.BloomFilter = BloomFilter;
186
192
  exports.BloomParamMismatchError = BloomParamMismatchError;
193
+ exports.ParamError = require_params.ParamError;
@@ -1,4 +1,5 @@
1
1
  import { t as BytesLike } from "../bytes-DCuYtUVS.cjs";
2
+ import { t as ParamError } from "../params-DnqJBqLS.cjs";
2
3
  //#region src/bloom/bloom.d.ts
3
4
  /** Thrown when an operation requires two filters built with identical parameters. */
4
5
  declare class BloomParamMismatchError extends Error {
@@ -79,4 +80,4 @@ declare class BloomFilter {
79
80
  has(key: BytesLike): boolean;
80
81
  }
81
82
  //#endregion
82
- export { BloomFilter, BloomParamMismatchError, type BloomParams };
83
+ export { BloomFilter, BloomParamMismatchError, type BloomParams, ParamError };
@@ -1,4 +1,5 @@
1
1
  import { t as BytesLike } from "../bytes-DCuYtUVS.js";
2
+ import { t as ParamError } from "../params-DnqJBqLS.js";
2
3
  //#region src/bloom/bloom.d.ts
3
4
  /** Thrown when an operation requires two filters built with identical parameters. */
4
5
  declare class BloomParamMismatchError extends Error {
@@ -79,4 +80,4 @@ declare class BloomFilter {
79
80
  has(key: BytesLike): boolean;
80
81
  }
81
82
  //#endregion
82
- export { BloomFilter, BloomParamMismatchError, type BloomParams };
83
+ export { BloomFilter, BloomParamMismatchError, type BloomParams, ParamError };
@@ -1,4 +1,5 @@
1
1
  import { a as probeInto, n as readHeader, r as writeHeader } from "../serialize-5XQ5y_R-.js";
2
+ import { a as assertUint32, i as assertProbability, r as assertPositiveInt, t as ParamError } from "../params-ChTRNxM9.js";
2
3
  //#region src/core/bitset.ts
3
4
  var BitSetRangeError = class extends RangeError {
4
5
  name = "BitSetRangeError";
@@ -79,6 +80,8 @@ var BloomFilter = class BloomFilter {
79
80
  * @returns A new, empty filter.
80
81
  */
81
82
  static create(n, epsilon) {
83
+ assertPositiveInt(n, "n");
84
+ assertProbability(epsilon, "epsilon");
82
85
  const f = new BloomFilter(optimal(n, epsilon));
83
86
  f.#n = n;
84
87
  return f;
@@ -108,6 +111,9 @@ var BloomFilter = class BloomFilter {
108
111
  * {@link BloomFilter.create} unless restoring a specific configuration.
109
112
  */
110
113
  constructor({ m, k, seed = 0 }) {
114
+ assertPositiveInt(m, "m");
115
+ assertPositiveInt(k, "k");
116
+ assertUint32(seed, "seed");
111
117
  this.#bits = new BitSet(m);
112
118
  this.#m = m;
113
119
  this.#k = k;
@@ -181,4 +187,4 @@ var BloomFilter = class BloomFilter {
181
187
  }
182
188
  };
183
189
  //#endregion
184
- export { BloomFilter, BloomParamMismatchError };
190
+ export { BloomFilter, BloomParamMismatchError, ParamError };
package/dist/index.cjs CHANGED
@@ -2,6 +2,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#endregion
3
3
  //#region src/index.ts
4
4
  /** The installed `distillate` package version. */
5
- const VERSION = "0.1.2";
5
+ const VERSION = "0.2.0";
6
6
  //#endregion
7
7
  exports.VERSION = VERSION;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  //#endregion
2
2
  //#region src/index.ts
3
3
  /** The installed `distillate` package version. */
4
- const VERSION = "0.1.2";
4
+ const VERSION = "0.2.0";
5
5
  //#endregion
6
6
  export { VERSION };
@@ -0,0 +1,24 @@
1
+ //#region src/core/params.ts
2
+ /** Thrown when a structure is constructed with invalid parameters. */
3
+ var ParamError = class extends RangeError {
4
+ /** Discriminates this error from other `Error`s. */
5
+ name = "ParamError";
6
+ };
7
+ /** Asserts `value` is an integer greater than or equal to 1. */
8
+ function assertPositiveInt(value, label) {
9
+ if (!Number.isInteger(value) || value < 1) throw new ParamError(`${label} must be a positive integer, got ${String(value)}`);
10
+ }
11
+ /** Asserts `value` is a finite number greater than 0 (a positive real). */
12
+ function assertPositiveFinite(value, label) {
13
+ if (!Number.isFinite(value) || value <= 0) throw new ParamError(`${label} must be a positive number, got ${String(value)}`);
14
+ }
15
+ /** Asserts `value` is an integer in the uint32 range `[0, 2^32 - 1]`. */
16
+ function assertUint32(value, label) {
17
+ if (!Number.isInteger(value) || value < 0 || value > 4294967295) throw new ParamError(`${label} must be a uint32, got ${String(value)}`);
18
+ }
19
+ /** Asserts `value` is a finite number in the open interval `(0, 1)`. */
20
+ function assertProbability(value, label) {
21
+ if (!Number.isFinite(value) || value <= 0 || value >= 1) throw new ParamError(`${label} must be in the open interval (0, 1), got ${String(value)}`);
22
+ }
23
+ //#endregion
24
+ export { assertUint32 as a, assertProbability as i, assertPositiveFinite as n, assertPositiveInt as r, ParamError as t };
@@ -0,0 +1,8 @@
1
+ //#region src/core/params.d.ts
2
+ /** Thrown when a structure is constructed with invalid parameters. */
3
+ declare class ParamError extends RangeError {
4
+ /** Discriminates this error from other `Error`s. */
5
+ override readonly name = "ParamError";
6
+ }
7
+ //#endregion
8
+ export { ParamError as t };
@@ -0,0 +1,8 @@
1
+ //#region src/core/params.d.ts
2
+ /** Thrown when a structure is constructed with invalid parameters. */
3
+ declare class ParamError extends RangeError {
4
+ /** Discriminates this error from other `Error`s. */
5
+ override readonly name = "ParamError";
6
+ }
7
+ //#endregion
8
+ export { ParamError as t };
@@ -0,0 +1,53 @@
1
+ //#region src/core/params.ts
2
+ /** Thrown when a structure is constructed with invalid parameters. */
3
+ var ParamError = class extends RangeError {
4
+ /** Discriminates this error from other `Error`s. */
5
+ name = "ParamError";
6
+ };
7
+ /** Asserts `value` is an integer greater than or equal to 1. */
8
+ function assertPositiveInt(value, label) {
9
+ if (!Number.isInteger(value) || value < 1) throw new ParamError(`${label} must be a positive integer, got ${String(value)}`);
10
+ }
11
+ /** Asserts `value` is a finite number greater than 0 (a positive real). */
12
+ function assertPositiveFinite(value, label) {
13
+ if (!Number.isFinite(value) || value <= 0) throw new ParamError(`${label} must be a positive number, got ${String(value)}`);
14
+ }
15
+ /** Asserts `value` is an integer in the uint32 range `[0, 2^32 - 1]`. */
16
+ function assertUint32(value, label) {
17
+ if (!Number.isInteger(value) || value < 0 || value > 4294967295) throw new ParamError(`${label} must be a uint32, got ${String(value)}`);
18
+ }
19
+ /** Asserts `value` is a finite number in the open interval `(0, 1)`. */
20
+ function assertProbability(value, label) {
21
+ if (!Number.isFinite(value) || value <= 0 || value >= 1) throw new ParamError(`${label} must be in the open interval (0, 1), got ${String(value)}`);
22
+ }
23
+ //#endregion
24
+ Object.defineProperty(exports, "ParamError", {
25
+ enumerable: true,
26
+ get: function() {
27
+ return ParamError;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "assertPositiveFinite", {
31
+ enumerable: true,
32
+ get: function() {
33
+ return assertPositiveFinite;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "assertPositiveInt", {
37
+ enumerable: true,
38
+ get: function() {
39
+ return assertPositiveInt;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "assertProbability", {
43
+ enumerable: true,
44
+ get: function() {
45
+ return assertProbability;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "assertUint32", {
49
+ enumerable: true,
50
+ get: function() {
51
+ return assertUint32;
52
+ }
53
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "distillate",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Probabilistic data structures for JavaScript. Space-efficient, approximate-membership filters (Bloom, Blocked Bloom, Binary Fuse) with tunable error and a portable binary format; zero dependencies, universal.",
5
5
  "keywords": [
6
6
  "bloom-filter",