@naturalcycles/js-lib 14.149.3 → 14.150.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
@@ -78,6 +78,7 @@ export * from './env';
78
78
  export * from './http/http.model';
79
79
  export * from './http/fetcher';
80
80
  export * from './http/fetcher.model';
81
+ export * from './string/hash.util';
81
82
  export * from './zod/zod.util';
82
83
  export * from './zod/zod.shared.schemas';
83
84
  import { z, ZodSchema, ZodError, ZodIssue } from 'zod';
package/dist/index.js CHANGED
@@ -82,6 +82,7 @@ tslib_1.__exportStar(require("./env"), exports);
82
82
  tslib_1.__exportStar(require("./http/http.model"), exports);
83
83
  tslib_1.__exportStar(require("./http/fetcher"), exports);
84
84
  tslib_1.__exportStar(require("./http/fetcher.model"), exports);
85
+ tslib_1.__exportStar(require("./string/hash.util"), exports);
85
86
  tslib_1.__exportStar(require("./zod/zod.util"), exports);
86
87
  tslib_1.__exportStar(require("./zod/zod.shared.schemas"), exports);
87
88
  const zod_1 = require("zod");
@@ -44,7 +44,7 @@ export declare function _filterObject<T extends AnyObject>(obj: T, predicate: Ob
44
44
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
45
45
  * }
46
46
  *
47
- * _.mapValues(users, function(o) { return o.age; });
47
+ * _.mapValues(users, function(_key, value) { return value.age; });
48
48
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
49
49
  *
50
50
  * // The `_.property` iteratee shorthand.
@@ -93,7 +93,7 @@ exports._filterObject = _filterObject;
93
93
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
94
94
  * }
95
95
  *
96
- * _.mapValues(users, function(o) { return o.age; });
96
+ * _.mapValues(users, function(_key, value) { return value.age; });
97
97
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
98
98
  *
99
99
  * // The `_.property` iteratee shorthand.
@@ -0,0 +1,38 @@
1
+ import { Integer } from '../types';
2
+ /**
3
+ * Returns hashCode as hex (radix 16).
4
+ *
5
+ * All hash functions here are optimized for:
6
+ *
7
+ * 1. Performance
8
+ * 2. For non-cryptographic use (where accidental collision is not the end-of-the-world)
9
+ * 3. Compact size (32 bits max, versus 128 in md5; presented in less string json-safe characters)
10
+ *
11
+ * Basically, these functions are as simple as they can be, but still "random enough" for
12
+ * normal non-cryptographic use cases.
13
+ *
14
+ * To be run on the ClientSide, as in Node there are plenty of other hash options in `node:crypto`.
15
+ *
16
+ * Perf: it runs ~10 times faster than CryptoJS md5, and ~3 times smaller String hash size (for
17
+ * hashCode64).
18
+ */
19
+ export declare function hashCode16(s: string): string;
20
+ /**
21
+ * Returns hashCode as "radix 36", using "Base36 alphabet".
22
+ * 36 is used, because it's the maximum "radix" that Number.toString() can take.
23
+ *
24
+ * See the hashCode16 for full description.
25
+ */
26
+ export declare function hashCode36(s: string): string;
27
+ /**
28
+ * Returns hashCode as "radix 64", using Base64url alphabet.
29
+ * See the hashCode16 for full description.
30
+ */
31
+ export declare function hashCode64(s: string): string;
32
+ /**
33
+ * Generates a stable integer hashCode for a given String.
34
+ * Matches Java implementation (they say), except it ensures a positive Integer
35
+ * by adding 2147483647 + 1 to the end result.
36
+ * Source: https://stackoverflow.com/a/33647870/4919972
37
+ */
38
+ export declare function hashCode(s: string): Integer;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hashCode = exports.hashCode64 = exports.hashCode36 = exports.hashCode16 = void 0;
4
+ const BASE62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
5
+ // const BASE64 = BASE62 + '+/'
6
+ const BASE64URL = BASE62 + '-_';
7
+ /**
8
+ * Returns hashCode as hex (radix 16).
9
+ *
10
+ * All hash functions here are optimized for:
11
+ *
12
+ * 1. Performance
13
+ * 2. For non-cryptographic use (where accidental collision is not the end-of-the-world)
14
+ * 3. Compact size (32 bits max, versus 128 in md5; presented in less string json-safe characters)
15
+ *
16
+ * Basically, these functions are as simple as they can be, but still "random enough" for
17
+ * normal non-cryptographic use cases.
18
+ *
19
+ * To be run on the ClientSide, as in Node there are plenty of other hash options in `node:crypto`.
20
+ *
21
+ * Perf: it runs ~10 times faster than CryptoJS md5, and ~3 times smaller String hash size (for
22
+ * hashCode64).
23
+ */
24
+ function hashCode16(s) {
25
+ return hashCode(s).toString(16);
26
+ }
27
+ exports.hashCode16 = hashCode16;
28
+ /**
29
+ * Returns hashCode as "radix 36", using "Base36 alphabet".
30
+ * 36 is used, because it's the maximum "radix" that Number.toString() can take.
31
+ *
32
+ * See the hashCode16 for full description.
33
+ */
34
+ function hashCode36(s) {
35
+ return hashCode(s).toString(36);
36
+ }
37
+ exports.hashCode36 = hashCode36;
38
+ /**
39
+ * Returns hashCode as "radix 64", using Base64url alphabet.
40
+ * See the hashCode16 for full description.
41
+ */
42
+ function hashCode64(s) {
43
+ return numberToBase(hashCode(s), BASE64URL);
44
+ }
45
+ exports.hashCode64 = hashCode64;
46
+ /**
47
+ * Generates a stable integer hashCode for a given String.
48
+ * Matches Java implementation (they say), except it ensures a positive Integer
49
+ * by adding 2147483647 + 1 to the end result.
50
+ * Source: https://stackoverflow.com/a/33647870/4919972
51
+ */
52
+ function hashCode(s) {
53
+ let hash = 0;
54
+ let i = 0;
55
+ const len = s.length;
56
+ while (i < len) {
57
+ // eslint-disable-next-line no-bitwise, unicorn/prefer-math-trunc, unicorn/prefer-code-point
58
+ hash = ((hash << 5) - hash + s.charCodeAt(i++)) << 0;
59
+ }
60
+ return hash + 2147483647 + 1;
61
+ }
62
+ exports.hashCode = hashCode;
63
+ /**
64
+ * Source: https://gist.github.com/alkaruno/b84162bae5115f4ca99b
65
+ */
66
+ function numberToBase(n, alphabet) {
67
+ const alen = alphabet.length;
68
+ let result = '';
69
+ do {
70
+ result = alphabet.charAt(n % alen) + result;
71
+ n = Math.floor(n / alen) - 1;
72
+ } while (n > -1);
73
+ return result;
74
+ }
package/dist-esm/index.js CHANGED
@@ -78,6 +78,7 @@ export * from './env';
78
78
  export * from './http/http.model';
