@taybart/corvid 0.1.0 → 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.
package/README.md CHANGED
@@ -2,5 +2,105 @@
2
2
 
3
3
  fear the crow
4
4
 
5
+ ![crowtein](https://github.com/user-attachments/assets/b3bbf267-d7b0-4116-80a5-b932b409a111)
5
6
 
6
- ![crowtein_2](https://github.com/user-attachments/assets/b3bbf267-d7b0-4116-80a5-b932b409a111)
7
+ ## Usage
8
+
9
+ Non-exhastive list of features
10
+
11
+ ### DOM
12
+
13
+ ```js
14
+ import { dom } from '@taybart/corvid'
15
+
16
+ dom.ready(() => {
17
+ // query for existing element
18
+ const username = new dom.el('#username')
19
+ // listen for events
20
+ username.on('click', () => {
21
+ // set style, will kebab keys backgroundColor -> background-color
22
+ username.style({ color: 'red', backgroundColor: 'yellow' })
23
+ // set/get content
24
+ username.content(`evil: ${username.value()}`)
25
+ }))
26
+
27
+
28
+ // create new elements
29
+ new dom.el({
30
+ type: 'div',
31
+ id: 'hair-color',
32
+ class: 'hair user-info',
33
+ content: 'blue',
34
+ // will append element to username
35
+ parent: username,
36
+ })
37
+
38
+ // listen for keys and check for modifiers
39
+ dom.onKey('E', ({ ctrl, alt, meta, shift }) => {
40
+ console.log('E pressed')
41
+ })
42
+ })
43
+ ```
44
+
45
+ ### LocalStorage
46
+
47
+ ```js
48
+ import { ls, dom } from '@taybart/corvid'
49
+
50
+ dom.ready(() => {
51
+ const hpStat = new dom.el({ query: '#stat-hp', content: ls.get('stats.hp') })
52
+ // set element content when localstorage changes
53
+ ls.listen('stats.hp', hpStat)
54
+ // or just a callback
55
+ ls.listen('stats.hp', ({ key, value }) => {
56
+ console.log(`health is now ${value}`)
57
+ })
58
+ // set a value (required if listening for events)
59
+ ls.set('stats.hp', ls.get('stats.hp') - 1))
60
+ // set a flattened object, will update "stats.hp" and "stats.attack"
61
+ ls.set({ stats: { hp: 100, attack: 10 } })
62
+ })
63
+ ```
64
+
65
+ ### Network
66
+
67
+ ```js
68
+ import { network } '@taybart/corvid'
69
+
70
+ // create an api client
71
+ const api = network.create({
72
+ url: 'https://api.example.com',
73
+ credentials: 'include',
74
+ success: 250, // odd success code
75
+ // corvid params, string, or custom object that has .toString() and renders url safe params
76
+ params: new network.params({hello: 'corvid'})
77
+ })
78
+
79
+ // make a request
80
+ const { username } = await api.do({
81
+ path: '/users/1',
82
+ override: {
83
+ params: network.params.render({hello: 'world!'}) , // only for this request
84
+ },
85
+ })
86
+
87
+
88
+ ```
89
+
90
+ ### Styles
91
+
92
+ ```js
93
+ import { style } '@taybart/corvid'
94
+
95
+ // query css media prefers-color-scheme
96
+ if (style.isDarkMode()) {
97
+ console.log('dark mode')
98
+ }
99
+ // listen for theme switch
100
+ style.onDarkMode((isDark) => {
101
+ // set document attribute 'data-theme'
102
+ style.switchTheme(isDark ? 'light' : 'dark')
103
+ // get css variables
104
+ console.log(`is dark mode: ${isDark} and background is ${style.cssVar('--color-bg')`)
105
+ })
106
+ ```
package/dist/dom.d.ts CHANGED
@@ -46,6 +46,5 @@ export declare class el {
46
46
  /*** Events ***/
47
47
  on(event: string, cb: (ev: Event) => void): this;
48
48
  listen(event: string, cb: (ev: Event) => void): this;
49
- onClick(cb: (ev: Event) => void): this;
50
49
  }
51
50
  export {};
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * as dom from './dom';
2
2
  export * as style from './style';
3
3
  export * as network from './network';
4
4
  export * as ls from './local_storage';
5
+ export * as strings from './strings';
5
6
  export * from './utils';
package/dist/index.js CHANGED
@@ -20,6 +20,12 @@ var __webpack_require__ = {};
20
20
  });
21
21
  };
