@softsky/utils 2.5.0 → 2.5.2

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/README.md CHANGED
@@ -107,10 +107,18 @@ ${\textsf{\color{ForestGreen}const}}$ SEC_MS - Milliseconds in a second
107
107
  ## Control
108
108
  Utils related to code execution flow.
109
109
 
110
- ${\textsf{\color{ForestGreen}const}}$ generateNumberId - Get unique number id
110
+ ${\textsf{\color{ForestGreen}const}}$ SESSION_ID - Id generated only once per session
111
111
 
112
112
  ---
113
- ${\textsf{\color{CornflowerBlue}function}}$ UUID - Get universally unique string id
113
+ ${\textsf{\color{CornflowerBlue}function}}$ UUID - Get universally unique string id.
114
+ You can get information then id was generated using extractUUIDDate(uuid)
115
+ - 13 char - timestamp
116
+ - 13 char - SESSION_ID
117
+ - 4 char - incremental id
118
+
119
+ 30 char total.
120
+
121
+ USING CUSTOM TIMESTAMP MAY RESULT IN COLLISSIONS
114
122
 
115
123
  ---
116
124
  ${\textsf{\color{CornflowerBlue}function}}$ extractUUIDDate - Extract exact date of uuid generation
package/dist/control.d.ts CHANGED
@@ -2,10 +2,20 @@
2
2
  * Utils related to code execution flow.
3
3
  */
4
4
  import { AwaitedObject, JSONSerializable } from './types';
5
- /** Get unique number id */
6
- export declare const generateNumberId: () => number;
7
- /** Get universally unique string id */
8
- export declare function UUID(): string;
5
+ /** Id generated only once per session */
6
+ export declare const SESSION_ID: string;
7
+ /**
8
+ * Get universally unique string id.
9
+ * You can get information then id was generated using extractUUIDDate(uuid)
10
+ * - 13 char - timestamp
11
+ * - 13 char - SESSION_ID
12
+ * - 4 char - incremental id
13
+ *
14
+ * 30 char total.
15
+ *
16
+ * USING CUSTOM TIMESTAMP MAY RESULT IN COLLISSIONS
17
+ */
18
+ export declare function UUID(timestamp?: number): string;
9
19
  /** Extract exact date of uuid generation */
10
20
  export declare function extractUUIDDate(uuid: string): Date;
11
21
  /**
package/dist/control.js CHANGED
@@ -1,18 +1,33 @@
1
1
  /**
2
2
  * Utils related to code execution flow.
3
3
  */
4
- let _uuid = Date.now() * 1000;
5
- /** Get unique number id */
6
- export const generateNumberId = () => _uuid++;
7
- const SESSION_ID = ((Math.random() * 2_147_483_648) | 0).toString(16);
8
- let stringIdInc = 0;
9
- /** Get universally unique string id */
10
- export function UUID() {
11
- return `${Date.now().toString(36).padStart(11, '0')}${(++stringIdInc).toString(36).padStart(3, '0')}${SESSION_ID}`;
4
+ let lastIncId = Math.floor(Math.random() * 0x1_00_00);
5
+ /** Id generated only once per session */
6
+ export const SESSION_ID = Math.floor(Math.random() * 0x10_00_00_00_00_00_00)
7
+ .toString(16)
8
+ .padStart(13, '0');
9
+ /**
10
+ * Get universally unique string id.
11
+ * You can get information then id was generated using extractUUIDDate(uuid)
12
+ * - 13 char - timestamp
13
+ * - 13 char - SESSION_ID
14
+ * - 4 char - incremental id
15
+ *
16
+ * 30 char total.
17
+ *
18
+ * USING CUSTOM TIMESTAMP MAY RESULT IN COLLISSIONS
19
+ */
20
+ export function UUID(timestamp = Date.now()) {
21
+ let inc = (++lastIncId).toString(16).padStart(4, '0');
22
+ if (inc.length === 5) {
23
+ lastIncId = 0;
24
+ inc = '0000';
25
+ }
26
+ return `${timestamp.toString(16).padStart(13, '0')}${inc}${SESSION_ID}`;
12
27
  }
13
28
  /** Extract exact date of uuid generation */
14
29
  export function extractUUIDDate(uuid) {
15
- return new Date(Number.parseInt(uuid.slice(0, 11), 36));
30
+ return new Date(Number.parseInt(uuid.slice(0, 13), 16));
16
31
  }
17
32
  /**
18
33
  * Creates cached function. All arguments/results are cached.
@@ -80,7 +80,7 @@ export function formatNumber(time, min = 0, ranges = FORMAT_NUMBER_RANGES) {
80
80
  break;
81
81
  if (time < start && !pad)
82
82
  continue;
83
- let value = ((time / start) | 0).toString();
83
+ let value = Math.floor(time / start).toString();
84
84
  time %= start;
85
85
  if (pad)
86
86
  value = value.padStart(pad, '0');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softsky/utils",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
4
4
  "description": "JavaScript/TypeScript utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {