@taybart/corvid 0.0.1

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 ADDED
@@ -0,0 +1 @@
1
+ # Corvid
package/dist/dom.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ /***************
2
+ * DOM *
3
+ ***************/
4
+ export declare function ready(cb: () => void): void;
5
+ export declare class btn {
6
+ el: Element | null;
7
+ constructor(id: string, content: string);
8
+ onClick(cb: (ev: Event) => void): void;
9
+ }
10
+ export declare class form {
11
+ el: Element | null;
12
+ constructor(id: string);
13
+ byID(id: string): this;
14
+ onChange(cb: (ev: Event) => void): void;
15
+ }
16
+ export declare class el {
17
+ el: HTMLElement | null;
18
+ constructor({ id, type, content, }: {
19
+ id: string;
20
+ type: string;
21
+ content?: any;
22
+ });
23
+ child(ch: HTMLElement): this;
24
+ inner(content: any): this;
25
+ src(url: string): this;
26
+ style(style: Object | string): this;
27
+ onClick(cb: (ev: Event) => void): this;
28
+ listen(event: string, cb: (ev: Event) => void): this;
29
+ }
30
+ export declare class params {
31
+ params: URLSearchParams;
32
+ constructor();
33
+ set(p: Object): this;
34
+ toString(): string;
35
+ }
36
+ export declare const ls: {
37
+ get(k: string): string | null;
38
+ set(k: string, v: any): void;
39
+ };
@@ -0,0 +1,3 @@
1
+ export * as dom from './dom';
2
+ export * as style from './style';
3
+ export * as utils from './utils';
package/dist/index.js ADDED
@@ -0,0 +1,277 @@
1
+ var __webpack_require__ = {};
2
+ (()=>{
3
+ __webpack_require__.d = (exports, definition)=>{
4
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, {
5
+ enumerable: true,
6
+ get: definition[key]
7
+ });
8
+ };
9
+ })();
10
+ (()=>{
11
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
12
+ })();
13
+ (()=>{
14
+ __webpack_require__.r = (exports)=>{
15
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports, Symbol.toStringTag, {
16
+ value: 'Module'
17
+ });
18
+ Object.defineProperty(exports, '__esModule', {
19
+ value: true
20
+ });
21
+ };
22
+ })();
23
+ var dom_namespaceObject = {};
24
+ __webpack_require__.r(dom_namespaceObject);
25
+ __webpack_require__.d(dom_namespaceObject, {
26
+ btn: ()=>btn,
27
+ el: ()=>el,
28
+ form: ()=>dom_form,
29
+ ls: ()=>ls,
30
+ params: ()=>params,
31
+ ready: ()=>ready
32
+ });
33
+ var style_namespaceObject = {};
34
+ __webpack_require__.r(style_namespaceObject);
35
+ __webpack_require__.d(style_namespaceObject, {
36
+ cssVar: ()=>cssVar,
37
+ gradient: ()=>gradient,
38
+ isDarkMode: ()=>isDarkMode,
39
+ onDarkMode: ()=>onDarkMode,
40
+ switchTheme: ()=>switchTheme
41
+ });
42
+ var utils_namespaceObject = {};
43
+ __webpack_require__.r(utils_namespaceObject);
44
+ __webpack_require__.d(utils_namespaceObject, {
45
+ bytesToHuman: ()=>bytesToHuman,
46
+ genID: ()=>genID,
47
+ request: ()=>request
48
+ });
49
+ function _define_property(obj, key, value) {
50
+ if (key in obj) Object.defineProperty(obj, key, {
51
+ value: value,
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true
55
+ });
56
+ else obj[key] = value;
57
+ return obj;
58
+ }
59
+ function ready(cb) {
60
+ window.addEventListener('DOMContentLoaded', cb);
61
+ }
62
+ class btn {
63
+ onClick(cb) {
64
+ var _this_el;
65
+ null == (_this_el = this.el) || _this_el.addEventListener('click', cb);
66
+ }
67
+ constructor(id, content){
68
+ _define_property(this, "el", void 0);
69
+ this.el = null;
70
+ if (id) {
71
+ this.el = document.getElementById(id);
72
+ if (this.el && content) this.el.innerHTML = content;
73
+ }
74
+ }
75
+ }
76
+ class dom_form {
77
+ byID(id) {
78
+ this.el = document.getElementById(id);
79
+ return this;
80
+ }
81
+ onChange(cb) {
82
+ var _this_el;
83
+ null == (_this_el = this.el) || _this_el.addEventListener('change', cb);
84
+ }
85
+ constructor(id){
86
+ _define_property(this, "el", void 0);
87
+ this.el = null;
88
+ if (id) this.el = document.getElementById(id);
89
+ }
90
+ }
91
+ class el {
92
+ child(ch) {
93
+ var _this_el;
94
+ null == (_this_el = this.el) || _this_el.appendChild(ch);
95
+ return this;
96
+ }
97
+ inner(content) {
98
+ if (this.el) this.el.innerHTML = content;
99
+ return this;
100
+ }
101
+ src(url) {
102
+ if (this.el instanceof HTMLIFrameElement) this.el.src = url;
103
+ return this;
104
+ }
105
+ style(style) {
106
+ if (this.el) {
107
+ if ('string' == typeof style) this.el.style = style;
108
+ else if ('object' == typeof style) {
109
+ let s = '';
110
+ Object.entries(style).forEach(([k, v])=>s += `${k}:${v};`);
111
+ this.el.style = s;
112
+ }
113
+ }
114
+ return this;
115
+ }
116
+ onClick(cb) {
117
+ var _this_el;
118
+ null == (_this_el = this.el) || _this_el.addEventListener('click', cb);
119
+ return this;
120
+ }
121
+ listen(event, cb) {
122
+ var _this_el;
123
+ null == (_this_el = this.el) || _this_el.addEventListener(event, cb);
124
+ return this;
125
+ }
126
+ constructor({ id, type, content }){
127
+ _define_property(this, "el", void 0);
128
+ if (id) this.el = document.getElementById(id);
129
+ else if (type) this.el = document.createElement(type);
130
+ else throw new Error('no id or type provided');
131
+ if (this.el && content) this.el.innerHTML = content;
132
+ }
133
+ }
134
+ class params {
135
+ set(p) {
136
+ for (let [k, v] of Object.entries(p))this.params.set(k, v);
137
+ return this;
138
+ }
139
+ toString() {
140
+ return this.params.toString();
141
+ }
142
+ constructor(){
143
+ _define_property(this, "params", void 0);
144
+ this.params = new URLSearchParams();
145
+ }
146
+ }
147
+ const ls = {
148
+ get (k) {
149
+ return localStorage.getItem(k);
150
+ },
151
+ set (k, v) {
152
+ localStorage.setItem(k, v);
153
+ }
154
+ };
155
+ function switchTheme(theme) {
156
+ document.documentElement.setAttribute('data-theme', theme);
157
+ }
158
+ function cssVar(name) {
159
+ const style = window.getComputedStyle(document.body);
160
+ return style.getPropertyValue(name);
161
+ }
162
+ function isDarkMode() {
163
+ return window.matchMedia('(prefers-color-scheme: dark)').matches;
164
+ }
165
+ function onDarkMode(cb) {
166
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (ev)=>{
167
+ cb(ev.matches);
168
+ });
169
+ }
170
+ function gradient(start, end, value) {
171
+ value = Math.max(0, Math.min(100, value));
172
+ const red = Math.round(start.red + (end.red - start.red) * value / 100);
173
+ const green = Math.round(start.green + (end.green - start.green) * value / 100);
174
+ const blue = Math.round(start.blue + (end.blue - start.blue) * value / 100);
175
+ const alpha = Math.round(start.alpha + (end.alpha - start.alpha) * value / 100);
176
+ return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
177
+ }
178
+ function utils_define_property(obj, key, value) {
179
+ if (key in obj) Object.defineProperty(obj, key, {
180
+ value: value,
181
+ enumerable: true,
182
+ configurable: true,
183
+ writable: true
184
+ });
185
+ else obj[key] = value;
186
+ return obj;
187
+ }
188
+ function bytesToHuman(bytes, options = {}) {
189
+ const { useSI = false, decimals = 2, includeUnits = true, targetUnit = null } = options;
190
+ const unit = useSI ? 1000 : 1024;
191
+ const units = useSI ? [
192
+ 'B',
193
+ 'kB',
194
+ 'MB',
195
+ 'GB',
196
+ 'TB',
197
+ 'PB',
198
+ 'EB',
199
+ 'ZB',
200
+ 'YB'
201
+ ] : [
202
+ 'B',
203
+ 'KiB',
204
+ 'MiB',
205
+ 'GiB',
206
+ 'TiB',
207
+ 'PiB',
208
+ 'EiB',
209
+ 'ZiB',
210
+ 'YiB'
211
+ ];
212
+ if (null === targetUnit) {
213
+ let val = parseInt(bytes, 10);
214
+ if (Math.abs(val) < unit) return `${bytes} B`;
215
+ let u = 0;
216
+ while(Math.abs(val) >= unit && u < units.length - 1){
217
+ val /= unit;
218
+ u++;
219
+ }
220
+ if (includeUnits) return `${val.toFixed(decimals)} ${units[u]}`;
221
+ return `${val.toFixed(decimals)}`;
222
+ }
223
+ const targetUnitIndex = units.indexOf(targetUnit);
224
+ if (-1 === targetUnitIndex) throw new Error(`Invalid unit: ${targetUnit}. Valid units are: ${units.join(', ')}`);
225
+ let val = parseInt(bytes, 10);
226
+ for(let i = 0; i < targetUnitIndex; i++)val /= unit;
227
+ if (includeUnits) return `${val.toFixed(decimals)} ${targetUnit}`;
228
+ return `${val.toFixed(decimals)}`;
229
+ }
230
+ function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
231
+ return [
232
+ ...crypto.getRandomValues(new Uint8Array(len))
233
+ ].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
234
+ }
235
+ class request {
236
+ auth(token) {
237
+ this.opts.headers.Authorization = `Bearer ${token}`;
238
+ return this;
239
+ }
240
+ basicAuth(username, password) {
241
+ this.opts.headers.Authorization = 'Basic ' + btoa(username + ':' + password);
242
+ return this;
243
+ }
244
+ body(body) {
245
+ this.opts.body = body;
246
+ return this;
247
+ }
248
+ async do({ path, overrideBody, overrideSuccess }) {
249
+ if (this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
250
+ const body = overrideBody || this.opts.body;
251
+ const res = await fetch(`${this.opts.url}${path}`, {
252
+ method: this.opts.method,
253
+ headers: {
254
+ accept: 'application/json',
255
+ 'content-type': 'application/json',
256
+ ...this.opts.headers
257
+ },
258
+ body: JSON.stringify(body)
259
+ });
260
+ const success = overrideSuccess || this.opts.success;
261
+ if (res.status !== success) {
262
+ const body = await res.json();
263
+ throw new Error(`bad response ${res.status} !== ${this.opts.success}, body: ${body}`);
264
+ }
265
+ return await res.json();
266
+ }
267
+ constructor(opts){
268
+ utils_define_property(this, "opts", void 0);
269
+ if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
270
+ if (!opts.url) throw new Error('must provide url');
271
+ this.opts = opts;
272
+ if (!this.opts.success) this.opts.success = 200;
273
+ if (!this.opts.method) this.opts.method = 'GET';
274
+ if (!this.opts.headers) this.opts.headers = {};
275
+ }
276
+ }
277
+ export { dom_namespaceObject as dom, style_namespaceObject as style, utils_namespaceObject as utils };
@@ -0,0 +1,14 @@
1
+ /***************
2
+ * Style *
3
+ **************/
4
+ export declare function switchTheme(theme: string): void;
5
+ export declare function cssVar(name: string): string;
6
+ export declare function isDarkMode(): boolean;
7
+ export declare function onDarkMode(cb: (dark: boolean) => void): void;
8
+ export type Color = {
9
+ red: number;
10
+ green: number;
11
+ blue: number;
12
+ alpha: number;
13
+ };
14
+ export declare function gradient(start: Color, end: Color, value: number): string;
@@ -0,0 +1,48 @@
1
+ /***************
2
+ * Utils *
3
+ **************/
4
+ /**
5
+ * Converts bytes to a human-readable string representation
6
+ *
7
+ * @param {number} bytes - The number of bytes to convert
8
+ * @param {Object} options - Optional configuration
9
+ * @param {boolean} options.useSI - If true, use SI units (KB, MB, GB) with powers of 1000
10
+ * If false, use binary units (KiB, MiB, GiB) with powers of 1024
11
+ * @param {number} options.decimals - Number of decimal places to include (default: 2)
12
+ * @return {string} Human-readable representation (e.g., "4.2 MB" or "3.7 GiB")
13
+ */
14
+ export type Options = {
15
+ useSI?: boolean;
16
+ decimals?: number;
17
+ includeUnits?: boolean;
18
+ targetUnit?: string;
19
+ };
20
+ export declare function bytesToHuman(bytes: string, options?: Options): string;
21
+ /**
22
+ * Generates a random id of length len using provided alphabet
23
+ * @param len - The length of the string to generate
24
+ * @param [alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'] - The set of characters to use
25
+ * @return A new random id
26
+ */
27
+ export declare function genID(len?: number, alphabet?: string): string;
28
+ export type RequestOptions = {
29
+ url: string;
30
+ type: 'json';
31
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
32
+ headers: Record<string, string>;
33
+ auth: string;
34
+ body: Object;
35
+ success: number;
36
+ };
37
+ export declare class request {
38
+ opts: RequestOptions;
39
+ constructor(opts: RequestOptions);
40
+ auth(token: string): this;
41
+ basicAuth(username: string, password: string): this;
42
+ body(body: Object): this;
43
+ do({ path, overrideBody, overrideSuccess, }: {
44
+ path: string;
45
+ overrideBody: Object;
46
+ overrideSuccess: number;
47
+ }): Promise<any>;
48
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@taybart/corvid",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.js"
9
+ }
10
+ },
11
+ "module": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "build": "rslib build",
18
+ "dev": "rslib build --watch"
19
+ },
20
+ "devDependencies": {
21
+ "@rslib/core": "^0.6.2",
22
+ "@types/node": "^22.8.1",
23
+ "typescript": "^5.8.3"
24
+ }
25
+ }