@lowentry/utils 1.0.4 → 1.1.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 +48 -1
- package/package.json +1 -1
package/LeUtils.js
CHANGED
|
@@ -1500,6 +1500,20 @@ export const LeUtils = {
|
|
|
1500
1500
|
let previousUniqueIdsTime = null;
|
|
1501
1501
|
let previousUniqueIds = {};
|
|
1502
1502
|
|
|
1503
|
+
const numberToBytes = (number) =>
|
|
1504
|
+
{
|
|
1505
|
+
const size = (number === 0) ? 0 : Math.ceil((Math.floor(Math.log2(number)) + 1) / 8);
|
|
1506
|
+
const bytes = new Uint8ClampedArray(size);
|
|
1507
|
+
let x = number;
|
|
1508
|
+
for(let i = (size - 1); i >= 0; i--)
|
|
1509
|
+
{
|
|
1510
|
+
const rightByte = x & 0xff;
|
|
1511
|
+
bytes[i] = rightByte;
|
|
1512
|
+
x = Math.floor(x / 0x100);
|
|
1513
|
+
}
|
|
1514
|
+
return bytes;
|
|
1515
|
+
};
|
|
1516
|
+
|
|
1503
1517
|
const generateUniqueId = () =>
|
|
1504
1518
|
{
|
|
1505
1519
|
let now;
|
|
@@ -1516,10 +1530,43 @@ export const LeUtils = {
|
|
|
1516
1530
|
{
|
|
1517
1531
|
now = (Date.now ? Date.now() : (new Date()).getTime());
|
|
1518
1532
|
}
|
|
1533
|
+
now = Math.round(now);
|
|
1534
|
+
const nowBytes = numberToBytes(now);
|
|
1535
|
+
|
|
1536
|
+
let uuid = null;
|
|
1537
|
+
try
|
|
1538
|
+
{
|
|
1539
|
+
uuid = crypto?.randomUUID();
|
|
1540
|
+
}
|
|
1541
|
+
catch(e)
|
|
1542
|
+
{
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
if(uuid)
|
|
1546
|
+
{
|
|
1547
|
+
uuid = LeUtils.base64ToBytes(LeUtils.hexToBase64(uuid));
|
|
1548
|
+
}
|
|
1549
|
+
else
|
|
1550
|
+
{
|
|
1551
|
+
const bytesChunkA = numberToBytes((Math.random() + ' ').substring(2, 12).padEnd(10, '0'));
|
|
1552
|
+
const bytesChunkB = numberToBytes((Math.random() + ' ').substring(2, 12).padEnd(10, '0'));
|
|
1553
|
+
const bytesChunkC = numberToBytes((Math.random() + ' ').substring(2, 12).padEnd(10, '0'));
|
|
1554
|
+
const bytesChunkD = numberToBytes((Math.random() + ' ').substring(2, 12).padEnd(10, '0'));
|
|
1555
|
+
uuid = new Uint8Array(bytesChunkA.length + bytesChunkB.length + bytesChunkC.length + bytesChunkD.length);
|
|
1556
|
+
uuid.set(bytesChunkA, 0);
|
|
1557
|
+
uuid.set(bytesChunkB, bytesChunkA.length);
|
|
1558
|
+
uuid.set(bytesChunkC, bytesChunkA.length + bytesChunkB.length);
|
|
1559
|
+
uuid.set(bytesChunkD, bytesChunkA.length + bytesChunkB.length + bytesChunkC.length);
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
const bytes = new Uint8Array(nowBytes.length + uuid.length);
|
|
1563
|
+
bytes.set(nowBytes, 0);
|
|
1564
|
+
bytes.set(uuid, nowBytes.length);
|
|
1565
|
+
uuid = LeUtils.bytesToBase64(bytes).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
1519
1566
|
|
|
1520
1567
|
return {
|
|
1521
1568
|
time:now,
|
|
1522
|
-
id:
|
|
1569
|
+
id: uuid,
|
|
1523
1570
|
};
|
|
1524
1571
|
};
|
|
1525
1572
|
|