@chainfuse/helpers 3.2.10 → 3.2.12

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.
@@ -0,0 +1,36 @@
1
+ import type { Request as CfRequest, WorkerVersionMetadata } from '@cloudflare/workers-types/experimental';
2
+ import type { Chalk } from 'chalk';
3
+ interface QwikCityPlatform {
4
+ request?: CfRequest;
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']>;
14
+ static precisionFloat(input: string): number;
15
+ /**
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.
17
+ * However, like `Promise.all()`, it only returns the values of successfully fulfilled promises without needing to manually check their status.
18
+ *
19
+ * @param promises - An array of promises to be settled.
20
+ * @returns A promise that resolves to an array of fulfilled values from the input promises.
21
+ */
22
+ static getFulfilledResults<T extends unknown>(promises: PromiseLike<T>[]): Promise<Awaited<T>[]>;
23
+ static areArraysEqual<T>(array1: T[], array2: T[]): boolean;
24
+ private static isWorkerVersionMetadata;
25
+ /**
26
+ * Determines if the provided object is running local based on its type and properties.
27
+ *
28
+ * For `WorkerVersionMetadata`, it checks if the `timestamp` property is absent.
29
+ * For `QwikCityPlatform`, it checks if the `request` property is absent.
30
+ *
31
+ * @param metadataOrPlatform - The object to evaluate, which can be either `WorkerVersionMetadata` or `QwikCityPlatform`.
32
+ * @returns `true` if the running local, otherwise `false`.
33
+ */
34
+ static isLocal<P extends QwikCityPlatform>(metadataOrPlatform: WorkerVersionMetadata | P): boolean;
35
+ }
36
+ export {};
@@ -0,0 +1,91 @@
1
+ export class Helpers {
2
+ /**
3
+ * 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
4
+ *
5
+ * @param id - The input string used to generate the unique color.
6
+ * @returns A tuple containing the RGB values [r, g, b].
7
+ */
8
+ static uniqueIdColor(id) {
9
+ // Hash the string to a numeric value
10
+ let hash = 0;
11
+ for (let i = 0; i < id.length; i++) {
12
+ const char = id.charCodeAt(i);
13
+ hash = (hash << 5) - hash + char;
14
+ hash |= 0; // Convert to 32-bit integer
15
+ }
16
+ // Convert the hash to RGB components
17
+ let r = (hash & 0xff0000) >> 16; // Extract red
18
+ let g = (hash & 0x00ff00) >> 8; // Extract green
19
+ let b = hash & 0x0000ff; // Extract blue
20
+ // Clamp RGB values to a more legible range (e.g., 64-200)
21
+ const clamp = (value) => Math.max(100, Math.min(222, value));
22
+ r = clamp(r);
23
+ g = clamp(g);
24
+ b = clamp(b);
25
+ return [r, g, b];
26
+ }
27
+ static precisionFloat(input) {
28
+ if (!input.includes('.')) {
29
+ // No decimal point means it's an integer, just return as a float
30
+ return parseFloat(input);
31
+ }
32
+ else {
33
+ if (input.endsWith('0')) {
34
+ // Replace the last '0' with '1' while keeping other characters unchanged
35
+ return parseFloat(input.substring(0, input.length - 1) + '1');
36
+ }
37
+ else {
38
+ // If last decimal is not zero, parse as usual
39
+ return parseFloat(input);
40
+ }
41
+ }
42
+ }
43
+ /**
44
+ * 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.
45
+ * However, like `Promise.all()`, it only returns the values of successfully fulfilled promises without needing to manually check their status.
46
+ *
47
+ * @param promises - An array of promises to be settled.
48
+ * @returns A promise that resolves to an array of fulfilled values from the input promises.
49
+ */
50
+ static getFulfilledResults(promises) {
51
+ return Promise.allSettled(promises).then((results) => results.filter((result) => result.status === 'fulfilled').map((result) => result.value));
52
+ }
53
+ static areArraysEqual(array1, array2) {
54
+ // Quick length check for early exit
55
+ if (array1.length !== array2.length) {
56
+ return false;
57
+ }
58
+ // Use Set for efficient comparison if arrays are of primitive types
59
+ const set1 = new Set(array1);
60
+ const set2 = new Set(array2);
61
+ if (set1.size !== set2.size) {
62
+ return false;
63
+ }
64
+ for (const item of set1) {
65
+ if (!set2.has(item)) {
66
+ return false;
67
+ }
68
+ }
69
+ return true;
70
+ }
71
+ static isWorkerVersionMetadata(metadata) {
72
+ return 'id' in metadata;
73
+ }
74
+ /**
75
+ * Determines if the provided object is running local based on its type and properties.
76
+ *
77
+ * For `WorkerVersionMetadata`, it checks if the `timestamp` property is absent.
78
+ * For `QwikCityPlatform`, it checks if the `request` property is absent.
79
+ *
80
+ * @param metadataOrPlatform - The object to evaluate, which can be either `WorkerVersionMetadata` or `QwikCityPlatform`.
81
+ * @returns `true` if the running local, otherwise `false`.
82
+ */
83
+ static isLocal(metadataOrPlatform) {
84
+ if (this.isWorkerVersionMetadata(metadataOrPlatform)) {
85
+ return !('timestamp' in metadataOrPlatform);
86
+ }
87
+ else {
88
+ return !('request' in metadataOrPlatform);
89
+ }
90
+ }
91
+ }
package/dist/index.d.mts CHANGED
@@ -1,49 +1,6 @@
1
- import type { PlatformCloudflarePages } from '@builder.io/qwik-city/middleware/cloudflare-pages';
2
- import type { Request as CfRequest, ExecutionContext, WorkerVersionMetadata } from '@cloudflare/workers-types/experimental';
3
- import type { Chalk } from 'chalk';
4
- import type { PlatformProxy } from 'wrangler';
5
1
  export * from './buffers.mjs';
