@forthtilliath/ts-kit 0.1.0 → 0.2.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/files/downloadText.d.ts +2 -1
- package/dist/files/downloadText.d.ts.map +1 -1
- package/dist/files/downloadText.js +3 -2
- package/dist/id/index.d.ts +2 -0
- package/dist/id/index.d.ts.map +1 -0
- package/dist/id/index.js +1 -0
- package/dist/id/randomId.d.ts +10 -0
- package/dist/id/randomId.d.ts.map +1 -0
- package/dist/id/randomId.js +33 -0
- package/package.json +2 -2
|
@@ -8,6 +8,7 @@ export declare function downloadText(filename: string, text: string): void;
|
|
|
8
8
|
* Downloads a text blob as a file.
|
|
9
9
|
* @param filename The filename to save to.
|
|
10
10
|
* @param text The text to save.
|
|
11
|
+
* @param mimeType The MIME type of the blob (defaults to `"text/plain"`).
|
|
11
12
|
*/
|
|
12
|
-
export declare function downloadTextBlob(filename: string, text: string): void;
|
|
13
|
+
export declare function downloadTextBlob(filename: string, text: string, mimeType?: string): void;
|
|
13
14
|
//# sourceMappingURL=downloadText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"downloadText.d.ts","sourceRoot":"","sources":["../../src/files/downloadText.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAcjE;AAED
|
|
1
|
+
{"version":3,"file":"downloadText.d.ts","sourceRoot":"","sources":["../../src/files/downloadText.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAcjE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,QAAQ,SAAe,GACtB,IAAI,CAcN"}
|
|
@@ -16,9 +16,10 @@ export function downloadText(filename, text) {
|
|
|
16
16
|
* Downloads a text blob as a file.
|
|
17
17
|
* @param filename The filename to save to.
|
|
18
18
|
* @param text The text to save.
|
|
19
|
+
* @param mimeType The MIME type of the blob (defaults to `"text/plain"`).
|
|
19
20
|
*/
|
|
20
|
-
export function downloadTextBlob(filename, text) {
|
|
21
|
-
const url = URL.createObjectURL(new Blob([text], { type:
|
|
21
|
+
export function downloadTextBlob(filename, text, mimeType = "text/plain") {
|
|
22
|
+
const url = URL.createObjectURL(new Blob([text], { type: `${mimeType};charset=utf-8` }));
|
|
22
23
|
const a = document.createElement("a");
|
|
23
24
|
a.setAttribute("download", filename);
|
|
24
25
|
a.setAttribute("href", url);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/id/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
|
package/dist/id/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./randomId.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `crypto.randomUUID()` with a fallback. That API only exists in a secure
|
|
3
|
+
* context (HTTPS or `localhost`) and is missing on a few older browsers. The
|
|
4
|
+
* fallbacks are not cryptographically secure but are good enough for local
|
|
5
|
+
* identifiers.
|
|
6
|
+
*
|
|
7
|
+
* @returns {string} A UUID-v4-shaped string, or a timestamp-based id as a last resort.
|
|
8
|
+
*/
|
|
9
|
+
export declare function randomId(): string;
|
|
10
|
+
//# sourceMappingURL=randomId.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomId.d.ts","sourceRoot":"","sources":["../../src/id/randomId.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,QAAQ,IAAI,MAAM,CA6BjC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `crypto.randomUUID()` with a fallback. That API only exists in a secure
|
|
3
|
+
* context (HTTPS or `localhost`) and is missing on a few older browsers. The
|
|
4
|
+
* fallbacks are not cryptographically secure but are good enough for local
|
|
5
|
+
* identifiers.
|
|
6
|
+
*
|
|
7
|
+
* @returns {string} A UUID-v4-shaped string, or a timestamp-based id as a last resort.
|
|
8
|
+
*/
|
|
9
|
+
export function randomId() {
|
|
10
|
+
// `globalThis.crypto` is typed as always present by lib.dom, but it can be
|
|
11
|
+
// missing at runtime (older browser, insecure context) — cast it away so
|
|
12
|
+
// the checks below are not flagged as unnecessary.
|
|
13
|
+
const c = globalThis.crypto;
|
|
14
|
+
if (typeof c?.randomUUID === "function") {
|
|
15
|
+
return c.randomUUID();
|
|
16
|
+
}
|
|
17
|
+
if (typeof c?.getRandomValues === "function") {
|
|
18
|
+
const bytes = c.getRandomValues(new Uint8Array(16));
|
|
19
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40; // version 4
|
|
20
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80; // variant
|
|
21
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0"));
|
|
22
|
+
return (hex.slice(0, 4).join("") +
|
|
23
|
+
"-" +
|
|
24
|
+
hex.slice(4, 6).join("") +
|
|
25
|
+
"-" +
|
|
26
|
+
hex.slice(6, 8).join("") +
|
|
27
|
+
"-" +
|
|
28
|
+
hex.slice(8, 10).join("") +
|
|
29
|
+
"-" +
|
|
30
|
+
hex.slice(10, 16).join(""));
|
|
31
|
+
}
|
|
32
|
+
return `id-${Date.now().toString(16)}-${Math.random().toString(16).slice(2, 10)}`;
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forthtilliath/ts-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"jsdom": "^29.1.1",
|
|
25
25
|
"typescript": "^5.9.3",
|
|
26
26
|
"vitest": "^4.1.10",
|
|
27
|
-
"@forthtilliath/eslint-config": "0.
|
|
27
|
+
"@forthtilliath/eslint-config": "0.1.0",
|
|
28
28
|
"@forthtilliath/typescript-config": "0.0.0"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|