79
79
  export * from './http/fetcher';
80
80
  export * from './http/fetcher.model';
81
+ export * from './string/hash.util';
81
82
  export * from './zod/zod.util';
82
83
  export * from './zod/zod.shared.schemas';
83
84
  import { z, ZodSchema, ZodError } from 'zod';
@@ -82,7 +82,7 @@ export function _filterObject(obj, predicate, mutate = false) {
82
82
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
83
83
  * }
84
84
  *
85
- * _.mapValues(users, function(o) { return o.age; });
85
+ * _.mapValues(users, function(_key, value) { return value.age; });
86
86
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
87
87
  *
88
88
  * // The `_.property` iteratee shorthand.
@@ -0,0 +1,67 @@
1
+ const BASE62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
2
+ // const BASE64 = BASE62 + '+/'
3
+ const BASE64URL = BASE62 + '-_';
4
+ /**
5
+ * Returns hashCode as hex (radix 16).
6
+ *
7
+ * All hash functions here are optimized for:
8
+ *
9
+ * 1. Performance
10
+ * 2. For non-cryptographic use (where accidental collision is not the end-of-the-world)
11
+ * 3. Compact size (32 bits max, versus 128 in md5; presented in less string json-safe characters)
12
+ *
13
+ * Basically, these functions are as simple as they can be, but still "random enough" for
14
+ * normal non-cryptographic use cases.
15
+ *
16
+ * To be run on the ClientSide, as in Node there are plenty of other hash options in `node:crypto`.
17
+ *
18
+ * Perf: it runs ~10 times faster than CryptoJS md5, and ~3 times smaller String hash size (for
19
+ * hashCode64).
20
+ */
21
+ export function hashCode16(s) {
22
+ return hashCode(s).toString(16);
23
+ }
24
+ /**
25
+ * Returns hashCode as "radix 36", using "Base36 alphabet".
26
+ * 36 is used, because it's the maximum "radix" that Number.toString() can take.
27
+ *
28
+ * See the hashCode16 for full description.
29
+ */
30
+ export function hashCode36(s) {
31
+ return hashCode(s).toString(36);
32
+ }
33
+ /**
34
+ * Returns hashCode as "radix 64", using Base64url alphabet.
35
+ * See the hashCode16 for full description.
36
+ */
37
+ export function hashCode64(s) {
38
+ return numberToBase(hashCode(s), BASE64URL);
39
+ }
40
+ /**
41
+ * Generates a stable integer hashCode for a given String.
42
+ * Matches Java implementation (they say), except it ensures a positive Integer
43
+ * by adding 2147483647 + 1 to the end result.
44
+ * Source: https://stackoverflow.com/a/33647870/4919972
45
+ */
46
+ export function hashCode(s) {
47
+ let hash = 0;
48
+ let i = 0;
49
+ const len = s.length;
50
+ while (i < len) {
51
+ // eslint-disable-next-line no-bitwise, unicorn/prefer-math-trunc, unicorn/prefer-code-point
52
+ hash = ((hash << 5) - hash + s.charCodeAt(i++)) << 0;
53
+ }
54
+ return hash + 2147483647 + 1;
55
+ }
56
+ /**
57
+ * Source: https://gist.github.com/alkaruno/b84162bae5115f4ca99b
58
+ */
59
+ function numberToBase(n, alphabet) {
60
+ const alen = alphabet.length;
61
+ let result = '';
62
+ do {
63
+ result = alphabet.charAt(n % alen) + result;
64
+ n = Math.floor(n / alen) - 1;
65
+ } while (n > -1);
66
+ return result;
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.149.3",
3
+ "version": "14.150.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -16,7 +16,9 @@
16
16
  "@naturalcycles/dev-lib": "^13.0.1",
17
17
  "@naturalcycles/nodejs-lib": "^12.33.4",
18
18
  "@naturalcycles/time-lib": "^3.5.1",
19
+ "@types/crypto-js": "^4.1.1",
19
20
  "@types/node": "^20.1.0",
21
+ "crypto-js": "^4.1.1",
20
22
  "jest": "^29.0.0",
21
23
  "prettier": "^2.1.2",
22
24
  "rxjs": "^7.0.1",
package/src/index.ts CHANGED
@@ -78,6 +78,7 @@ export * from './env'
78
78
  export * from './http/http.model'
79
79
  export * from './http/fetcher'
80
80
  export * from './http/fetcher.model'
81
+ export * from './string/hash.util'
81
82
  export * from './zod/zod.util'
82
83
  export * from './zod/zod.shared.schemas'
83
84
  import { z, ZodSchema, ZodError, ZodIssue } from 'zod'
@@ -111,7 +111,7 @@ export function _filterObject<T extends AnyObject>(
111
111
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
112
112
  * }
113
113
  *
114
- * _.mapValues(users, function(o) { return o.age; });
114
+ * _.mapValues(users, function(_key, value) { return value.age; });
115
115
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
116
116
  *
117
117
  * // The `_.property` iteratee shorthand.
@@ -0,0 +1,76 @@
1
+ import { Integer } from '../types'
2
+
3
+ const BASE62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
4
+ // const BASE64 = BASE62 + '+/'
5
+ const BASE64URL = BASE62 + '-_'
6
+
7
+ /**
8
+ * Returns hashCode as hex (radix 16).
9
+ *
10
+ * All hash functions here are optimized for:
11
+ *
12
+ * 1. Performance
13
+ * 2. For non-cryptographic use (where accidental collision is not the end-of-the-world)
14
+ * 3. Compact size (32 bits max, versus 128 in md5; presented in less string json-safe characters)
15
+ *
16
+ * Basically, these functions are as simple as they can be, but still "random enough" for
17
+ * normal non-cryptographic use cases.
18
+ *
19
+ * To be run on the ClientSide, as in Node there are plenty of other hash options in `node:crypto`.
20
+ *
21
+ * Perf: it runs ~10 times faster than CryptoJS md5, and ~3 times smaller String hash size (for
22
+ * hashCode64).
23
+ */
24
+ export function hashCode16(s: string): string {
25
+ return hashCode(s).toString(16)
26
+ }
27
+
28
+ /**
29
+ * Returns hashCode as "radix 36", using "Base36 alphabet".
30
+ * 36 is used, because it's the maximum "radix" that Number.toString() can take.
31
+ *
32
+ * See the hashCode16 for full description.
33
+ */
34
+ export function hashCode36(s: string): string {
35
+ return hashCode(s).toString(36)
36
+ }
37
+
38
+ /**
39
+ * Returns hashCode as "radix 64", using Base64url alphabet.
40
+ * See the hashCode16 for full description.
41
+ */
42
+ export function hashCode64(s: string): string {
43
+ return numberToBase(hashCode(s), BASE64URL)
44
+ }
45
+
46
+ /**
47
+ * Generates a stable integer hashCode for a given String.
48
+ * Matches Java implementation (they say), except it ensures a positive Integer
49
+ * by adding 2147483647 + 1 to the end result.
50
+ * Source: https://stackoverflow.com/a/33647870/4919972
51
+ */
52
+ export function hashCode(s: string): Integer {
53
+ let hash = 0
54
+ let i = 0
55
+ const len = s.length
56
+ while (i < len) {
57
+ // eslint-disable-next-line no-bitwise, unicorn/prefer-math-trunc, unicorn/prefer-code-point
58
+ hash = ((hash << 5) - hash + s.charCodeAt(i++)) << 0
59
+ }
60
+ return hash + 2147483647 + 1
61
+ }
62
+
63
+ /**
64
+ * Source: https://gist.github.com/alkaruno/b84162bae5115f4ca99b
65
+ */
66
+ function numberToBase(n: number, alphabet: string): string {
67
+ const alen = alphabet.length
68
+ let result = ''
69
+
70
+ do {
71
+ result = alphabet.charAt(n % alen) + result
72
+ n = Math.floor(n / alen) - 1
73
+ } while (n > -1)
74
+
75
+ return result
76
+ }