2
+ export * from './common.mjs';
6
3
  export * from './crypto.mjs';
7
4
  export * from './discord.mjs';
8
5
  export * from './dns.mjs';
9
6
  export * from './net.mjs';
10
- interface QwikCityPlatformLive<E extends Record<string, any> = Record<string, any>> extends Omit<PlatformCloudflarePages, 'request'> {
11
- request: CfRequest;
12
- env: E;
13
- ctx: ExecutionContext;
14
- cf: never;
15
- }
16
- interface QwikCityPlatformLocal<E extends Record<string, any> = Record<string, any>> extends PlatformProxy<E> {
17
- request?: never;
18
- }
19
- type QwikCityPlatform = QwikCityPlatformLive | QwikCityPlatformLocal;
20
- export declare class Helpers {
21
- /**
22
- * 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
23
- *
24
- * @param id - The input string used to generate the unique color.
25
- * @returns A tuple containing the RGB values [r, g, b].
26
- */
27
- static uniqueIdColor(id: string): Parameters<InstanceType<typeof Chalk>['rgb']>;
28
- static precisionFloat(input: string): number;
29
- /**
30
- * 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.
31
- * However, like `Promise.all()`, it only returns the values of successfully fulfilled promises without needing to manually check their status.
32
- *
33
- * @param promises - An array of promises to be settled.
34
- * @returns A promise that resolves to an array of fulfilled values from the input promises.
35
- */
36
- static getFulfilledResults<T extends unknown>(promises: PromiseLike<T>[]): Promise<Awaited<T>[]>;
37
- static areArraysEqual<T>(array1: T[], array2: T[]): boolean;
38
- private static isWorkerVersionMetadata;
39
- /**
40
- * Determines if the provided object is running local based on its type and properties.
41
- *
42
- * For `WorkerVersionMetadata`, it checks if the `timestamp` property is absent.
43
- * For `QwikCityPlatform`, it checks if the `request` property is absent.
44
- *
45
- * @param metadataOrPlatform - The object to evaluate, which can be either `WorkerVersionMetadata` or `QwikCityPlatform`.
46
- * @returns `true` if the running local, otherwise `false`.
47
- */
48
- static isLocal<P extends QwikCityPlatform>(metadataOrPlatform: WorkerVersionMetadata | P): boolean;
49
- }
package/dist/index.mjs CHANGED
@@ -1,96 +1,6 @@
1
1
  export * from './buffers.mjs';
2
+ export * from './common.mjs';
2
3
  export * from './crypto.mjs';
3
4
  export * from './discord.mjs';
4
5
  export * from './dns.mjs';
5
6
  export * from './net.mjs';
