@nemigo/helpers 2.2.1 → 2.2.2

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/veil.d.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import type { Serializable } from "./types.js";
2
+ /**
3
+ * Декодирует строку, закодированную функцией {@link veil} в сериализованное значение
4
+ */
5
+ export declare const __unveil: (veiled: string) => string;
2
6
  /**
3
7
  * Декодирует строку, закодированную функцией {@link veil}, в исходное значение и десериализует
4
8
  *
5
9
  * @remarks Для десериализации используется {@link JSON.parse}
6
10
  */
7
11
  export declare const unveil: <T = string>(veiled: string) => T;
12
+ /**
13
+ * Кодирует сериализованное в URL-safe base64-строку
14
+ */
15
+ export declare const __veil: (data: string) => string;
8
16
  /**
9
17
  * Кодирует serializable-значение в URL-safe base64-строку
10
18
  *
package/dist/veil.js CHANGED
@@ -1,26 +1,34 @@
1
- const encoder = new TextEncoder();
2
1
  const decoder = new TextDecoder();
3
2
  /**
4
- * Декодирует строку, закодированную функцией {@link veil}, в исходное значение и десериализует
5
- *
6
- * @remarks Для десериализации используется {@link JSON.parse}
3
+ * Декодирует строку, закодированную функцией {@link veil} в сериализованное значение
7
4
  */
8
- export const unveil = (veiled) => {
5
+ export const __unveil = (veiled) => {
9
6
  const decoded = atob(decodeURIComponent(veiled));
10
7
  const bytes = new Uint8Array(decoded.length);
11
8
  for (let i = 0; i < decoded.length; i++)
12
9
  bytes[i] = decoded.codePointAt(i);
13
- return JSON.parse(decoder.decode(bytes));
10
+ return decoder.decode(bytes);
14
11
  };
15
12
  /**
16
- * Кодирует serializable-значение в URL-safe base64-строку
13
+ * Декодирует строку, закодированную функцией {@link veil}, в исходное значение и десериализует
17
14
  *
18
- * @remarks Для сериализации используется {@link JSON.stringify}
19
- * @remarks Поддерживает полный Unicode, включая смайлики и символы вне BMP
15
+ * @remarks Для десериализации используется {@link JSON.parse}
20
16
  */
21
- export const veil = (snap) => {
22
- const str = JSON.stringify(snap);
23
- const bytes = encoder.encode(str);
17
+ export const unveil = (veiled) => JSON.parse(__unveil(veiled));
18
+ //...
19
+ const encoder = new TextEncoder();
20
+ /**
21
+ * Кодирует сериализованное в URL-safe base64-строку
22
+ */
23
+ export const __veil = (data) => {
24
+ const bytes = encoder.encode(data);
24
25
  const binary = String.fromCodePoint(...bytes);
25
26
  return encodeURIComponent(btoa(binary));
26
27
  };
28
+ /**
29
+ * Кодирует serializable-значение в URL-safe base64-строку
30
+ *
31
+ * @remarks Для сериализации используется {@link JSON.stringify}
32
+ * @remarks Поддерживает полный Unicode, включая смайлики и символы вне BMP
33
+ */
34
+ export const veil = (snap) => __veil(JSON.stringify(snap));
package/dist/zipper.d.ts CHANGED
@@ -6,14 +6,18 @@ export type ZIP = string;
6
6
  /**
7
7
  * Предварительно вычисленный массив из 256 32-битных значений полинома 0xEDB88320 (стандартный для {@link http://create.stephan-brumme.com/crc32 CRC32})
8
8
  */
9
- export declare const polynomial: Uint32Array<ArrayBuffer>;
9
+ export declare const __polynomial: Uint32Array<ArrayBuffer>;
10
+ /**
11
+ * Генерирует хэш на основе алгоритма {@link http://create.stephan-brumme.com/crc32 CRC32}
12
+ */
13
+ export declare const __zipper: (data: string) => string;
10
14
  /**
11
15
  * Генерирует хэш в формате `${prefix}{hash}` на основе алгоритма {@link http://create.stephan-brumme.com/crc32 CRC32}
12
16
  *
13
17
  * @remarks Для сериализации используется {@link JSON.stringify}
14
18
  * @remarks Порядок свойств в объектах **влияет** на результат
15
19
  *
16
- * @param data - Данные для хэширования
20
+ * @param [data="undefined"] - Данные для хэширования
17
21
  * @param [prefix="zip:"] - Префикс для идентификации хэша
18
22
  *
19
23
  * @example
@@ -27,4 +31,4 @@ export declare const polynomial: Uint32Array<ArrayBuffer>;
27
31
  * const hash3 = zipper(obj3); // "zip:6b9g4d3f" (другой хэш)
28
32
  * ```
29
33
  */
30
- export declare const zipper: (data: Serializable, prefix?: string) => ZIP;
34
+ export declare const zipper: (data?: Serializable, prefix?: string) => ZIP;
package/dist/zipper.js CHANGED
@@ -1,19 +1,30 @@
1
1
  /**
2
2
  * Предварительно вычисленный массив из 256 32-битных значений полинома 0xEDB88320 (стандартный для {@link http://create.stephan-brumme.com/crc32 CRC32})
3
3
  */
4
- export const polynomial = Uint32Array.from({ length: 256 }, (_, i) => {
4
+ export const __polynomial = Uint32Array.from({ length: 256 }, (_, i) => {
5
5
  let crc = i;
6
6
  for (let j = 8; j--;)
7
7
  crc = (crc >>> 1) ^ (0xed_b8_83_20 & -(crc & 1));
8
8
  return crc;
9
9
  });
10
+ /**
11
+ * Генерирует хэш на основе алгоритма {@link http://create.stephan-brumme.com/crc32 CRC32}
12
+ */
13
+ export const __zipper = (data) => {
14
+ let crc = 0 ^ -1;
15
+ for (let i = data.length - 1; i >= 0; i--)
16
+ crc = (crc >>> 8) ^ __polynomial[(crc ^ data.codePointAt(i)) & 0xff];
17
+ return Math.trunc(crc ^ -1)
18
+ .toString(16)
19
+ .padStart(8, "0");
20
+ };
10
21
  /**
11
22
  * Генерирует хэш в формате `${prefix}{hash}` на основе алгоритма {@link http://create.stephan-brumme.com/crc32 CRC32}
12
23
  *
13
24
  * @remarks Для сериализации используется {@link JSON.stringify}
14
25
  * @remarks Порядок свойств в объектах **влияет** на результат
15
26
  *
16
- * @param data - Данные для хэширования
27
+ * @param [data="undefined"] - Данные для хэширования
17
28
  * @param [prefix="zip:"] - Префикс для идентификации хэша
18
29
  *
19
30
  * @example
@@ -27,13 +38,4 @@ export const polynomial = Uint32Array.from({ length: 256 }, (_, i) => {
27
38
  * const hash3 = zipper(obj3); // "zip:6b9g4d3f" (другой хэш)
28
39
  * ```
29
40
  */
30
- export const zipper = (data, prefix = "zip:") => {
31
- const str = JSON.stringify(data);
32
- let crc = 0 ^ -1;
33
- for (let i = str.length - 1; i >= 0; i--)
34
- crc = (crc >>> 8) ^ polynomial[(crc ^ str.codePointAt(i)) & 0xff];
35
- const hash = Math.trunc(crc ^ -1)
36
- .toString(16)
37
- .padStart(8, "0");
38
- return prefix + hash;
39
- };
41
+ export const zipper = (data = "undefined", prefix = "zip:") => prefix + __zipper(JSON.stringify(data));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/helpers",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "Vlad Logvin",