@hkdigital/lib-core 0.3.9 → 0.3.10

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.
@@ -17,7 +17,10 @@
17
17
 
18
18
  /* ------------------------------------------------------------------ Imports */
19
19
 
20
- import { ALPHABET_BASE_HUMAN, ALPHABET_BASE_58 } from '../../constants/bases/index.js';
20
+ import {
21
+ ALPHABET_BASE_HUMAN,
22
+ ALPHABET_BASE_58
23
+ } from '../../constants/bases/index.js';
21
24
 
22
25
  import { base58fromNumber } from '../bases';
23
26
 
@@ -35,7 +38,7 @@ import { sinceMs } from '../time';
35
38
  */
36
39
  var vars = {}; /* @note use 'var declaration' for hoisting */
37
40
 
38
- export const BOOT_STAMP = ( Date.now() - TIME_2025_01_01 ).toString(36);
41
+ export const BOOT_STAMP = (Date.now() - TIME_2025_01_01).toString(36);
39
42
 
40
43
  /* ------------------------------------------------------------------ Exports */
41
44
 
@@ -44,10 +47,8 @@ export const BOOT_STAMP = ( Date.now() - TIME_2025_01_01 ).toString(36);
44
47
  *
45
48
  * @returns {string} boot time prefix
46
49
  */
