cafe-utility 24.0.1 → 25.0.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.
Files changed (3) hide show
  1. package/index.d.ts +8 -8
  2. package/index.js +26 -18
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -492,10 +492,10 @@ declare function isPng(bytes: Uint8Array): boolean;
492
492
  declare function isJpg(bytes: Uint8Array): boolean;
493
493
  declare function isWebp(bytes: Uint8Array): boolean;
494
494
  declare function isImage(bytes: Uint8Array): boolean;
495
- declare function numberToUint64LE(number: number): Uint8Array;
496
- declare function uint64LEToNumber(bytes: Uint8Array): number;
497
- declare function numberToUint64BE(number: number): Uint8Array;
498
- declare function uint64BEToNumber(bytes: Uint8Array): number;
495
+ declare function numberToUint256(number: bigint, endian: 'LE' | 'BE'): Uint8Array;
496
+ declare function uint256ToNumber(bytes: Uint8Array, endian: 'LE' | 'BE'): bigint;
497
+ declare function numberToUint64(number: number, endian: 'LE' | 'BE'): Uint8Array;
498
+ declare function uint64ToNumber(bytes: Uint8Array, endian: 'LE' | 'BE'): number;
499
499
  interface Uint8ArrayIO {
500
500
  max: () => number;
501
501
  }
@@ -592,10 +592,10 @@ export declare const Binary: {
592
592
  merkleStart: typeof merkleStart;
593
593
  merkleAppend: typeof merkleAppend;
594
594
  merkleFinalize: typeof merkleFinalize;
595
- numberToUint64LE: typeof numberToUint64LE;
596
- uint64LEToNumber: typeof uint64LEToNumber;
597
- numberToUint64BE: typeof numberToUint64BE;
598
- uint64BEToNumber: typeof uint64BEToNumber;
595
+ numberToUint64: typeof numberToUint64;
596
+ uint64ToNumber: typeof uint64ToNumber;
597
+ numberToUint256: typeof numberToUint256;
598
+ uint256ToNumber: typeof uint256ToNumber;
599
599
  };
600
600
  export declare const Random: {
601
601
  intBetween: typeof intBetween;
package/index.js CHANGED
@@ -2076,21 +2076,29 @@ function isWebp(n) {
2076
2076
  function isImage(n) {
2077
2077
  return isPng(n) || isJpg(n) || isWebp(n)
2078
2078
  }
2079
- function numberToUint64LE(n) {
2080
- const e = new ArrayBuffer(8)
2081
- return new DataView(e).setBigUint64(0, BigInt(n), !0), new Uint8Array(e)
2079
+ function numberToUint256(n, e) {
2080
+ const r = new Uint8Array(32)
2081
+ let o = n
2082
+ if (e === 'LE') for (let i = 0; i < 32; i++) return (r[i] = Number(o & BigInt(255))), (o >>= BigInt(8)), r
2083
+ for (let i = 32 - 1; i >= 0; i--) (r[i] = Number(o & BigInt(255))), (o >>= BigInt(8))
2084
+ return r
2082
2085
  }
2083
- function uint64LEToNumber(n) {
2084
- const e = new DataView(n.buffer)
2085
- return Number(e.getBigUint64(0, !0))
2086
+ function uint256ToNumber(n, e) {
2087
+ let r = BigInt(0)
2088
+ if (e === 'LE') {
2089
+ for (let o = 32 - 1; o >= 0; o--) r = (r << BigInt(8)) | BigInt(n[o])
2090
+ return r
2091
+ }
2092
+ for (let o = 0; o < 32; o++) r = (r << BigInt(8)) | BigInt(n[o])
2093
+ return r
2086
2094
  }
2087
- function numberToUint64BE(n) {
2088
- const e = new ArrayBuffer(8)
2089
- return new DataView(e).setBigUint64(0, BigInt(n), !1), new Uint8Array(e)
2095
+ function numberToUint64(n, e) {
2096
+ const t = new ArrayBuffer(8)
2097
+ return new DataView(t).setBigUint64(0, BigInt(n), e === 'LE'), new Uint8Array(t)
2090
2098
  }
2091
- function uint64BEToNumber(n) {
2092
- const e = new DataView(n.buffer)
2093
- return Number(e.getBigUint64(0, !1))
2099
+ function uint64ToNumber(n, e) {
2100
+ const t = new DataView(n.buffer)
2101
+ return Number(t.getBigUint64(0, e === 'LE'))
2094
2102
  }
2095
2103
  class Uint8ArrayReader {
2096
2104
  constructor(e) {
@@ -2121,11 +2129,11 @@ class Chunk {
2121
2129
  ;(this.span = r), (this.writer = new Uint8ArrayWriter(new Uint8Array(e))), (this.hashFn = t)
2122
2130
  }
2123
2131
  build() {
2124
- return concatBytes(numberToUint64LE(this.span), this.writer.buffer)
2132
+ return concatBytes(numberToUint64(this.span, 'LE'), this.writer.buffer)
2125
2133
  }
2126
2134
  hash() {
2127
2135
  const e = log2Reduce(partition(this.writer.buffer, 32), this.hashFn)
2128
- return this.hashFn(numberToUint64LE(this.span), e)
2136
+ return this.hashFn(numberToUint64(this.span, 'LE'), e)
2129
2137
  }
2130
2138
  }
2131
2139
  function merkleStart(n, e) {
@@ -2462,10 +2470,10 @@ class AsyncQueue {
2462
2470
  merkleStart,
2463
2471
  merkleAppend,
2464
2472
  merkleFinalize,
2465
- numberToUint64LE,
2466
- uint64LEToNumber,
2467
- numberToUint64BE,
2468
- uint64BEToNumber
2473
+ numberToUint64,
2474
+ uint64ToNumber,
2475
+ numberToUint256,
2476
+ uint256ToNumber
2469
2477
  }),
2470
2478
  (exports.Random = {
2471
2479
  intBetween,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "24.0.1",
3
+ "version": "25.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {