lib0 0.2.49 → 0.2.50

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/dist/test.js CHANGED
@@ -1547,41 +1547,6 @@
1547
1547
  }
1548
1548
  };
1549
1549
 
1550
- /* eslint-env browser */
1551
- const performance = typeof window === 'undefined' ? null : (typeof window.performance !== 'undefined' && window.performance) || null;
1552
-
1553
- const isoCrypto = typeof crypto === 'undefined' ? null : crypto;
1554
-
1555
- /**
1556
- * @type {function(number):ArrayBuffer}
1557
- */
1558
- const cryptoRandomBuffer = isoCrypto !== null
1559
- ? len => {
1560
- // browser
1561
- const buf = new ArrayBuffer(len);
1562
- const arr = new Uint8Array(buf);
1563
- isoCrypto.getRandomValues(arr);
1564
- return buf
1565
- }
1566
- : len => {
1567
- // polyfill
1568
- const buf = new ArrayBuffer(len);
1569
- const arr = new Uint8Array(buf);
1570
- for (let i = 0; i < len; i++) {
1571
- arr[i] = Math.ceil((Math.random() * 0xFFFFFFFF) >>> 0);
1572
- }
1573
- return buf
1574
- };
1575
-
1576
- /* istanbul ignore next */
1577
- const uint32$1 = () => new Uint32Array(cryptoRandomBuffer(4))[0];
1578
-
1579
- // @ts-ignore
1580
- const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
1581
- const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
1582
- (c ^ uint32$1() & 15 >> c / 4).toString(16)
1583
- );
1584
-
1585
1550
  /* eslint-env browser */
1586
1551
 
1587
1552
  /**
@@ -1742,6 +1707,45 @@
1742
1707
  BITS32: BITS32
1743
1708
  });
1744
1709
 
1710
+ /* eslint-env browser */
1711
+ const performance = typeof window === 'undefined' ? null : (typeof window.performance !== 'undefined' && window.performance) || null;
1712
+
1713
+ const isoCrypto = typeof crypto === 'undefined' ? null : crypto;
1714
+
1715
+ /**
1716
+ * @type {function(number):ArrayBuffer}
1717
+ */
1718
+ const cryptoRandomBuffer = isoCrypto !== null
1719
+ ? len => {
1720
+ // browser
1721
+ const buf = new ArrayBuffer(len);
1722
+ const arr = new Uint8Array(buf);
1723
+ isoCrypto.getRandomValues(arr);
1724
+ return buf
1725
+ }
1726
+ : len => {
1727
+ // polyfill
1728
+ const buf = new ArrayBuffer(len);
1729
+ const arr = new Uint8Array(buf);
1730
+ for (let i = 0; i < len; i++) {
1731
+ arr[i] = Math.ceil((Math.random() * 0xFFFFFFFF) >>> 0);
1732
+ }
1733
+ return buf
1734
+ };
1735
+
1736
+ const uint32$1 = () => new Uint32Array(cryptoRandomBuffer(4))[0];
1737
+
1738
+ const uint53$1 = () => {
1739
+ const arr = new Uint32Array(cryptoRandomBuffer(8));
1740
+ return (arr[0] & BITS21) * (BITS32 + 1) + (arr[1] >>> 0)
1741
+ };
1742
+
1743
+ // @ts-ignore
1744
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
1745
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
1746
+ (c ^ uint32$1() & 15 >> c / 4).toString(16)
1747
+ );
1748
+
1745
1749
  /**
1746
1750
  * @module prng
1747
1751
  */
@@ -3414,7 +3418,7 @@
3414
3418
  /**
3415
3419
  * Description of the function
3416
3420
  * @callback generatorNext
3417
- * @return {number} A 32bit integer
3421
+ * @return {number} A random float in the cange of [0,1)
3418
3422
  */
3419
3423
 
3420
3424
  /**
@@ -6078,6 +6082,34 @@
6078
6082
  assert(((smallest & BITS32) >>> 0) === smallest, 'Smallest number is 32 bits long.');
6079
6083
  };
6080
6084
 
6085
+ /**
6086
+ * @param {t.TestCase} tc
6087
+ */
6088
+ const testUint53 = tc => {
6089
+ const iterations = 10000;
6090
+ let largest = 0;
6091
+ let smallest = MAX_SAFE_INTEGER;
6092
+ let newNum = 0;
6093
+ let lenSum = 0;
6094
+ let ones = 0;
6095
+ for (let i = 0; i < iterations; i++) {
6096
+ newNum = uint53$1();
6097
+ lenSum += newNum.toString().length;
6098
+ ones += newNum.toString(2).split('').filter(x => x === '1').length;
6099
+ if (newNum > largest) {
6100
+ largest = newNum;
6101
+ }
6102
+ if (newNum < smallest) {
6103
+ smallest = newNum;
6104
+ }
6105
+ }
6106
+ info(`Largest number generated is ${largest}`);
6107
+ info(`Smallest number generated is ${smallest}`);
6108
+ info(`Average decimal length of number is ${lenSum / iterations}`);
6109
+ info(`Average number of 1s in number is ${ones / iterations} (expecting ~26.5)`);
6110
+ assert(largest > MAX_SAFE_INTEGER * 0.9);
6111
+ };
6112
+
6081
6113
  /**
6082
6114
  * @param {t.TestCase} tc
6083
6115
  */
@@ -6109,6 +6141,7 @@
6109
6141
  var random = /*#__PURE__*/Object.freeze({
6110
6142
  __proto__: null,
6111
6143
  testUint32: testUint32,
6144
+ testUint53: testUint53,
6112
6145
  testUuidv4: testUuidv4,
6113
6146
  testUuidv4Overlaps: testUuidv4Overlaps
6114
6147
  });