@chainfuse/helpers 0.4.1 → 0.5.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/README.md +1 -1
- package/dist/crypto.d.mts +3 -9
- package/dist/crypto.mjs +4 -10
- package/dist/index.d.mts +9 -0
- package/dist/index.mjs +43 -0
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://securityscorecards.dev/viewer/?uri=github.com/ChainFuse/packages)[](https://socket.dev/npm/package/@
|
|
1
|
+
[](https://securityscorecards.dev/viewer/?uri=github.com/ChainFuse/packages)[](https://socket.dev/npm/package/@chainfuse/helpers)
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
package/dist/crypto.d.mts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
export declare class CryptoHelpers {
|
|
2
|
-
static secretBytes(byteSize: number): Promise<Uint8Array<ArrayBuffer> | Uint8Array<ArrayBuffer
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
static base16secret(byteSize: number): Promise<string>;
|
|
7
|
-
/**
|
|
8
|
-
* @yields secret length = (`byteSize` * Math.log2(62)) / 8
|
|
9
|
-
*/
|
|
10
|
-
static base62secret(byteSize: number): Promise<string>;
|
|
2
|
+
static secretBytes(byteSize: number): Promise<Uint8Array<ArrayBuffer | SharedArrayBuffer> | Uint8Array<ArrayBuffer>>;
|
|
3
|
+
static base16secret(secretLength: number): Promise<string>;
|
|
4
|
+
static base62secret(secretLength: number): Promise<string>;
|
|
11
5
|
static getHash(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', input: string | ArrayBufferLike): Promise<string>;
|
|
12
6
|
/**
|
|
13
7
|
* @returns Fully formatted (double quote encapsulated) `ETag` header value
|
package/dist/crypto.mjs
CHANGED
|
@@ -12,20 +12,14 @@ export class CryptoHelpers {
|
|
|
12
12
|
return randomBytes;
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
18
|
-
static base16secret(byteSize) {
|
|
19
|
-
return this.secretBytes(byteSize).then((bytes) => BufferHelpers.bufferToHex(bytes.buffer));
|
|
15
|
+
static base16secret(secretLength) {
|
|
16
|
+
return this.secretBytes(secretLength / 2).then((bytes) => BufferHelpers.bufferToHex(bytes.buffer));
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
* @yields secret length = (`byteSize` * Math.log2(62)) / 8
|
|
23
|
-
*/
|
|
24
|
-
static base62secret(byteSize) {
|
|
18
|
+
static base62secret(secretLength) {
|
|
25
19
|
const LOWER_CHAR_SET = 'abcdefghijklmnopqrstuvwxyz';
|
|
26
20
|
const NUMBER_CHAR_SET = '0123456789';
|
|
27
21
|
const CHAR_SET = `${NUMBER_CHAR_SET}${LOWER_CHAR_SET}${LOWER_CHAR_SET.toUpperCase()}`;
|
|
28
|
-
return this.secretBytes(
|
|
22
|
+
return this.secretBytes(secretLength).then((randomBytes) => {
|
|
29
23
|
/**
|
|
30
24
|
* @link https://jsbm.dev/x1F2ITy7RU8T2
|
|
31
25
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import type { Chalk } from 'chalk';
|
|
1
2
|
export * from './buffers.mjs';
|
|
2
3
|
export * from './crypto.mjs';
|
|
3
4
|
export * from './discord.mjs';
|
|
4
5
|
export * from './net.mjs';
|
|
5
6
|
export declare class Helpers {
|
|
7
|
+
/**
|
|
8
|
+
* Generates a unique RGB color based unique to the provided string ID. The RGB values are clamped to a range that ensures the resulting color is legible
|
|
9
|
+
*
|
|
10
|
+
* @param id - The input string used to generate the unique color.
|
|
11
|
+
* @returns A tuple containing the RGB values [r, g, b].
|
|
12
|
+
*/
|
|
13
|
+
static uniqueIdColor(id: string): Parameters<InstanceType<typeof Chalk>['rgb']>;
|
|
6
14
|
static precisionFloat(input: string): number;
|
|
7
15
|
/**
|
|
8
16
|
* A wrapper around `Promise.allSettled()` that filters and returns only the fulfilled results. This method behaves like `Promise.allSettled()` where one promise failing doesn't stop others.
|
|
@@ -12,4 +20,5 @@ export declare class Helpers {
|
|
|
12
20
|
* @returns A promise that resolves to an array of fulfilled values from the input promises.
|
|
13
21
|
*/
|
|
14
22
|
static getFulfilledResults<T extends unknown>(promises: PromiseLike<T>[]): Promise<Awaited<T>[]>;
|
|
23
|
+
static areArraysEqual<T>(array1: T[], array2: T[]): boolean;
|
|
15
24
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,31 @@ export * from './crypto.mjs';
|
|
|
3
3
|
export * from './discord.mjs';
|
|
4
4
|
export * from './net.mjs';
|
|
5
5
|
export class Helpers {
|
|
6
|
+
/**
|
|
7
|
+
* Generates a unique RGB color based unique to the provided string ID. The RGB values are clamped to a range that ensures the resulting color is legible
|
|
8
|
+
*
|
|
9
|
+
* @param id - The input string used to generate the unique color.
|
|
10
|
+
* @returns A tuple containing the RGB values [r, g, b].
|
|
11
|
+
*/
|
|
12
|
+
static uniqueIdColor(id) {
|
|
13
|
+
// Hash the string to a numeric value
|
|
14
|
+
let hash = 0;
|
|
15
|
+
for (let i = 0; i < id.length; i++) {
|
|
16
|
+
const char = id.charCodeAt(i);
|
|
17
|
+
hash = (hash << 5) - hash + char;
|
|
18
|
+
hash |= 0; // Convert to 32-bit integer
|
|
19
|
+
}
|
|
20
|
+
// Convert the hash to RGB components
|
|
21
|
+
let r = (hash & 0xff0000) >> 16; // Extract red
|
|
22
|
+
let g = (hash & 0x00ff00) >> 8; // Extract green
|
|
23
|
+
let b = hash & 0x0000ff; // Extract blue
|
|
24
|
+
// Clamp RGB values to a more legible range (e.g., 64-200)
|
|
25
|
+
const clamp = (value) => Math.max(100, Math.min(222, value));
|
|
26
|
+
r = clamp(r);
|
|
27
|
+
g = clamp(g);
|
|
28
|
+
b = clamp(b);
|
|
29
|
+
return [r, g, b];
|
|
30
|
+
}
|
|
6
31
|
static precisionFloat(input) {
|
|
7
32
|
if (!input.includes('.')) {
|
|
8
33
|
// No decimal point means it's an integer, just return as a float
|
|
@@ -29,4 +54,22 @@ export class Helpers {
|
|
|
29
54
|
static getFulfilledResults(promises) {
|
|
30
55
|
return Promise.allSettled(promises).then((results) => results.filter((result) => result.status === 'fulfilled').map((result) => result.value));
|
|
31
56
|
}
|
|
57
|
+
static areArraysEqual(array1, array2) {
|
|
58
|
+
// Quick length check for early exit
|
|
59
|
+
if (array1.length !== array2.length) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
// Use Set for efficient comparison if arrays are of primitive types
|
|
63
|
+
const set1 = new Set(array1);
|
|
64
|
+
const set2 = new Set(array2);
|
|
65
|
+
if (set1.size !== set2.size) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
for (const item of set1) {
|
|
69
|
+
if (!set2.has(item)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
32
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
|
|
@@ -30,8 +30,7 @@
|
|
|
30
30
|
"build": "tsc",
|
|
31
31
|
"build:clean": "npm run build -- --build --clean && npm run build",
|
|
32
32
|
"pretest": "tsc --project tsconfig.tests.json",
|
|
33
|
-
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout"
|
|
34
|
-
"test:local": "npm run test"
|
|
33
|
+
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout"
|
|
35
34
|
},
|
|
36
35
|
"type": "module",
|
|
37
36
|
"bugs": {
|
|
@@ -49,14 +48,14 @@
|
|
|
49
48
|
},
|
|
50
49
|
"prettier": "@demosjarco/prettier-config",
|
|
51
50
|
"dependencies": {
|
|
52
|
-
"@discordjs/rest": "^2.4.
|
|
53
|
-
"chalk": "^5.
|
|
51
|
+
"@discordjs/rest": "^2.4.2",
|
|
52
|
+
"chalk": "^5.4.1",
|
|
54
53
|
"cloudflare": "^3.5.0",
|
|
55
54
|
"uuid": "^11.0.3"
|
|
56
55
|
},
|
|
57
56
|
"devDependencies": {
|
|
58
|
-
"@chainfuse/types": "^1.
|
|
59
|
-
"@types/node": "^22.10.
|
|
57
|
+
"@chainfuse/types": "^1.4.0",
|
|
58
|
+
"@types/node": "^22.10.5"
|
|
60
59
|
},
|
|
61
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "13881adbb875a4b1680448205ff9e0ac55577f72"
|
|
62
61
|
}
|