utilium 0.8.0 → 0.8.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.
package/dist/dom.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Upload a file
3
+ * @todo use Promise.withResolvers
4
+ */
5
+ export declare function upload(type?: string, multiple?: boolean): Promise<File>;
6
+ /**
7
+ * Downloads some data
8
+ */
9
+ export declare function download(data: BlobPart, name: string): void;
package/dist/dom.js ADDED
@@ -0,0 +1,29 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
2
+ /**
3
+ * Upload a file
4
+ * @todo use Promise.withResolvers
5
+ */
6
+ export function upload(type, multiple = false) {
7
+ return new Promise((resolve, reject) => {
8
+ const input = document.createElement('input');
9
+ input.type = 'file';
10
+ if (type)
11
+ input.accept = type;
12
+ if (multiple)
13
+ input.multiple = true;
14
+ input.addEventListener('change', e => {
15
+ const file = input.files?.[0];
16
+ file ? resolve(file) : reject(new ReferenceError('No files uploaded'));
17
+ });
18
+ input.click();
19
+ });
20
+ }
21
+ /**
22
+ * Downloads some data
23
+ */
24
+ export function download(data, name) {
25
+ const link = document.createElement('a');
26
+ link.href = URL.createObjectURL(new Blob([data]));
27
+ link.download = name;
28
+ link.click();
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Typescript utilities",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Utilium
2
2
 
3
- A bunch of utilies for Typescript. This includes:
3
+ A bunch of utilities for Typescript. This includes:
4
4
 
5
5
  - Structs (using decorators)
6
6
  - Compile-time math types
package/src/dom.ts ADDED
@@ -0,0 +1,32 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
2
+
3
+ /**
4
+ * Upload a file
5
+ * @todo use Promise.withResolvers
6
+ */
7
+ export function upload(type?: string, multiple = false): Promise<File> {
8
+ return new Promise<File>((resolve, reject) => {
9
+ const input = document.createElement('input');
10
+ input.type = 'file';
11
+ if (type) input.accept = type;
12
+
13
+ if (multiple) input.multiple = true;
14
+
15
+ input.addEventListener('change', e => {
16
+ const file = input.files?.[0];
17
+ file ? resolve(file) : reject(new ReferenceError('No files uploaded'));
18
+ });
19
+
20
+ input.click();
21
+ });
22
+ }
23
+
24
+ /**
25
+ * Downloads some data
26
+ */
27
+ export function download(data: BlobPart, name: string): void {
28
+ const link = document.createElement('a');
29
+ link.href = URL.createObjectURL(new Blob([data]));
30
+ link.download = name;
31
+ link.click();
32
+ }