@lowentry/utils 1.3.2 → 1.5.1
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/LeUtils.js +53 -1
- package/package.json +1 -1
package/LeUtils.js
CHANGED
|
@@ -1490,7 +1490,7 @@ export const LeUtils = {
|
|
|
1490
1490
|
},
|
|
1491
1491
|
|
|
1492
1492
|
/**
|
|
1493
|
-
* Generates a string that is guaranteed to be unique
|
|
1493
|
+
* Generates a base64 string (with +/ replaced by -_) that is guaranteed to be unique.
|
|
1494
1494
|
*
|
|
1495
1495
|
* @returns {string}
|
|
1496
1496
|
*/
|
|
@@ -1590,6 +1590,58 @@ export const LeUtils = {
|
|
|
1590
1590
|
};
|
|
1591
1591
|
})(),
|
|
1592
1592
|
|
|
1593
|
+
/**
|
|
1594
|
+
* Generates a base64 string (with +/ replaced by -_) of the current time (in milliseconds since 1970).
|
|
1595
|
+
*
|
|
1596
|
+
* @param {number} [now] Optional time to use instead of the current time. If not set, the current time will be used.
|
|
1597
|
+
* @returns {string}
|
|
1598
|
+
*/
|
|
1599
|
+
timestamp:
|
|
1600
|
+
(() =>
|
|
1601
|
+
{
|
|
1602
|
+
const numberToBytes = (number) =>
|
|
1603
|
+
{
|
|
1604
|
+
const size = (number === 0) ? 0 : Math.ceil((Math.floor(Math.log2(number)) + 1) / 8);
|
|
1605
|
+
const bytes = new Uint8ClampedArray(size);
|
|
1606
|
+
let x = number;
|
|
1607
|
+
for(let i = (size - 1); i >= 0; i--)
|
|
1608
|
+
{
|
|
1609
|
+
const rightByte = x & 0xff;
|
|
1610
|
+
bytes[i] = rightByte;
|
|
1611
|
+
x = Math.floor(x / 0x100);
|
|
1612
|
+
}
|
|
1613
|
+
return bytes;
|
|
1614
|
+
};
|
|
1615
|
+
|
|
1616
|
+
return (now = null) =>
|
|
1617
|
+
{
|
|
1618
|
+
if(ISSET(now))
|
|
1619
|
+
{
|
|
1620
|
+
now = FLOAT_LAX(now);
|
|
1621
|
+
}
|
|
1622
|
+
else
|
|
1623
|
+
{
|
|
1624
|
+
try
|
|
1625
|
+
{
|
|
1626
|
+
// noinspection JSDeprecatedSymbols
|
|
1627
|
+
now = (performance.timeOrigin || performance.timing.navigationStart) + performance.now();
|
|
1628
|
+
if(typeof now !== 'number')
|
|
1629
|
+
{
|
|
1630
|
+
throw new Error();
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
catch(e)
|
|
1634
|
+
{
|
|
1635
|
+
now = (Date.now ? Date.now() : (new Date()).getTime());
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
now = Math.round(now);
|
|
1639
|
+
const nowBytes = numberToBytes(now);
|
|
1640
|
+
|
|
1641
|
+
return LeUtils.bytesToBase64(nowBytes).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
1642
|
+
};
|
|
1643
|
+
})(),
|
|
1644
|
+
|
|
1593
1645
|
/**
|
|
1594
1646
|
* Returns a data URL of a 1x1 transparent pixel.
|
|
1595
1647
|
*
|