@ancon/wildcat-utils 1.53.1 → 1.53.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.53.1",
3
+ "version": "1.53.3",
4
4
  "main": "index.js",
5
5
  "module": "index.mjs",
6
6
  "types": "index.d.ts",
@@ -1115,6 +1115,11 @@
1115
1115
  "require": "./quantity/index.js",
1116
1116
  "types": "./quantity/index.d.ts"
1117
1117
  },
1118
+ "./shared/bitmask": {
1119
+ "import": "./shared/bitmask.mjs",
1120
+ "require": "./shared/bitmask.js",
1121
+ "types": "./shared/bitmask.d.ts"
1122
+ },
1118
1123
  "./shared/constants": {
1119
1124
  "import": "./shared/constants.mjs",
1120
1125
  "require": "./shared/constants.js",
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Bitmasks where flag `id` is stored as bit 2^(id - 1), e.g. the gift card
3
+ * template `templates` mask. Ids must be positive integers.
4
+ *
5
+ * Uses BigInt internally, because JavaScript's 32-bit bitwise operators give
6
+ * wrong results for ids above 31.
7
+ */
8
+ /** Bit value of one flag: 2^(id - 1). */
9
+ export declare function getBitValue(id: number): bigint;
10
+ /** Whether flag `id` is set in `mask`. A null or undefined mask has no flags set. */
11
+ export declare function isBitSet<T extends number>(mask: number | null | undefined, id: T): boolean;
12
+ /** Sum of the given ids as a mask. Returns null when there are no ids. */
13
+ export declare function idsToBitmask<T extends number>(ids: readonly T[]): number | null;
14
+ /**
15
+ * The candidate ids that are set in `mask`, in the order of `candidates`.
16
+ * Needed because a mask alone does not say which ids exist.
17
+ *
18
+ * Generic over the id type, so numeric enums keep their type:
19
+ * `bitmaskToIds<GiftCardDesignTemplate>(templates, templateIds)`.
20
+ */
21
+ export declare function bitmaskToIds<T extends number>(mask: number | null | undefined, candidates: readonly T[]): T[];
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function s(t){if(!Number.isInteger(t)||t<1)throw new RangeError(`Bitmask id must be a positive integer, got ${t}`)}function n(t){return s(t),BigInt(2)**BigInt(t-1)}function r(t,e){return t?(BigInt(t)&n(e))!==BigInt(0):!1}function o(t){if(t.length===0)return null;const e=t.reduce((i,u)=>i|n(u),BigInt(0));return Number(e)}function a(t,e){return e.filter(i=>r(t,i))}exports.bitmaskToIds=a;exports.getBitValue=n;exports.idsToBitmask=o;exports.isBitSet=r;
@@ -0,0 +1,28 @@
1
+ function u(t) {
2
+ if (!Number.isInteger(t) || t < 1)
3
+ throw new RangeError(`Bitmask id must be a positive integer, got ${t}`);
4
+ }
5
+ function r(t) {
6
+ return u(t), BigInt(2) ** BigInt(t - 1);
7
+ }
8
+ function o(t, n) {
9
+ return t ? (BigInt(t) & r(n)) !== BigInt(0) : !1;
10
+ }
11
+ function s(t) {
12
+ if (t.length === 0)
13
+ return null;
14
+ const n = t.reduce(
15
+ (e, i) => e | r(i),
16
+ BigInt(0)
17
+ );
18
+ return Number(n);
19
+ }
20
+ function g(t, n) {
21
+ return n.filter((e) => o(t, e));
22
+ }
23
+ export {
24
+ g as bitmaskToIds,
25
+ r as getBitValue,
26
+ s as idsToBitmask,
27
+ o as isBitSet
28
+ };
package/shared/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import isWithinFlag from './isWithinFlag';
2
+ import { bitmaskToIds, getBitValue, idsToBitmask, isBitSet } from './bitmask';
2
3
  import generateId from './generateId';
3
4
  import wait from './wait';
4
5
  import kiloGramToGram from './kiloGramToGram';
5
6
  import gramToKiloGram from './gramToKiloGram';
6
7
  import { useMountedRef, usePrevious } from './hooks';
7
- export { isWithinFlag, generateId, wait, useMountedRef, usePrevious, gramToKiloGram, kiloGramToGram, };
8
+ export { isWithinFlag, bitmaskToIds, getBitValue, idsToBitmask, isBitSet, generateId, wait, useMountedRef, usePrevious, gramToKiloGram, kiloGramToGram, };
package/shared/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./isWithinFlag.js"),r=require("./generateId.js"),o=require("./wait.js"),s=require("./kiloGramToGram.js"),i=require("./gramToKiloGram.js"),a=require("./hooks/usePrevious.js"),t=require("./hooks/useMountedRef.js");require("react");exports.isWithinFlag=e;exports.generateId=r;exports.wait=o;exports.kiloGramToGram=s;exports.gramToKiloGram=i;exports.usePrevious=a;exports.useMountedRef=t;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./isWithinFlag.js"),e=require("./bitmask.js"),i=require("./generateId.js"),s=require("./wait.js"),t=require("./kiloGramToGram.js"),o=require("./gramToKiloGram.js"),a=require("./hooks/usePrevious.js"),u=require("./hooks/useMountedRef.js");require("react");exports.isWithinFlag=r;exports.bitmaskToIds=e.bitmaskToIds;exports.getBitValue=e.getBitValue;exports.idsToBitmask=e.idsToBitmask;exports.isBitSet=e.isBitSet;exports.generateId=i;exports.wait=s;exports.kiloGramToGram=t;exports.gramToKiloGram=o;exports.usePrevious=a;exports.useMountedRef=u;
package/shared/index.mjs CHANGED
@@ -1,17 +1,22 @@
1
- import { default as r } from "./isWithinFlag.mjs";
2
- import { default as f } from "./generateId.mjs";
3
- import { default as s } from "./wait.mjs";
4
- import { default as l } from "./kiloGramToGram.mjs";
5
- import { default as i } from "./gramToKiloGram.mjs";
6
- import { default as x } from "./hooks/usePrevious.mjs";
7
- import { default as n } from "./hooks/useMountedRef.mjs";
1
+ import { default as a } from "./isWithinFlag.mjs";
2
+ import { bitmaskToIds as f, getBitValue as s, idsToBitmask as m, isBitSet as i } from "./bitmask.mjs";
3
+ import { default as d } from "./generateId.mjs";
4
+ import { default as p } from "./wait.mjs";
5
+ import { default as g } from "./kiloGramToGram.mjs";
6
+ import { default as k } from "./gramToKiloGram.mjs";
7
+ import { default as B } from "./hooks/usePrevious.mjs";
8
+ import { default as I } from "./hooks/useMountedRef.mjs";
8
9
  import "react";
9
10
  export {
10
- f as generateId,
11
- i as gramToKiloGram,
12
- r as isWithinFlag,
13
- l as kiloGramToGram,
14
- n as useMountedRef,
15
- x as usePrevious,
16
- s as wait
11
+ f as bitmaskToIds,
12
+ d as generateId,
13
+ s as getBitValue,
14
+ k as gramToKiloGram,
15
+ m as idsToBitmask,
16
+ i as isBitSet,
17
+ a as isWithinFlag,
18
+ g as kiloGramToGram,
19
+ I as useMountedRef,
20
+ B as usePrevious,
21
+ p as wait
17
22
  };