6
- export 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) {
14
- // Hash the string to a numeric value
15
- let hash = 0;
16
- for (let i = 0; i < id.length; i++) {
17
- const char = id.charCodeAt(i);
18
- hash = (hash << 5) - hash + char;
19
- hash |= 0; // Convert to 32-bit integer
20
- }
21
- // Convert the hash to RGB components
22
- let r = (hash & 0xff0000) >> 16; // Extract red
23
- let g = (hash & 0x00ff00) >> 8; // Extract green
24
- let b = hash & 0x0000ff; // Extract blue
25
- // Clamp RGB values to a more legible range (e.g., 64-200)
26
- const clamp = (value) => Math.max(100, Math.min(222, value));
27
- r = clamp(r);
28
- g = clamp(g);
29
- b = clamp(b);
30
- return [r, g, b];
31
- }
32
- static precisionFloat(input) {
33
- if (!input.includes('.')) {
34
- // No decimal point means it's an integer, just return as a float
35
- return parseFloat(input);
36
- }
37
- else {
38
- if (input.endsWith('0')) {
39
- // Replace the last '0' with '1' while keeping other characters unchanged
40
- return parseFloat(input.substring(0, input.length - 1) + '1');
41
- }
42
- else {
43
- // If last decimal is not zero, parse as usual
44
- return parseFloat(input);
45
- }
46
- }
47
- }
48
- /**
49
- * 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.
50
- * However, like `Promise.all()`, it only returns the values of successfully fulfilled promises without needing to manually check their status.
51
- *
52
- * @param promises - An array of promises to be settled.
53
- * @returns A promise that resolves to an array of fulfilled values from the input promises.
54
- */
55
- static getFulfilledResults(promises) {
56
- return Promise.allSettled(promises).then((results) => results.filter((result) => result.status === 'fulfilled').map((result) => result.value));
57
- }
58
- static areArraysEqual(array1, array2) {
59
- // Quick length check for early exit
60
- if (array1.length !== array2.length) {
61
- return false;
62
- }
63
- // Use Set for efficient comparison if arrays are of primitive types
64
- const set1 = new Set(array1);
65
- const set2 = new Set(array2);
66
- if (set1.size !== set2.size) {
67
- return false;
68
- }
69
- for (const item of set1) {
70
- if (!set2.has(item)) {
71
- return false;
72
- }
73
- }
74
- return true;
75
- }
76
- static isWorkerVersionMetadata(metadata) {
77
- return 'id' in metadata;
78
- }
79
- /**
80
- * Determines if the provided object is running local based on its type and properties.
81
- *
82
- * For `WorkerVersionMetadata`, it checks if the `timestamp` property is absent.
83
- * For `QwikCityPlatform`, it checks if the `request` property is absent.
84
- *
85
- * @param metadataOrPlatform - The object to evaluate, which can be either `WorkerVersionMetadata` or `QwikCityPlatform`.
86
- * @returns `true` if the running local, otherwise `false`.
87
- */
88
- static isLocal(metadataOrPlatform) {
89
- if (this.isWorkerVersionMetadata(metadataOrPlatform)) {
90
- return !('timestamp' in metadataOrPlatform);
91
- }
92
- else {
93
- return !('request' in metadataOrPlatform);
94
- }
95
- }
96
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/helpers",
3
- "version": "3.2.10",
3
+ "version": "3.2.12",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
@@ -44,23 +44,45 @@
44
44
  ".": {
45
45
  "import": "./dist/index.mjs",
46
46
  "types": "./dist/index.d.mts"
47
+ },
48
+ "./buffers": {
49
+ "import": "./dist/buffers.js",
50
+ "types": "./dist/buffers.d.ts"
51
+ },
52
+ "./common": {
53
+ "import": "./dist/common.js",
54
+ "types": "./dist/common.d.ts"
55
+ },
56
+ "./crypto": {
57
+ "import": "./dist/crypto.js",
58
+ "types": "./dist/crypto.d.ts"
59
+ },
60
+ "./discord": {
61
+ "import": "./dist/discord.js",
62
+ "types": "./dist/discord.d.ts"
63
+ },
64
+ "./dns": {
65
+ "import": "./dist/dns.js",
66
+ "types": "./dist/dns.d.ts"
67
+ },
68
+ "./net": {
69
+ "import": "./dist/net.js",
70
+ "types": "./dist/net.d.ts"
47
71
  }
48
72
  },
49
73
  "prettier": "@demosjarco/prettier-config",
50
74
  "dependencies": {
51
- "@chainfuse/types": "^2.10.18",
75
+ "@chainfuse/types": "^2.10.20",
52
76
  "@discordjs/rest": "^2.5.0",
53
77
  "chalk": "^5.4.1",
54
78
  "cloudflare": "^4.3.0",
55
79
  "strip-ansi": "^7.1.0",
56
80
  "uuid": "^11.1.0",
57
- "zod": "^3.25.42"
81
+ "zod": "^3.25.49"
58
82
  },
59
83
  "devDependencies": {
60
- "@builder.io/qwik-city": "^1.14.1",
61
- "@cloudflare/workers-types": "^4.20250529.0",
62
- "@types/node": "^22.15.26",
63
- "wrangler": "^4.18.0"
84
+ "@cloudflare/workers-types": "^4.20250603.0",
85
+ "@types/node": "^22.15.29"
64
86
  },
65
- "gitHead": "e7723806e2f740dd24652028891ddc4aa4fa0e57"
87
+ "gitHead": "932b7500502575cce5fc6751d594f14098bdd543"
66
88
  }