@yiuayiu/functions 0.1.1 → 0.1.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.
@@ -1,4 +1,4 @@
1
- export declare function sha1(string: string): Promise<string>;
2
- export declare function sha512(str: string): Promise<string>;
3
- export declare function hashToString(hashBuffer: ArrayBuffer): Promise<string>;
1
+ export declare function sha1(string: string | Buffer): Promise<string>;
2
+ export declare function sha512(input: string | Buffer): Promise<string>;
3
+ export declare function digest(input: string | Buffer, algorithm: "SHA-1" | "SHA-512"): Promise<string>;
4
4
  export declare function md5(_string: string): Promise<string>;
package/dist/functions.js CHANGED
@@ -1,17 +1,15 @@
1
1
  const { subtle } = globalThis.crypto;
2
2
  export async function sha1(string) {
3
- const encoder = new TextEncoder();
4
- const data = encoder.encode(string);
5
- const hashBuffer = await subtle.digest("SHA-1", data);
6
- return hashToString(hashBuffer);
3
+ return digest(string, "SHA-1");
7
4
  }
8
- export async function sha512(str) {
9
- const encoder = new TextEncoder();
10
- const data = encoder.encode(str);
11
- const hashBuffer = await subtle.digest("SHA-512", data);
12
- return hashToString(hashBuffer);
5
+ export async function sha512(input) {
6
+ return digest(input, "SHA-512");
13
7
  }
14
- export async function hashToString(hashBuffer) {
8
+ export async function digest(input, algorithm) {
9
+ const inputData = typeof input === "string"
10
+ ? new TextEncoder().encode(input)
11
+ : new Uint8Array(input);
12
+ const hashBuffer = await subtle.digest(algorithm, inputData);
15
13
  return Array.from(new Uint8Array(hashBuffer))
16
14
  .map((b) => b.toString(16).padStart(2, "0"))
17
15
  .join("");
@@ -0,0 +1 @@
1
+ {"root":["../src/functions.test.ts","../src/functions.ts"],"version":"5.9.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiuayiu/functions",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "main": "dist/functions.js",
6
6
  "type": "module",
@@ -12,10 +12,12 @@
12
12
  },
13
13
  "devDependencies": {
14
14
  "@types/node": "^25.1.0",
15
+ "typescript": "^5.9.3",
15
16
  "vitest": "^4.0.18"
16
17
  },
17
18
  "scripts": {
18
19
  "build": "tsc -b",
20
+ "dev": "tsc -b --watch",
19
21
  "test": "vitest"
20
22
  }
21
23
  }