@taybart/corvid 0.1.1 → 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.js CHANGED
@@ -55,7 +55,8 @@ __webpack_require__.r(local_storage_namespaceObject);
55
55
  __webpack_require__.d(local_storage_namespaceObject, {
56
56
  get: ()=>get,
57
57
  listen: ()=>listen,
58
- set: ()=>set
58
+ set: ()=>set,
59
+ setObj: ()=>setObj
59
60
  });
60
61
  function bytesToHuman(bytes, options = {}) {
61
62
  const { useSI = false, decimals = 2, includeUnits = true, targetUnit = null } = options;
@@ -221,6 +222,8 @@ class dom_el {
221
222
  return this;
222
223
  }
223
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;
224
227
  this.log.warn(`element (${this.query}) does not contain value, returning empty string`);
225
228
  return '';
226
229
  }
@@ -281,9 +284,6 @@ class dom_el {
281
284
  listen(event, cb) {
282
285
  return this.on(event, cb);
283
286
  }
284
- onClick(cb) {
285
- return this.on('click', cb);
286
- }
287
287
  constructor(opts, verbose = false){
288
288
  dom_define_property(this, "el", void 0);
289
289
  dom_define_property(this, "query", '');
@@ -304,6 +304,7 @@ class dom_el {
304
304
  this.log.debug(`using existing element: ${element}`);
305
305
  this.el = element;
306
306
  } else if (type) {
307
+ this.query = type;
307
308
  this.log.debug(`creating element: ${type}`);
308
309
  this.el = document.createElement(type);
309
310
  } else throw new Error('no query or type provided');
@@ -529,6 +530,7 @@ function get(key) {
529
530
  return localStorage.getItem(key);
530
531
  }
531
532
  function set(key, value, broadcast = false) {
533
+ if ('object' == typeof key) return void setObj(key, value, broadcast);
532
534
  const prev = get(key);
533
535
  if (prev !== value || broadcast) {
534
536
  const event = new CustomEvent('@corvid/ls-update', {
@@ -541,6 +543,21 @@ function set(key, value, broadcast = false) {
541
543
  }
542
544
  localStorage.setItem(key, value);
543
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
+ }
544
561
  function listen(key, cb) {
545
562
  document.addEventListener('@corvid/ls-update', (ev)=>{
546
563
  if (ev.detail.key === key || '*' === key) {
@@ -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.1",
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
  },