47
- export function bootTimePrefix()
48
- {
49
- if( !vars.bootTimePrefix )
50
- {
50
+ export function bootTimePrefix() {
51
+ if (!vars.bootTimePrefix) {
51
52
  vars.bootTimePrefix = '3' + getTwoChar10ms();
52
53
  }
53
54
 
@@ -61,9 +62,8 @@ export function bootTimePrefix()
61
62
  *
62
63
  * @returns {string} a base 58 encoded random string
63
64
  */
64
- export function randomStringBase58( length=48 )
65
- {
66
- return randomString( length, ALPHABET_BASE_58 );
65
+ export function randomStringBase58(length = 48) {
66
+ return randomString(length, ALPHABET_BASE_58);
67
67
  }
68
68
 
69
69
  /**
@@ -74,9 +74,8 @@ export function randomStringBase58( length=48 )
74
74
  *
75
75
  * @returns {string} a human friendly encoded random string
76
76
  */
77
- export function randomStringBaseHuman( length=48 )
78
- {
79
- return randomString( length, ALPHABET_BASE_HUMAN );
77
+ export function randomStringBaseHuman(length = 48) {
78
+ return randomString(length, ALPHABET_BASE_HUMAN);
80
79
  }
81
80
 
82
81
  /**
@@ -87,15 +86,12 @@ export function randomStringBaseHuman( length=48 )
87
86
  *
88
87
  * @returns {string} a base 58 encoded random string
89
88
  */
90
- export function randomString( length=48, ALPHABET=ALPHABET_BASE_58 )
91
- {
92
- if( typeof length !== 'number' || length < 1 )
93
- {
89
+ export function randomString(length = 48, ALPHABET = ALPHABET_BASE_58) {
90
+ if (typeof length !== 'number' || length < 1) {
94
91
  throw new Error('Invalid parameter [length]');
95
92
  }
96
93
 
97
- if( typeof ALPHABET !== 'string' || !ALPHABET.length )
98
- {
94
+ if (typeof ALPHABET !== 'string' || !ALPHABET.length) {
99
95
  throw new Error('Invalid parameter [ALPHABET]');
100
96
  }
101
97
 
@@ -103,10 +99,9 @@ export function randomString( length=48, ALPHABET=ALPHABET_BASE_58 )
103
99
 
104
100
  const n = ALPHABET.length;
105
101
 
106
- for( let j = length; j > 0; j = j - 1 )
107
- {
108
- const num = n * Math.random() & -1; // number [0...n-1]
109
- str += ALPHABET[ num ];
102
+ for (let j = length; j > 0; j = j - 1) {
103
+ const num = (n * Math.random()) & -1; // number [0...n-1]
104
+ str += ALPHABET[num];
110
105
  }
111
106
 
112
107
  return str;
@@ -118,9 +113,8 @@ export function randomString( length=48, ALPHABET=ALPHABET_BASE_58 )
118
113
  *
119
114
  * @returns {string} a base 58 encoded random string of length 48
120
115
  */
121
- export function randomAccessCode()
122
- {
123
- return randomStringBase58( 48 );
116
+ export function randomAccessCode() {
117
+ return randomStringBase58(48);
124
118
  }
125
119
 
126
120
  /**
@@ -128,9 +122,8 @@ export function randomAccessCode()
128
122
  *
129
123
  * @returns {string} a base 58 encoded random string of length 48
130
124
  */
131
- export function generateClientSessionId()
132
- {
133
- return randomStringBase58( 48 );
125
+ export function generateClientSessionId() {
126
+ return randomStringBase58(48);
134
127
  }
135
128
 
136
129
  /**
@@ -143,41 +136,37 @@ export function generateClientSessionId()
143
136
  *
144
137
  * @returns {string} local id
145
138
  */
146
- export function generateLocalId( timeMs )
147
- {
148
- const timeBasedNumber = getTimeBasedNumber30s( timeMs );
139
+ export function generateLocalId(timeMs) {
140
+ const timeBasedNumber = getTimeBasedNumber30s(timeMs);
149
141
 
150
142
  let timeBasedValue58;
151
143
 
152
144
  let countBasedNumber;
153
145
 
154
- if( vars.lastTimeBasedNumber !== timeBasedNumber )
155
- {
146
+ if (vars.lastTimeBasedNumber !== timeBasedNumber) {
156
147
  // -- Time stamp based number changed -> reset counter to zero
157
148
 
158
- countBasedNumber =
159
- vars.lastCountBasedNumber = 0;
149
+ countBasedNumber = vars.lastCountBasedNumber = 0;
160
150
 
161
151
  // -- Calculate timeBasedValue58 and update cache
162
152
 
163
153
  vars.lastTimeBasedNumber = timeBasedNumber;
164
154
 
165
155
  // cache string representation
166
- timeBasedValue58 =
167
- vars.lastTimeBasedValue58 = base58fromNumber( timeBasedNumber );
168
- }
169
- else {
156
+ timeBasedValue58 = vars.lastTimeBasedValue58 =
157
+ base58fromNumber(timeBasedNumber);
158
+ } else {
170
159
  // -- Same time stamp based number -> increment counter
171
160
 
172
- countBasedNumber =
173
- vars.lastCountBasedNumber = vars.lastCountBasedNumber + 1;
161
+ countBasedNumber = vars.lastCountBasedNumber =
162
+ vars.lastCountBasedNumber + 1;
174
163
 
175
164
  // -- Use cached lastTimeBasedNumber
176
165
 
177
166
  timeBasedValue58 = vars.lastTimeBasedValue58;
178
167
  }
179
168
 
180
- const countBasedValue58 = base58fromNumber( countBasedNumber );
169
+ const countBasedValue58 = base58fromNumber(countBasedNumber);
181
170
 
182
171
  // Combine parts into single identifier string
183
172
  //
@@ -187,7 +176,7 @@ export function generateLocalId( timeMs )
187
176
  const id =
188
177
  // idFormatPrefix
189
178
  bootTimePrefix() +
190
- ALPHABET_BASE_58[ timeBasedValue58.length ] +
179
+ ALPHABET_BASE_58[timeBasedValue58.length] +
191
180
  timeBasedValue58 +
192
181
  countBasedValue58;
193
182
 
@@ -196,7 +185,6 @@ export function generateLocalId( timeMs )
196
185
  return id;
197
186
  }
198
187
 
199
-
200
188
  /**
201
189
  * Returns a time based number that changes every 30 seconds
202
190
  *
@@ -205,18 +193,15 @@ export function generateLocalId( timeMs )
205
193
  *
206
194
  * @returns {number} time based numerical that changes every 30 seconds
207
195
  */
208
- export function getTimeBasedNumber30s( timeMs )
209
- {
210
- if( !timeMs )
211
- {
196
+ export function getTimeBasedNumber30s(timeMs) {
197
+ if (!timeMs) {
212
198
  timeMs = sinceMs();
213
199
  }
214
200
 
215
201
  // @note do not use bitwise shift since it only works on 32 bit numbers!
216
- return Math.floor( timeMs / 30000 );
202
+ return Math.floor(timeMs / 30000);
217
203
  }
218
204
 
219
-
220
205
  /**
221
206
  * Returns two character base58 encoded string that changes every 10
222
207
  * milliseconds
@@ -231,19 +216,16 @@ export function getTimeBasedNumber30s( timeMs )
231
216
  *
232
217
  * @returns {string} time based value
233
218
  */
234
- export function getTwoChar10ms( timeMs )
235
- {
219
+ export function getTwoChar10ms(timeMs) {
236
220
  const now = timeMs || Date.now();
237
221
 
238
222
  // @note
239
223
  // do not use bitwise shift since it only works on 32 bit numbers
240
- const num = Math.floor( now / 10 ) % 3364;
224
+ const num = Math.floor(now / 10) % 3364;
241
225
 
242
- if( num >= 58 )
243
- {
244
- return base58fromNumber( num );
245
- }
246
- else {
247
- return '1' + base58fromNumber( num );
226
+ if (num >= 58) {
227
+ return base58fromNumber(num);
228
+ } else {
229
+ return '1' + base58fromNumber(num);
248
230
  }
249
231
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"