@nemigo/helpers 2.6.0 → 2.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.
@@ -15,7 +15,7 @@ export type RoundMethod = "up" | "down" | "math";
15
15
  * @param [fraction=2] - Количество знаков после запятой
16
16
  * @param [method="math"] - Метод округления
17
17
  */
18
- export declare const round: (value: number, fraction?: number, method?: RoundMethod) => number;
18
+ export declare const round: (value?: number | null, fraction?: number, method?: RoundMethod) => number;
19
19
  /**
20
20
  * Вычисляет сумму всех переданных аргументов
21
21
  *
@@ -14,7 +14,7 @@ export const normalizero = (value) => (value === 0 ? 0 : value);
14
14
  * @param [method="math"] - Метод округления
15
15
  */
16
16
  export const round = (value, fraction = 2, method = "math") => {
17
- if (Number.isNaN(value) || value === 0)
17
+ if (!value)
18
18
  return 0;
19
19
  const factor = 10 ** fraction;
20
20
  switch (method) {
package/dist/random.d.ts CHANGED
@@ -15,21 +15,21 @@ export declare const rand: (length?: number) => string;
15
15
  /**
16
16
  * Генерирует уникальный ID, состоящий из {@link Timestamp} и случайного кода из {@link rand}
17
17
  *
18
- * Формат: `${Date.now()}:${rand(length)}`
18
+ * Формат: `${Date.now()}_${rand(length)}`
19
19
  *
20
20
  * @param [length=7] - Длина случайной части кода
21
21
  *
22
22
  * @example
23
23
  * ```typescript
24
- * randID(4); // "1696934400000:1234"
25
- * randID(6); // "1696934400000:123456"
24
+ * randID(4); // "1696934400000_1234"
25
+ * randID(6); // "1696934400000_123456"
26
26
  * ```
27
27
  */
28
28
  export declare const randID: (length?: number) => string;
29
29
  /**
30
30
  * Генерирует случайный UID из латиницы в нижнем регистре и цифр
31
31
  *
32
- * Формат: `prefix ? `${prefix}:${uid}` : uid`
32
+ * Формат: `prefix ? `${prefix}${uid}` : uid`
33
33
  *
34
34
  * @param [prefix] - Префикс для категоризации UID (например таблица)
35
35
  * @param [length=20] - Длина UID в символах
@@ -37,8 +37,12 @@ export declare const randID: (length?: number) => string;
37
37
  * @example
38
38
  * ```typescript
39
39
  * randUID(); // "a1b2c3d4e5f6g7h8i9j0"
40
- * randUID("user", 10); // "user:a1b2c3d4e5"
41
- * randUID("session", 16); // "session:a1b2c3d4e5f6g7h8"
40
+ * randUID("user:", 10); // "user:a1b2c3d4e5"
41
+ * randUID("session:", 16); // "session:a1b2c3d4e5f6g7h8"
42
42
  * ```
43
43
  */
44
44
  export declare const randUID: (prefix?: string, length?: number) => string;
45
+ /**
46
+ * Генерирует случайный UUID версии 4 (RFC 4122). Использует {@link Math.random()}
47
+ */
48
+ export declare const randUUID: () => string;
package/dist/random.js CHANGED
@@ -18,21 +18,21 @@ export const rand = (length = 6) => {
18
18
  /**
19
19
  * Генерирует уникальный ID, состоящий из {@link Timestamp} и случайного кода из {@link rand}
20
20
  *
21
- * Формат: `${Date.now()}:${rand(length)}`
21
+ * Формат: `${Date.now()}_${rand(length)}`
22
22
  *
23
23
  * @param [length=7] - Длина случайной части кода
24
24
  *
25
25
  * @example
26
26
  * ```typescript
27
- * randID(4); // "1696934400000:1234"
28
- * randID(6); // "1696934400000:123456"
27
+ * randID(4); // "1696934400000_1234"
28
+ * randID(6); // "1696934400000_123456"
29
29
  * ```
30
30
  */
31
- export const randID = (length = 7) => `${Date.now()}:${rand(length)}`;
31
+ export const randID = (length = 7) => `${Date.now()}_${rand(length)}`;
32
32
  /**
33
33
  * Генерирует случайный UID из латиницы в нижнем регистре и цифр
34
34
  *
35
- * Формат: `prefix ? `${prefix}:${uid}` : uid`
35
+ * Формат: `prefix ? `${prefix}${uid}` : uid`
36
36
  *
37
37
  * @param [prefix] - Префикс для категоризации UID (например таблица)
38
38
  * @param [length=20] - Длина UID в символах
@@ -40,14 +40,41 @@ export const randID = (length = 7) => `${Date.now()}:${rand(length)}`;
40
40
  * @example
41
41
  * ```typescript
42
42
  * randUID(); // "a1b2c3d4e5f6g7h8i9j0"
43
- * randUID("user", 10); // "user:a1b2c3d4e5"
44
- * randUID("session", 16); // "session:a1b2c3d4e5f6g7h8"
43
+ * randUID("user:", 10); // "user:a1b2c3d4e5"
44
+ * randUID("session:", 16); // "session:a1b2c3d4e5f6g7h8"
45
45
  * ```
46
46
  */
47
47
  export const randUID = (prefix, length = 20) => {
48
- const UID = "abcdefghijklmnopqrstuvwxyz0123456789";
48
+ const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
49
49
  let uid = "";
50
50
  for (let i = 0; i < length; i++)
51
- uid += UID[Math.floor(Math.random() * 36)];
52
- return prefix ? `${prefix}:${uid}` : uid;
51
+ uid += chars[Math.floor(Math.random() * 36)];
52
+ return prefix ? `${prefix}${uid}` : uid;
53
+ };
54
+ /**
55
+ * Генерирует случайный UUID версии 4 (RFC 4122). Использует {@link Math.random()}
56
+ */
57
+ export const randUUID = () => {
58
+ const chars = "0123456789abcdef";
59
+ let uuid = "";
60
+ for (let i = 0; i < 36; i++) {
61
+ switch (i) {
62
+ case 8:
63
+ case 13:
64
+ case 18:
65
+ case 23:
66
+ uuid += "-";
67
+ break;
68
+ case 14:
69
+ uuid += "4"; // версия UUID — 4
70
+ break;
71
+ case 19:
72
+ // Устанавливаем старшие два бита в 10xx (RFC 4122 variant)
73
+ uuid += chars[(Math.floor(Math.random() * 16) & 0x3) | 0x8];
74
+ break;
75
+ default:
76
+ uuid += chars[Math.floor(Math.random() * 16)];
77
+ }
78
+ }
79
+ return uuid;
53
80
  };
package/dist/script.d.ts CHANGED
@@ -38,15 +38,23 @@ export interface ExecuteLogger {
38
38
  /**
39
39
  * Перед выполнением
40
40
  */
41
- before?: (path: string) => string;
41
+ before?: (path: string) => void;
42
+ /**
43
+ * `stderr` поток вывода процесса
44
+ */
45
+ stderr?: (message: string) => void;
46
+ /**
47
+ * `stdout` поток вывода процесса
48
+ */
49
+ stdout?: (message: string) => void;
42
50
  /**
43
51
  * При возникновении ошибки
44
52
  */
45
- onerror?: (error: ExecException) => string;
53
+ onerror?: (error: ExecException) => void;
46
54
  /**
47
55
  * При истечении таймаута
48
56
  */
49
- ontimeout?: () => string;
57
+ ontimeout?: () => void;
50
58
  }
51
59
  /**
52
60
  * Опции для {@link execute}
package/dist/script.js CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-console */
2
1
  import { exec } from "node:child_process";
3
2
  import { MS_IN_MINUTE } from "./datetime/delta.js";
4
3
  /**
@@ -61,8 +60,7 @@ export { getProcessFlag };
61
60
  * ```
62
61
  */
63
62
  export const execute = (path, command, { logger = {}, timeout = 5 * MS_IN_MINUTE } = {}) => new Promise((resolve, reject) => {
64
- if (logger.before)
65
- console.log(logger.before(path));
63
+ logger.before?.(path);
66
64
  let process = null;
67
65
  let timer = null;
68
66
  const cleanup = () => {
@@ -74,20 +72,18 @@ export const execute = (path, command, { logger = {}, timeout = 5 * MS_IN_MINUTE
74
72
  if (timeout) {
75
73
  timer = setTimeout(() => {
76
74
  process?.kill("SIGTERM");
77
- if (logger.ontimeout)
78
- console.error(logger.ontimeout());
75
+ logger.ontimeout?.();
79
76
  reject(new Error("TIMEOUT"));
80
77
  }, timeout);
81
78
  }
82
79
  process = exec(command, { cwd: path }, (error, stdout, stderr) => {
83
80
  cleanup();
84
81
  if (stderr)
85
- console.warn(stderr);
82
+ logger.stderr?.(stderr);
86
83
  if (stdout)
87
- console.log(stdout);
84
+ logger.stdout?.(stdout);
88
85
  if (error) {
89
- if (logger.onerror)
90
- console.error(logger.onerror(error));
86
+ logger.onerror?.(error);
91
87
  reject(error);
92
88
  return;
93
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/helpers",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "private": false,
5
5
  "license": "MPL-2.0",
6
6
  "author": {
@@ -192,6 +192,6 @@
192
192
  "@std/msgpack": "npm:@jsr/std__msgpack"
193
193
  },
194
194
  "devDependencies": {
195
- "@nemigo/configs": "2.6.0"
195
+ "@nemigo/configs": "2.6.1"
196
196
  }
197
197
  }