@taybart/corvid 0.0.3 → 0.0.5

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 CHANGED
@@ -1,39 +1,42 @@
1
- /***************
2
- * DOM *
1
+ /****************
2
+ * DOM *
3
3
  ***************/
4
+ /*** window ***/
5
+ /**
6
+ * onDomContentLoaded
7
+ */
4
8
  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
- }
9
+ /*** element ***/
10
+ type elOpts = {
11
+ query?: string;
12
+ type?: string;
13
+ content?: any;
14
+ parent?: HTMLElement;
15
+ create?: boolean;
16
+ };
16
17
  export declare class el {
17
18
  el: HTMLElement | null;
18
- constructor({ id, type, content, }: {
19
- id: string;
20
- type: string;
21
- content?: any;
22
- });
19
+ constructor(opts: string | elOpts);
20
+ /*** get ***/
21
+ value(): string;
22
+ /*** set ***/
23
+ parent(parent: HTMLElement): this;
23
24
  child(ch: HTMLElement): this;
24
- inner(content: any): this;
25
+ inner(content: any, { force, text }?: {
26
+ force?: boolean;
27
+ text?: boolean;
28
+ }): this;
25
29
  src(url: string): this;
26
30
  style(style: Object | string): this;
27
- onClick(cb: (ev: Event) => void): this;
28
31
  listen(event: string, cb: (ev: Event) => void): this;
32
+ onClick(cb: (ev: Event) => void): this;
29
33
  }
34
+ /*** url params ***/
30
35
  export declare class params {
31
36
  params: URLSearchParams;
32
- constructor();
37
+ constructor(p?: Object);
33
38
  set(p: Object): this;
34
39
  toString(): string;
40
+ static render(p: Object): string;
35
41
  }
36
- export declare const ls: {
37
- get(k: string): string | null;
38
- set(k: string, v: any): void;
39
- };
42
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * as dom from './dom';
2
2
  export * as style from './style';
3
- export * as utils from './utils';
3
+ export * as network from './network';
4
+ export * as ls from './local_storage';
5
+ export * from './utils';
package/dist/index.js CHANGED
@@ -23,11 +23,8 @@ var __webpack_require__ = {};
23
23
  var dom_namespaceObject = {};
24
24
  __webpack_require__.r(dom_namespaceObject);
25
25
  __webpack_require__.d(dom_namespaceObject, {
26
- btn: ()=>btn,
27
26
  el: ()=>el,
28
- form: ()=>dom_form,
29
- ls: ()=>ls,
30
- params: ()=>params,
27
+ params: ()=>dom_params,
31
28
  ready: ()=>ready
32
29
  });
33
30
  var style_namespaceObject = {};
@@ -39,13 +36,18 @@ __webpack_require__.d(style_namespaceObject, {
39
36
  onDarkMode: ()=>onDarkMode,
40
37
  switchTheme: ()=>switchTheme
41
38
  });
