@ibiliaze/stringman 3.11.0 → 3.13.0

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/dist/index.d.ts CHANGED
@@ -158,7 +158,7 @@ export declare const ticket: {
158
158
  dashed?: boolean;
159
159
  }) => string;
160
160
  validateTicketCode: (code: string) => boolean;
161
- rndBlock: any;
162
- fixtureBlock: any;
163
- seatBlock: any;
161
+ };
162
+ export declare const order: {
163
+ createNumericOrderId: () => string;
164
164
  };
package/dist/index.js CHANGED
@@ -14,9 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.ticket = exports.luhn36 = exports.b36 = exports.invalidPw = exports.getPublicIP = exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
17
+ exports.order = exports.ticket = exports.luhn36 = exports.b36 = exports.invalidPw = exports.getPublicIP = exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
18
18
  __exportStar(require("./seat"), exports);
19
19
  const ticket_1 = require("./ticket");
20
+ const order_1 = require("./order");
20
21
  /**
21
22
  * Clean up extra whitespace in a string.
22
23
  *
@@ -305,7 +306,5 @@ exports.ticket = {
305
306
  getTicketId: ticket_1.getTicketId,
306
307
  buildTicketCode: ticket_1.buildTicketCode,
307
308
  validateTicketCode: ticket_1.validateTicketCode,
308
- rndBlock: ticket_1.rndBlock,
309
- fixtureBlock: ticket_1.fixtureBlock,
310
- seatBlock: ticket_1.seatBlock,
311
309
  };
310
+ exports.order = { createNumericOrderId: order_1.createNumericOrderId };
@@ -0,0 +1 @@
1
+ export declare const createNumericOrderId: () => string;
package/dist/order.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ // src/orderId-numeric.ts
3
+ // 20 digits: <unixMs(13)><counter(4)><random(3)>
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.createNumericOrderId = void 0;
6
+ let _lastMs = 0;
7
+ let _ctr = 0;
8
+ const cryptoObj = globalThis.crypto;
9
+ const randomDigits = (n) => {
10
+ if (!cryptoObj?.getRandomValues) {
11
+ throw new Error('Secure RNG not available. Use Node 18+ (globalThis.crypto) or a browser with Web Crypto.');
12
+ }
13
+ const out = [];
14
+ const b = new Uint8Array(1);
15
+ while (out.length < n) {
16
+ cryptoObj.getRandomValues(b);
17
+ const x = b[0];
18
+ if (x < 250)
19
+ out.push(String(x % 10)); // rejection sampling avoids modulo bias
20
+ }
21
+ return out.join('');
22
+ };
23
+ const createNumericOrderId = () => {
24
+ const now = Date.now(); // 13 digits
25
+ if (now === _lastMs) {
26
+ _ctr++;
27
+ if (_ctr > 9999) {
28
+ // non-blocking: bump ms instead of busy-wait
29
+ _lastMs = now + 1;
30
+ _ctr = 0;
31
+ }
32
+ }
33
+ else {
34
+ _lastMs = now;
35
+ _ctr = 0;
36
+ }
37
+ const timePart = String(_lastMs);
38
+ const ctrPart = String(_ctr).padStart(4, '0');
39
+ const randPart = randomDigits(3);
40
+ return timePart + ctrPart + randPart; // 20 digits
41
+ };
42
+ exports.createNumericOrderId = createNumericOrderId;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ibiliaze/stringman",
3
- "version": "3.11.0",
3
+ "version": "3.13.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "pub": "npm publish --access public",
10
- "git": "git add .; git commit -m 'changes'; git tag -a v3.11.0 -m 'v3.11.0'; git push origin v3.11.0; git push",
10
+ "git": "git add .; git commit -m 'changes'; git tag -a v3.13.0 -m 'v3.13.0'; git push origin v3.13.0; git push",
11
11
  "push": "npm run build; npm run git; npm run pub"
12
12
  },
13
13
  "author": "Ibi Hasanli",
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './seat';
2
- import { buildTicketCode, fixtureBlock, getTicketId, rndBlock, seatBlock, validateTicketCode } from './ticket';
2
+ import { buildTicketCode, getTicketId, validateTicketCode } from './ticket';
3
+ import { createNumericOrderId } from './order';
3
4
 
4
5
  /**
5
6
  * Clean up extra whitespace in a string.
@@ -281,7 +282,6 @@ export const ticket = {
281
282
  getTicketId,
282
283
  buildTicketCode,
283
284
  validateTicketCode,
284
- rndBlock,
285
- fixtureBlock,
286
- seatBlock,
287
285
  };
286
+
287
+ export const order = { createNumericOrderId };
package/src/order.ts ADDED
@@ -0,0 +1,43 @@
1
+ // src/orderId-numeric.ts
2
+ // 20 digits: <unixMs(13)><counter(4)><random(3)>
3
+
4
+ let _lastMs = 0;
5
+ let _ctr = 0;
6
+
7
+ const cryptoObj = (globalThis as any).crypto as Crypto | undefined;
8
+
9
+ const randomDigits = (n: number): string => {
10
+ if (!cryptoObj?.getRandomValues) {
11
+ throw new Error('Secure RNG not available. Use Node 18+ (globalThis.crypto) or a browser with Web Crypto.');
12
+ }
13
+ const out: string[] = [];
14
+ const b = new Uint8Array(1);
15
+ while (out.length < n) {
16
+ cryptoObj.getRandomValues(b);
17
+ const x = b[0];
18
+ if (x < 250) out.push(String(x % 10)); // rejection sampling avoids modulo bias
19
+ }
20
+ return out.join('');
21
+ };
22
+
23
+ export const createNumericOrderId = (): string => {
24
+ const now = Date.now(); // 13 digits
25
+
26
+ if (now === _lastMs) {
27
+ _ctr++;
28
+ if (_ctr > 9999) {
29
+ // non-blocking: bump ms instead of busy-wait
30
+ _lastMs = now + 1;
31
+ _ctr = 0;
32
+ }
33
+ } else {
34
+ _lastMs = now;
35
+ _ctr = 0;
36
+ }
37
+
38
+ const timePart = String(_lastMs);
39
+ const ctrPart = String(_ctr).padStart(4, '0');
40
+ const randPart = randomDigits(3);
41
+
42
+ return timePart + ctrPart + randPart; // 20 digits
43
+ };