22
22
  })();
23
+ var strings_namespaceObject = {};
24
+ __webpack_require__.r(strings_namespaceObject);
25
+ __webpack_require__.d(strings_namespaceObject, {
26
+ bytesToHuman: ()=>bytesToHuman,
27
+ toKebab: ()=>toKebab
28
+ });
23
29
  var dom_namespaceObject = {};
24
30
  __webpack_require__.r(dom_namespaceObject);
25
31
  __webpack_require__.d(dom_namespaceObject, {
@@ -49,8 +55,51 @@ __webpack_require__.r(local_storage_namespaceObject);
49
55
  __webpack_require__.d(local_storage_namespaceObject, {
50
56
  get: ()=>get,
51
57
  listen: ()=>listen,
52
- set: ()=>set
58
+ set: ()=>set,
59
+ setObj: ()=>setObj
53
60
  });
61
+ function bytesToHuman(bytes, options = {}) {
62
+ const { useSI = false, decimals = 2, includeUnits = true, targetUnit = null } = options;
63
+ const unit = useSI ? 1000 : 1024;
64
+ const units = useSI ? [
65
+ 'B',
66
+ 'kB',
67
+ 'MB',
68
+ 'GB',
69
+ 'TB',
70
+ 'PB',
71
+ 'EB',
72
+ 'ZB',
73
+ 'YB'
74
+ ] : [
75
+ 'B',
76
+ 'KiB',
77
+ 'MiB',
78
+ 'GiB',
79
+ 'TiB',
80
+ 'PiB',
81
+ 'EiB',
82
+ 'ZiB',
83
+ 'YiB'
84
+ ];
85
+ if (null === targetUnit) {
86
+ let val = parseInt(bytes, 10);
87
+ if (Math.abs(val) < unit) return `${bytes} B`;
88
+ let u = 0;
89
+ while(Math.abs(val) >= unit && u < units.length - 1){
90
+ val /= unit;
91
+ u++;
92
+ }
93
+ if (includeUnits) return `${val.toFixed(decimals)} ${units[u]}`;
94
+ return `${val.toFixed(decimals)}`;
95
+ }
96
+ const targetUnitIndex = units.indexOf(targetUnit);
97
+ if (-1 === targetUnitIndex) throw new Error(`Invalid unit: ${targetUnit}. Valid units are: ${units.join(', ')}`);
98
+ let val = parseInt(bytes, 10);
99
+ for(let i = 0; i < targetUnitIndex; i++)val /= unit;
100
+ if (includeUnits) return `${val.toFixed(decimals)} ${targetUnit}`;
101
+ return `${val.toFixed(decimals)}`;
102
+ }
54
103
  function toKebab(str) {
55
104
  return str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, (s, ofs)=>(ofs ? '-' : '') + s.toLowerCase());
56
105
  }
@@ -129,19 +178,16 @@ class logger {
129
178
  _define_property(this, "prefix", void 0);
130
179
  this.level = level;
131
180
  this.prefix = prefix ? `(${prefix}):` : ':';
132
- if (-1 === this.level) {
133
- const methodsToMock = [
134
- 'error',
135
- 'warn',
136
- 'info',
137
- 'debug',
138
- 'trace',
139
- 'log'
140
- ];
141
- methodsToMock.forEach((methodName)=>{
142
- this[methodName] = ()=>{};
143
- });
144
- }
181
+ if (-1 === this.level) [
182
+ 'error',
183
+ 'warn',
184
+ 'info',
185
+ 'debug',
186
+ 'trace',
187
+ 'log'
188
+ ].forEach((methodName)=>{
189
+ this[methodName] = ()=>{};
190
+ });
145
191
  }
146
192
  }