42
- var utils_namespaceObject = {};
43
- __webpack_require__.r(utils_namespaceObject);
44
- __webpack_require__.d(utils_namespaceObject, {
45
- bytesToHuman: ()=>bytesToHuman,
46
- genID: ()=>genID,
39
+ var network_namespaceObject = {};
40
+ __webpack_require__.r(network_namespaceObject);
41
+ __webpack_require__.d(network_namespaceObject, {
47
42
  request: ()=>request
48
43
  });
44
+ var local_storage_namespaceObject = {};
45
+ __webpack_require__.r(local_storage_namespaceObject);
46
+ __webpack_require__.d(local_storage_namespaceObject, {
47
+ get: ()=>get,
48
+ listen: ()=>listen,
49
+ set: ()=>set
50
+ });
49
51
  function _define_property(obj, key, value) {
50
52
  if (key in obj) Object.defineProperty(obj, key, {
51
53
  value: value,
@@ -59,47 +61,32 @@ function _define_property(obj, key, value) {
59
61
  function ready(cb) {
60
62
  window.addEventListener('DOMContentLoaded', cb);
61
63
  }
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
- }
64
+ class el {
65
+ value() {
66
+ if (this.el && this.el instanceof HTMLInputElement) return this.el.value;
67
+ return '';
74
68
  }
75
- }
76
- class dom_form {
77
- byID(id) {
78
- this.el = document.getElementById(id);
69
+ parent(parent) {
70
+ if (!this.el) throw new Error('no element');
71
+ parent.appendChild(this.el);
79
72
  return this;
80
73
  }
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
74
  child(ch) {
93
75
  var _this_el;
76
+ if (!this.el) throw new Error('no element');
94
77
  null == (_this_el = this.el) || _this_el.appendChild(ch);
95
78
  return this;
96
79
  }
97
- inner(content) {
98
- if (this.el) this.el.innerHTML = content;
80
+ inner(content, { force = false, text = false } = {}) {
81
+ if (!this.el) throw new Error('no element');
82
+ if (this.el instanceof HTMLIFrameElement && !force) this.el.src = content;
83
+ else if (this.el instanceof HTMLInputElement && !force) this.el.value = content;
84
+ else if (text) this.el.textContent = content;
85
+ else this.el.innerHTML = content;
99
86
  return this;
100
87
  }
101
88
  src(url) {
102
- if (this.el instanceof HTMLIFrameElement) this.el.src = url;
89
+ if (this.el instanceof HTMLIFrameElement || this.el instanceof HTMLImageElement) this.el.src = url;
103
90
  return this;
104
91
  }
105
92
  style(style) {
@@ -113,25 +100,31 @@ class el {
113
100
  }
114
101
  return this;
115
102
  }
116
- onClick(cb) {
117
- var _this_el;
118
- null == (_this_el = this.el) || _this_el.addEventListener('click', cb);
119
- return this;
120
- }
121
103
  listen(event, cb) {
122
- var _this_el;
123
- null == (_this_el = this.el) || _this_el.addEventListener(event, cb);
104
+ if (!this.el) throw new Error('no element');
105
+ this.el.addEventListener(event, cb);
124
106
  return this;
125
107
  }
126
- constructor({ id, type, content }){
108
+ onClick(cb) {
109
+ return this.listen('click', cb);
110
+ }
111
+ constructor(opts){
127
112
  _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');
113
+ if ('string' == typeof opts) {
114
+ this.el = document.querySelector(opts);
115
+ return;
116
+ }
117
+ const { query, type, content, parent, create } = opts;
118
+ if (query) {
119
+ this.el = document.querySelector(query);
120
+ if (!this.el && type && create) this.el = document.createElement(type);
121
+ } else if (type) this.el = document.createElement(type);
122
+ else throw new Error('no query or type provided');
131
123
  if (this.el && content) this.el.innerHTML = content;
124
+ if (this.el && parent) parent.appendChild(this.el);
132
125
  }
133
126
  }
134
- class params {
127
+ class dom_params {
135
128
  set(p) {
136
129
  for (let [k, v] of Object.entries(p))this.params.set(k, v);
137
130
  return this;
@@ -139,22 +132,18 @@ class params {
139
132
  toString() {
140
133
  return this.params.toString();
141
134
  }
142
- constructor(){
135
+ static render(p) {
136
+ const params = new URLSearchParams();
137
+ if (p) for (let [k, v] of Object.entries(p))params.set(k, v);
138
+ return params.toString();
139
+ }
140
+ constructor(p){
143
141
  _define_property(this, "params", void 0);
144
142
  this.params = new URLSearchParams();
143
+ if (p) for (let [k, v] of Object.entries(p))this.params.set(k, v);
144
+ return this;
145
145
  }
146
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
147
  function cssVar(name) {
159
148
  const style = window.getComputedStyle(document.body);
160
149
  return style.getPropertyValue(name);
@@ -167,15 +156,17 @@ function onDarkMode(cb) {
167
156
  cb(ev.matches);
168
157
  });
169
158
  }
159
+ function switchTheme(theme) {
160
+ document.documentElement.setAttribute('data-theme', theme);
161
+ }
170
162
  function gradient(start, end, value) {
171
163
  value = Math.max(0, Math.min(100, value));
172
164
  const red = Math.round(start.red + (end.red - start.red) * value / 100);
173
165
  const green = Math.round(start.green + (end.green - start.green) * value / 100);
174
166
  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})`;
167
+ return `rgb(${red}, ${green}, ${blue})`;
177
168
  }
178
- function utils_define_property(obj, key, value) {
169
+ function network_define_property(obj, key, value) {
179
170
  if (key in obj) Object.defineProperty(obj, key, {
180
171
  value: value,
181
172
  enumerable: true,
@@ -185,6 +176,74 @@ function utils_define_property(obj, key, value) {
185
176
  else obj[key] = value;
186
177
  return obj;
187
178
  }
179
+ class request {
180
+ auth(token) {
181
+ this.opts.headers.Authorization = `Bearer ${token}`;
182
+ return this;
183
+ }
184
+ basicAuth(username, password) {
185
+ this.opts.headers.Authorization = 'Basic ' + btoa(username + ':' + password);
186
+ return this;
187
+ }
188
+ body(body) {
189
+ this.opts.body = body;
190
+ return this;
191
+ }
192
+ async do({ path, overrideBody, overrideSuccess } = {}) {
193
+ if (this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
194
+ const body = overrideBody || this.opts.body;
195
+ let url = this.opts.url;
196
+ if (path) url = `${this.opts.url}${path}`;
197
+ const res = await fetch(url, {
198
+ method: this.opts.method,
199
+ headers: {
200
+ accept: 'application/json',
201
+ 'content-type': 'application/json',
202
+ ...this.opts.headers
203
+ },
204
+ body: JSON.stringify(body)
205
+ });
206
+ const success = overrideSuccess || this.opts.success;
207
+ if (res.status !== success) {
208
+ const body = await res.json();
209
+ throw new Error(`bad response ${res.status} !== ${this.opts.success}, body: ${body}`);
210
+ }
211
+ return await res.json();
212
+ }
213
+ constructor(opts){
214
+ network_define_property(this, "opts", void 0);
215
+ if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
216
+ if (!opts.url) throw new Error('must provide url');
217
+ this.opts = opts;
218
+ if (!this.opts.success) this.opts.success = 200;
219
+ if (!this.opts.method) this.opts.method = 'GET';
220
+ if (!this.opts.headers) this.opts.headers = {};
221
+ }
222
+ }
223
+ function get(key) {
224
+ return localStorage.getItem(key);
225
+ }
226
+ function set(key, value, broadcast = false) {
227
+ const prev = get(key);
228
+ if (prev !== value || broadcast) {
229
+ const event = new CustomEvent('@corvid/ls-update', {
230
+ detail: {
231
+ key,
232
+ value
233
+ }
234
+ });
235
+ document.dispatchEvent(event);
236
+ }
237
+ localStorage.setItem(key, value);
238
+ }
239
+ function listen(key, cb) {
240
+ document.addEventListener('@corvid/ls-update', (ev)=>{
241
+ if (ev.detail.key === key || '*' === key) cb({
242
+ key: ev.detail.key,
243
+ value: ev.detail.value
244
+ });
245
+ });
246
+ }
188
247
  function bytesToHuman(bytes, options = {}) {
189
248
  const { useSI = false, decimals = 2, includeUnits = true, targetUnit = null } = options;
190
249
  const unit = useSI ? 1000 : 1024;
@@ -232,48 +291,4 @@ function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg
232
291
  ...crypto.getRandomValues(new Uint8Array(len))
233
292
  ].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
234
293
  }
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
- let url = this.opts.url;
252
- if (path) url = `${this.opts.url}${path}`;
253
- const res = await fetch(url, {
254
- method: this.opts.method,
255
- headers: {
256
- accept: 'application/json',
257
- 'content-type': 'application/json',
258
- ...this.opts.headers
259
- },
260
- body: JSON.stringify(body)
261
- });
262
- const success = overrideSuccess || this.opts.success;
263
- if (res.status !== success) {
264
- const body = await res.json();
265
- throw new Error(`bad response ${res.status} !== ${this.opts.success}, body: ${body}`);
266
- }
267
- return await res.json();
268
- }
269
- constructor(opts){
270
- utils_define_property(this, "opts", void 0);
271
- if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
272
- if (!opts.url) throw new Error('must provide url');
273
- this.opts = opts;
274
- if (!this.opts.success) this.opts.success = 200;
275
- if (!this.opts.method) this.opts.method = 'GET';
276
- if (!this.opts.headers) this.opts.headers = {};
277
- }
278
- }
279
- export { dom_namespaceObject as dom, style_namespaceObject as style, utils_namespaceObject as utils };
294
+ export { bytesToHuman, dom_namespaceObject as dom, genID, local_storage_namespaceObject as ls, network_namespaceObject as network, style_namespaceObject as style };
@@ -0,0 +1,6 @@
1
+ export declare function get(key: string): any;
2
+ export declare function set(key: string, value: any, broadcast?: boolean): void;
3
+ export declare function listen(key: string, cb: (update: {
4
+ key: string;
5
+ value: any;
6
+ }) => void): void;
@@ -0,0 +1,21 @@
1
+ export type requestOpts = {
2
+ url: string;
3
+ type: 'json';
4
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
5
+ headers: Record<string, string>;
6
+ auth: string;
7
+ body: Object;
8
+ success: number;
9
+ };
10
+ export declare class request {
11
+ opts: requestOpts;
12
+ constructor(opts: requestOpts);
13
+ auth(token: string): this;
14
+ basicAuth(username: string, password: string): this;
15
+ body(body: Object): this;
16
+ do({ path, overrideBody, overrideSuccess, }?: {
17
+ path?: string;
18
+ overrideBody?: Object;
19
+ overrideSuccess?: number;
20
+ }): Promise<any>;
21
+ }
package/dist/style.d.ts CHANGED
@@ -1,14 +1,31 @@
1
1
  /***************
2
2
  * Style *
3
3
  **************/
4
- export declare function switchTheme(theme: string): void;
5
4
  export declare function cssVar(name: string): string;
5
+ /**
6
+ * Check if the current theme is dark
7
+ */
6
8
  export declare function isDarkMode(): boolean;
7
- export declare function onDarkMode(cb: (dark: boolean) => void): void;
8
- export type Color = {
9
+ /**
10
+ * Listen for changes in the dark mode preference
11
+ * @param cb - Callback function when theme changes
12
+ */
13
+ export declare function onDarkMode(cb: (isDark: boolean) => void): void;
14
+ /**
15
+ * Switch between light and dark theme
16
+ * @param theme - 'light' or 'dark'
17
+ */
18
+ export declare function switchTheme(theme: string): void;
19
+ /**
20
+ * Calculate a color gradient
21
+ * @param start - The starting color
22
+ * @param end - The ending color
23
+ * @param value - The value to interpolate between the start and end colors 0 <= value <= 100
24
+ * @return rgb string
25
+ */
26
+ export type color = {
9
27
  red: number;
10
28
  green: number;
11
29
  blue: number;
12
- alpha: number;
13
30
  };
14
- export declare function gradient(start: Color, end: Color, value: number): string;
31
+ export declare function gradient(start: color, end: color, value: number): string;
package/dist/utils.d.ts CHANGED
@@ -4,20 +4,20 @@
4
4
  /**
5
5
  * Converts bytes to a human-readable string representation
6
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
7
+ * @param bytes - The number of bytes to convert
8
+ * @param options - Optional configuration
9
+ * @param options.useSI - If true, use SI units (KB, MB, GB) with powers of 1000
10
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")
11
+ * @param options.decimals - Number of decimal places to include (default: 2)
12
+ * @return Human-readable representation (e.g., "4.2 MB" or "3.7 GiB")
13
13
  */
14
- export type Options = {
14
+ export type bytesOptions = {
15
15
  useSI?: boolean;
16
16
  decimals?: number;
17
17
  includeUnits?: boolean;
18
18
  targetUnit?: string;
19
19
  };
20
- export declare function bytesToHuman(bytes: string, options?: Options): string;
20
+ export declare function bytesToHuman(bytes: string, options?: bytesOptions): string;
21
21
  /**
22
22
  * Generates a random id of length len using provided alphabet
23
23
  * @param len - The length of the string to generate
@@ -25,24 +25,3 @@ export declare function bytesToHuman(bytes: string, options?: Options): string;
25
25
  * @return A new random id
26
26
  */
27
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taybart/corvid",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {