@antti5/ship-tool-common 1.0.4 → 1.0.5

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/lib/UID.js +67 -0
  2. package/lib/Util.js +4 -81
  3. package/package.json +2 -1
package/lib/UID.js ADDED
@@ -0,0 +1,67 @@
1
+ class UID {
2
+
3
+ /* Return an UID with the following properties:
4
+
5
+ 1) Length is fixed at 10 characters.
6
+ 2) Used characters are 0-9, a-z and A-Z.
7
+ 3) Characters at both ends of the UID change more often.
8
+ 4) Uniqueness in the same process is guaranteed for 25 years.
9
+ 5) If UIDs are created in two different processes within a one-second time
10
+ window, the probability of a collision is less than one in billion. */
11
+
12
+ static #uidOldTimestamp = null;
13
+ static #uidCounter = null;
14
+
15
+ static create() {
16
+
17
+ const DIGITS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
18
+ const BASE = DIGITS.length;
19
+ const TIMESTAMP_DIGITS = 7;
20
+ const TIMESTAMP_WRAPAROUND_YEARS = 25;
21
+ const COUNTER_DIGITS = 3;
22
+
23
+ /* Return a positive integer in a different base. */
24
+
25
+ const ntoaBase = n => {
26
+ let result = '';
27
+ while (n >= BASE) {
28
+ result = DIGITS[n % BASE] + result;
29
+ n = Math.floor(n / BASE);
30
+ };
31
+ return DIGITS[n % BASE] + result;
32
+ };
33
+
34
+ /* Create a timestamp value that as a 7-digit base-62 number wraps around every 25 years.
35
+ Timestamp value changes every 0.224 milliseconds. */
36
+
37
+ const timestampMax = BASE ** TIMESTAMP_DIGITS;
38
+ const msPerTimestamp =
39
+ 1 / (timestampMax / TIMESTAMP_WRAPAROUND_YEARS / 365 / 24 / 60 / 60 / 1000);
40
+ const timestamp = Math.abs(Math.floor(Date.now() / msPerTimestamp) % timestampMax);
41
+
42
+ /* Create a counter value represented as a 3-digit base-62 number. If the timestamp is
43
+ different from previous call the counter value is reset to a random value to avoid using
44
+ the same counter value in different processes.
45
+
46
+ If the timestamp has not changed, however, the counter is incremented by one to guarantee
47
+ 62 ** 3 = 238328 different UIDs with the same timestamp value. */
48
+
49
+ const counterMax = BASE ** COUNTER_DIGITS;
50
+ if (timestamp !== UID.#uidOldTimestamp)
51
+ UID.#uidCounter = Math.floor(Math.random() * counterMax);
52
+ else
53
+ UID.#uidCounter = (UID.#uidCounter + 1) % counterMax;
54
+ UID.#uidOldTimestamp = timestamp;
55
+
56
+ /* Counter value is first in the UID because it's more random, thus providing more random
57
+ characters at the start of the UID. Counter value is reversed so that the first character
58
+ changes when several UIDs are created with the same timestamp value. */
59
+
60
+ let uid = '';
61
+ ntoaBase(UID.#uidCounter).padStart(COUNTER_DIGITS, '0').split('').reverse().forEach(c => uid += c);
62
+ uid += ntoaBase(timestamp).padStart(TIMESTAMP_DIGITS, '0');
63
+ return uid;
64
+ }
65
+ }
66
+
67
+ export default UID;
package/lib/Util.js CHANGED
@@ -77,7 +77,7 @@ class Util {
77
77
  const responseBody =
78
78
  response.headers.get('Content-Type')?.includes('application/json') ?
79
79
  await response.json() :
80
- await response.text(); // If response isn't JSON, return a String instead -- TODO: This may be very poor design
80
+ await response.text(); // If response isn't JSON, return a String instead -- TODO: This may be a poor design
81
81
 
82
82
  if (debug)
83
83
  console.debug('fetch() response body:', responseBody);
@@ -128,12 +128,12 @@ class Util {
128
128
  }
129
129
 
130
130
 
131
- /* Generated random numbers and index for the next number. */
131
+ /* Generated random numbers and index for the next number */
132
132
 
133
133
  static #randoms = [];
134
134
  static #randomsIndex = 0;
135
135
 
136
- /* Get a random number, either from the #randoms array or create and store a new one. */
136
+ /* Get a random number, either from the #randoms array or create and store a new one */
137
137
 
138
138
  static random() {
139
139
  if (Util.#randomsIndex === Util.#randoms.length)
@@ -141,77 +141,13 @@ class Util {
141
141
  return Util.#randoms[Util.#randomsIndex++];
142
142
  }
143
143
 
144
- /* Switch back to the start of randoms array to return the same random numbers again. */
144
+ /* Switch back to the start of randoms array to return the same random numbers again */
145
145
 
146
146
  static randomReset() {
147
147
  Util.#randomsIndex = 0;
148
148
  }
149
149
 
150
150
 
151
- /* Return an UID with the following properties:
152
-
153
- 1) Length is fixed at 10 characters.
154
- 2) Used characters are 0-9, a-z and A-Z.
155
- 3) Characters at both ends of the UID change more often.
156
- 4) Uniqueness in the same process is guaranteed for 25 years.
157
- 5) If UIDs are created in two different processes within a one-second time
158
- window, the probability of a collision is less than one in billion. */
159
-
160
- static #uidOldTimestamp = null;
161
- static #uidCounter = null;
162
-
163
- static uid() {
164
-
165
- const DIGITS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
166
- const BASE = DIGITS.length;
167
- const TIMESTAMP_DIGITS = 7;
168
- const TIMESTAMP_WRAPAROUND_YEARS = 25;
169
- const COUNTER_DIGITS = 3;
170
-
171
- /* Return a positive integer in a different base. */
172
-
173
- const ntoaBase = n => {
174
- let result = '';
175
- while (n >= BASE) {
176
- result = DIGITS[n % BASE] + result;
177
- n = Math.floor(n / BASE);
178
- };
179
- return DIGITS[n % BASE] + result;
180
- };
181
-
182
- /* Create a timestamp value that as a 7-digit base-62 number wraps around every 25 years.
183
- Timestamp value changes every 0.224 milliseconds. */
184
-
185
- const timestampMax = BASE ** TIMESTAMP_DIGITS;
186
- const msPerTimestamp =
187
- 1 / (timestampMax / TIMESTAMP_WRAPAROUND_YEARS / 365 / 24 / 60 / 60 / 1000);
188
- const timestamp = Math.abs(Math.floor(Date.now() / msPerTimestamp) % timestampMax);
189
-
190
- /* Create a counter value represented as a 3-digit base-62 number. If the timestamp is
191
- different from previous call the counter value is reset to a random value to avoid using
192
- the same counter value in different processes.
193
-
194
- If the timestamp has not changed, however, the counter is incremented by one to guarantee
195
- 62 ** 3 = 238328 different UIDs with the same timestamp value. */
196
-
197
- const counterMax = BASE ** COUNTER_DIGITS;
198
- if (timestamp !== Util.#uidOldTimestamp)
199
- Util.#uidCounter = Math.floor(Math.random() * counterMax);
200
- else
201
- Util.#uidCounter = (Util.#uidCounter + 1) % counterMax;
202
- Util.#uidOldTimestamp = timestamp;
203
-
204
- /* Counter value is first in the UID because it's more random, thus providing more random
205
- characters at the start of the UID. Counter value is reversed so that the first character
206
- changes when several UIDs are created with the same timestamp value. */
207
-
208
- let uid = '';
209
- ntoaBase(Util.#uidCounter).padStart(COUNTER_DIGITS, '0').split('').reverse().forEach(c => uid += c);
210
- uid += ntoaBase(timestamp).padStart(TIMESTAMP_DIGITS, '0');
211
- return uid;
212
- }
213
-
214
-
215
151
  static perfObject(key) {
216
152
  window.ST_PERF ??= {};
217
153
  window.ST_PERF[key] ??= [];
@@ -673,19 +609,6 @@ class Util {
673
609
  }
674
610
 
675
611
 
676
- /* Return the prototype chain of an object as an array of constructor names */
677
-
678
- static getPrototypeChain(o) {
679
- const result = [];
680
- let p = Object.getPrototypeOf(o);
681
- while(p) {
682
- result.push(p.constructor.name);
683
- p = Object.getPrototypeOf(p);
684
- }
685
- return result;
686
- }
687
-
688
-
689
612
  /* Linear regression using least squares method.
690
613
 
691
614
  Adapted from: https://stackoverflow.com/a/31566791
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antti5/ship-tool-common",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Ship Tool common utils and library code",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,6 +18,7 @@
18
18
  "exports": {
19
19
  "./Util": "./lib/Util.js",
20
20
  "./Semaphore": "./lib/Semaphore.js",
21
+ "./UID": "./lib/UID.js",
21
22
  "./Trajectory": "./lib/trajectory/Trajectory.js",
22
23
  "./node/Util": "./lib/UtilNode.js"
23
24
  },