@naturalcycles/nodejs-lib 15.6.0 → 15.7.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.
@@ -1,3 +1,5 @@
1
+ export declare function mockStringId(id: string): void;
2
+ export declare function unmockStringId(): void;
1
3
  /**
2
4
  * Generate cryptographically-secure string id.
3
5
  * Powered by `nanoid`.
@@ -1,11 +1,18 @@
1
1
  import { randomBytes } from 'node:crypto';
2
2
  import { ALPHABET_ALPHANUMERIC, ALPHABET_ALPHANUMERIC_LOWERCASE, ALPHABET_NONAMBIGUOUS, nanoIdCustomAlphabet, } from './nanoid.js';
3
+ let mockedStringId;
4
+ export function mockStringId(id) {
5
+ mockedStringId = id;
6
+ }
7
+ export function unmockStringId() {
8
+ mockedStringId = undefined;
9
+ }
3
10
  /**
4
11
  * Generate cryptographically-secure string id.
5
12
  * Powered by `nanoid`.
6
13
  */
7
14
  export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE) {
8
- return nanoIdCustomAlphabet(alphabet, length)();
15
+ return mockedStringId ?? nanoIdCustomAlphabet(alphabet, length)();
9
16
  }
10
17
  /**
11
18
  * Generate a string id of Base62 alphabet (same as "alphanumeric": A-Za-z0-9)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.6.0",
4
+ "version": "15.7.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -6,12 +6,21 @@ import {
6
6
  nanoIdCustomAlphabet,
7
7
  } from './nanoid.js'
8
8
 
9
+ let mockedStringId: string | undefined
10
+
11
+ export function mockStringId(id: string): void {
12
+ mockedStringId = id
13
+ }
14
+ export function unmockStringId(): void {
15
+ mockedStringId = undefined
16
+ }
17
+
9
18
  /**
10
19
  * Generate cryptographically-secure string id.
11
20
  * Powered by `nanoid`.
12
21
  */
13
22
  export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
14
- return nanoIdCustomAlphabet(alphabet, length)()
23
+ return mockedStringId ?? nanoIdCustomAlphabet(alphabet, length)()
15
24
  }
16
25
 
17
26
  /**