147
193
  function dom_define_property(obj, key, value) {
@@ -176,6 +222,8 @@ class dom_el {
176
222
  return this;
177
223
  }
178
224
  if ('value' in this.el) return this.el.value;
225
+ if ('innerText' in this.el) return this.el.innerText;
226
+ if ('innerHTML' in this.el) return this.el.innerHTML;
179
227
  this.log.warn(`element (${this.query}) does not contain value, returning empty string`);
180
228
  return '';
181
229
  }
@@ -236,9 +284,6 @@ class dom_el {
236
284
  listen(event, cb) {
237
285
  return this.on(event, cb);
238
286
  }
239
- onClick(cb) {
240
- return this.on('click', cb);
241
- }
242
287
  constructor(opts, verbose = false){
243
288
  dom_define_property(this, "el", void 0);
244
289
  dom_define_property(this, "query", '');
@@ -259,6 +304,7 @@ class dom_el {
259
304
  this.log.debug(`using existing element: ${element}`);
260
305
  this.el = element;
261
306
  } else if (type) {
307
+ this.query = type;
262
308
  this.log.debug(`creating element: ${type}`);
263
309
  this.el = document.createElement(type);
264
310
  } else throw new Error('no query or type provided');
@@ -484,6 +530,7 @@ function get(key) {
484
530
  return localStorage.getItem(key);
485
531
  }
486
532
  function set(key, value, broadcast = false) {
533
+ if ('object' == typeof key) return void setObj(key, value, broadcast);
487
534
  const prev = get(key);
488
535
  if (prev !== value || broadcast) {
489
536
  const event = new CustomEvent('@corvid/ls-update', {
@@ -496,6 +543,21 @@ function set(key, value, broadcast = false) {
496
543
  }
497
544
  localStorage.setItem(key, value);
498
545
  }
546
+ function setObj(update, prefix, broadcast = false) {
547
+ const flatten = (ob)=>{
548
+ const ret = {};
549
+ for(let i in ob)if (ob.hasOwnProperty(i)) if ('object' == typeof ob[i] && null !== ob[i]) {
550
+ const flat = flatten(ob[i]);
551
+ for(let x in flat)if (flat.hasOwnProperty(x)) ret[`${i}.${x}`] = flat[x];
552
+ } else ret[i] = ob[i];
553
+ return ret;
554
+ };
555
+ for (let [k, v] of Object.entries(flatten(update))){
556
+ let key = k;
557
+ if (prefix) key = `${prefix}.${k}`;
558
+ set(key, v, broadcast);
559
+ }
560
+ }
499
561
  function listen(key, cb) {
500
562
  document.addEventListener('@corvid/ls-update', (ev)=>{
501
563
  if (ev.detail.key === key || '*' === key) {
@@ -510,4 +572,4 @@ function listen(key, cb) {
510
572
  }
511
573
  });
512
574
  }
513
- export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, style_namespaceObject as style };
575
+ export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, strings_namespaceObject as strings, style_namespaceObject as style };
@@ -1,6 +1,7 @@
1
1
  import { el } from './dom';
2
2
  export declare function get(key: string): any;
3
- export declare function set(key: string, value: any, broadcast?: boolean): void;
3
+ export declare function set(key: string | object, value: any, broadcast?: boolean): void;
4
+ export declare function setObj(update: object, prefix?: string, broadcast?: boolean): void;
4
5
  export declare function listen(key: string, cb: (update: {
5
6
  key: string;
6
7
  value: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taybart/corvid",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
@@ -15,7 +15,12 @@
15
15
  "dist"
16
16
  ],
17
17
  "devDependencies": {
18
+ "@happy-dom/global-registrator": "^17.4.7",
18
19
  "@rslib/core": "^0.6.2",
20
+ "@testing-library/dom": "^10.4.0",
21
+ "@testing-library/jest-dom": "^6.6.3",
22
+ "@types/bun": "^1.2.13",
23
+ "@types/jsdom": "^21.1.7",
19
24
  "@types/node": "^22.8.1",
20
25
  "typescript": "^5.8.3"
21
26
  },
package/dist/crypto.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export declare class EncryptionHelper {
2
- private iv?;
3
- private aesKey?;
4
- private encryptionKey?;
5
- constructor(publicKey: string);
6
- encrypt(plaintext: string): Promise<string>;
7
- key(): Promise<string>;
8
- }