@uistate/aliases 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ajdin Imsirovic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,348 @@
1
+ # @uistate/aliases
2
+
3
+ **Ergonomic DOM aliases that stay close to the metal.**
4
+
5
+ Thin, transparent wrappers for verbose DOM APIs. Zero abstraction penalty. You're still writing vanilla JavaScript—just faster.
6
+
7
+ ## Philosophy
8
+
9
+ Every function is a one-line wrapper with 1:1 mapping to platform primitives. No magic, no lock-in, no framework churn. Just ergonomics.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @uistate/aliases
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```javascript
20
+ // Import what you need
21
+ import { $, mk, on, find } from '@uistate/aliases';
22
+
23
+ // Or import everything
24
+ import * as dom from '@uistate/aliases';
25
+ ```
26
+
27
+ ## Full Example
28
+
29
+ ```javascript
30
+ import { $, mk, on, find } from '@uistate/aliases';
31
+
32
+ const list = $('list');
33
+ const add = $('add');
34
+ let counter = 1;
35
+
36
+ on(add, 'click', () => {
37
+ const li = mk('li');
38
+ li.innerHTML = `Item ${counter++} <button class="remove">Remove</button>`;
39
+ list.append(li);
40
+ });
41
+
42
+ on(list, 'click', (e) => {
43
+ const btn = find(e.target, 'button.remove');
44
+ if (!btn) return;
45
+ const li = find(btn, 'li');
46
+ alert(`Removing ${li.textContent.replace(' Remove', '')}`);
47
+ li.remove();
48
+ });
49
+ ```
50
+
51
+ **Character Count Comparison:**
52
+ - Original: 449 characters
53
+ - Aliased: 329 characters
54
+ - **Savings: ~27% reduction**
55
+
56
+ ## API Reference
57
+
58
+ ### Selection
59
+
60
+ #### `$(id)`
61
+ Get element by ID.
62
+ ```javascript
63
+ const el = $('myId');
64
+ // document.getElementById('myId')
65
+ ```
66
+
67
+ #### `qs(selector, context?)`
68
+ Query selector (single element).
69
+ ```javascript
70
+ const btn = qs('.button');
71
+ const nested = qs('.child', parent);
72
+ // document.querySelector('.button')
73
+ ```
74
+
75
+ #### `qsa(selector, context?)`
76
+ Query selector all.
77
+ ```javascript
78
+ const items = qsa('.item');
79
+ // document.querySelectorAll('.item')
80
+ ```
81
+
82
+ #### `find(element, selector)`
83
+ Find closest ancestor matching selector.
84
+ ```javascript
85
+ const li = find(button, 'li');
86
+ // button.closest('li')
87
+ ```
88
+
89
+ ### Creation
90
+
91
+ #### `mk(tag)`
92
+ Create element.
93
+ ```javascript
94
+ const div = mk('div');
95
+ // document.createElement('div')
96
+ ```
97
+
98
+ #### `txt(content)`
99
+ Create text node.
100
+ ```javascript
101
+ const text = txt('Hello');
102
+ // document.createTextNode('Hello')
103
+ ```
104
+
105
+ #### `frag()`
106
+ Create document fragment.
107
+ ```javascript
108
+ const fragment = frag();
109
+ // document.createDocumentFragment()
110
+ ```
111
+
112
+ ### Events
113
+
114
+ #### `on(element, event, callback, options?)`
115
+ Add event listener.
116
+ ```javascript
117
+ on(button, 'click', handleClick);
118
+ on(input, 'change', handleChange, { passive: true });
119
+ // element.addEventListener(event, callback, options)
120
+ ```
121
+
122
+ #### `off(element, event, callback, options?)`
123
+ Remove event listener.
124
+ ```javascript
125
+ off(button, 'click', handleClick);
126
+ // element.removeEventListener(event, callback, options)
127
+ ```
128
+
129
+ #### `once(element, event, callback)`
130
+ Add one-time event listener.
131
+ ```javascript
132
+ once(button, 'click', handleClick);
133
+ // element.addEventListener(event, callback, { once: true })
134
+ ```
135
+
136
+ #### `emit(element, event, detail?)`
137
+ Dispatch custom event.
138
+ ```javascript
139
+ emit(element, 'custom', { data: 'value' });
140
+ // element.dispatchEvent(new CustomEvent(event, { detail }))
141
+ ```
142
+
143
+ ### Manipulation
144
+
145
+ #### `append(parent, ...children)`
146
+ Append children to parent.
147
+ ```javascript
148
+ append(list, item1, item2);
149
+ // parent.append(...children)
150
+ ```
151
+
152
+ #### `prepend(parent, ...children)`
153
+ Prepend children to parent.
154
+ ```javascript
155
+ prepend(list, item);
156
+ // parent.prepend(...children)
157
+ ```
158
+
159
+ #### `before(reference, ...nodes)`
160
+ Insert nodes before reference.
161
+ ```javascript
162
+ before(item, newItem);
163
+ // reference.before(...nodes)
164
+ ```
165
+
166
+ #### `after(reference, ...nodes)`
167
+ Insert nodes after reference.
168
+ ```javascript
169
+ after(item, newItem);
170
+ // reference.after(...nodes)
171
+ ```
172
+
173
+ #### `replace(old, ...nodes)`
174
+ Replace element with nodes.
175
+ ```javascript
176
+ replace(oldItem, newItem);
177
+ // old.replaceWith(...nodes)
178
+ ```
179
+
180
+ #### `remove(element)`
181
+ Remove element from DOM.
182
+ ```javascript
183
+ remove(item);
184
+ // element.remove()
185
+ ```
186
+
187
+ ### Attributes
188
+
189
+ #### `attr(element, name, value?)`
190
+ Get/set/remove attribute.
191
+ ```javascript
192
+ const href = attr(link, 'href'); // Get
193
+ attr(link, 'href', '/path'); // Set
194
+ attr(link, 'disabled', null); // Remove
195
+ ```
196
+
197
+ #### `data(element, key, value?)`
198
+ Get/set dataset attribute.
199
+ ```javascript
200
+ const id = data(element, 'id'); // Get
201
+ data(element, 'id', '123'); // Set
202
+ ```
203
+
204
+ ### Classes
205
+
206
+ #### `cls(element, ...classes)`
207
+ Add classes.
208
+ ```javascript
209
+ cls(element, 'active', 'highlight');
210
+ // element.classList.add(...classes)
211
+ ```
212
+
213
+ #### `uncls(element, ...classes)`
214
+ Remove classes.
215
+ ```javascript
216
+ uncls(element, 'active', 'highlight');
217
+ // element.classList.remove(...classes)
218
+ ```
219
+
220
+ #### `toggle(element, class, force?)`
221
+ Toggle class.
222
+ ```javascript
223
+ toggle(element, 'active');
224
+ toggle(element, 'visible', true);
225
+ // element.classList.toggle(class, force)
226
+ ```
227
+
228
+ #### `has(element, class)`
229
+ Check if element has class.
230
+ ```javascript
231
+ if (has(element, 'active')) { }
232
+ // element.classList.contains(class)
233
+ ```
234
+
235
+ ### Styles
236
+
237
+ #### `css(element, property, value?)`
238
+ Get/set styles.
239
+ ```javascript
240
+ const color = css(element, 'color'); // Get
241
+ css(element, 'color', 'red'); // Set
242
+ css(element, { color: 'red', fontSize: '16px' }); // Set multiple
243
+ ```
244
+
245
+ ### Traversal
246
+
247
+ #### `parent(element)`
248
+ Get parent element.
249
+ ```javascript
250
+ const p = parent(element);
251
+ // element.parentElement
252
+ ```
253
+
254
+ #### `children(element)`
255
+ Get child elements as array.
256
+ ```javascript
257
+ const kids = children(element);
258
+ // Array.from(element.children)
259
+ ```
260
+
261
+ #### `siblings(element)`
262
+ Get sibling elements.
263
+ ```javascript
264
+ const sibs = siblings(element);
265
+ ```
266
+
267
+ #### `next(element)`
268
+ Get next sibling element.
269
+ ```javascript
270
+ const nextEl = next(element);
271
+ // element.nextElementSibling
272
+ ```
273
+
274
+ #### `prev(element)`
275
+ Get previous sibling element.
276
+ ```javascript
277
+ const prevEl = prev(element);
278
+ // element.previousElementSibling
279
+ ```
280
+
281
+ ### Utilities
282
+
283
+ #### `ready(callback)`
284
+ Execute callback when DOM is ready.
285
+ ```javascript
286
+ ready(() => {
287
+ console.log('DOM ready');
288
+ });
289
+ ```
290
+
291
+ #### `clone(element, deep?)`
292
+ Clone element.
293
+ ```javascript
294
+ const copy = clone(element);
295
+ const deepCopy = clone(element, true);
296
+ // element.cloneNode(deep)
297
+ ```
298
+
299
+ #### `empty(element)`
300
+ Remove all children.
301
+ ```javascript
302
+ empty(element);
303
+ // element.innerHTML = ''
304
+ ```
305
+
306
+ ## Bundle Size
307
+
308
+ ~2-3kb minified + gzipped. Tree-shakeable—import only what you use.
309
+
310
+ ## TypeScript
311
+
312
+ Full TypeScript definitions included.
313
+
314
+ ```typescript
315
+ import { $, mk, on } from '@uistate/aliases';
316
+
317
+ const button = $('myButton'); // HTMLElement | null
318
+ const div = mk('div'); // HTMLDivElement
319
+ ```
320
+
321
+ ## Why @uistate/aliases?
322
+
323
+ ### ✅ What We Alias
324
+ - Verbose global functions: `document.getElementById` → `$`
325
+ - Repetitive API calls: `document.createElement` → `mk`
326
+ - Long method names: `addEventListener` → `on`
327
+
328
+ ### ❌ What We Don't Alias
329
+ - Property access: `e.target`, `el.textContent` (stay vanilla)
330
+ - Native methods: `remove()`, `replace()` (unless they're verbose)
331
+ - Standard operations: String/Array methods (use platform APIs)
332
+
333
+ ### Philosophy
334
+ Close to the metal, ergonomic by design. These aliases are:
335
+ - **Transparent**: 1:1 mapping to platform APIs
336
+ - **Educational**: Learn vanilla JS through usage
337
+ - **Optional**: No lock-in, adopt incrementally
338
+ - **Timeless**: Won't break when frameworks change
339
+
340
+ ## Ecosystem
341
+
342
+ Part of the **uistate** family:
343
+ - `@uistate/core` - State management
344
+ - `@uistate/aliases` - DOM ergonomics (this package)
345
+
346
+ ## License
347
+
348
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ // ============================================
2
+ // @uistate/aliases - TypeScript Definitions
3
+ // ============================================
4
+
5
+ // --- SELECTION ---
6
+ export function $(id: string): HTMLElement | null;
7
+ export function qs<E extends Element = Element>(sel: string, ctx?: Document | Element): E | null;
8
+ export function qsa<E extends Element = Element>(sel: string, ctx?: Document | Element): NodeListOf<E>;
9
+ export function find<E extends Element = Element>(el: Element, sel: string): E | null;
10
+
11
+ // --- CREATION ---
12
+ export function mk<K extends keyof HTMLElementTagNameMap>(tag: K): HTMLElementTagNameMap[K];
13
+ export function mk(tag: string): HTMLElement;
14
+ export function txt(content: string): Text;
15
+ export function frag(): DocumentFragment;
16
+
17
+ // --- EVENTS ---
18
+ export function on<K extends keyof HTMLElementEventMap>(
19
+ el: Element | Document | Window,
20
+ ev: K,
21
+ cb: (this: Element, ev: HTMLElementEventMap[K]) => any,
22
+ opts?: boolean | AddEventListenerOptions
23
+ ): void;
24
+ export function on(
25
+ el: Element | Document | Window,
26
+ ev: string,
27
+ cb: EventListenerOrEventListenerObject,
28
+ opts?: boolean | AddEventListenerOptions
29
+ ): void;
30
+
31
+ export function off<K extends keyof HTMLElementEventMap>(
32
+ el: Element | Document | Window,
33
+ ev: K,
34
+ cb: (this: Element, ev: HTMLElementEventMap[K]) => any,
35
+ opts?: boolean | EventListenerOptions
36
+ ): void;
37
+ export function off(
38
+ el: Element | Document | Window,
39
+ ev: string,
40
+ cb: EventListenerOrEventListenerObject,
41
+ opts?: boolean | EventListenerOptions
42
+ ): void;
43
+
44
+ export function once<K extends keyof HTMLElementEventMap>(
45
+ el: Element | Document | Window,
46
+ ev: K,
47
+ cb: (this: Element, ev: HTMLElementEventMap[K]) => any
48
+ ): void;
49
+ export function once(
50
+ el: Element | Document | Window,
51
+ ev: string,
52
+ cb: EventListenerOrEventListenerObject
53
+ ): void;
54
+
55
+ export function emit(el: Element, ev: string, detail?: any): void;
56
+
57
+ // --- MANIPULATION ---
58
+ export function append(parent: Element, ...children: (Node | string)[]): void;
59
+ export function prepend(parent: Element, ...children: (Node | string)[]): void;
60
+ export function before(ref: Element, ...nodes: (Node | string)[]): void;
61
+ export function after(ref: Element, ...nodes: (Node | string)[]): void;
62
+ export function replace(old: Element, ...nodes: (Node | string)[]): void;
63
+ export function remove(el: Element): void;
64
+
65
+ // --- ATTRIBUTES ---
66
+ export function attr(el: Element, name: string): string | null;
67
+ export function attr(el: Element, name: string, value: string | null): void;
68
+
69
+ export function data(el: HTMLElement, key: string): string | undefined;
70
+ export function data(el: HTMLElement, key: string, value: string): void;
71
+
72
+ // --- CLASSES ---
73
+ export function cls(el: Element, ...classes: string[]): void;
74
+ export function uncls(el: Element, ...classes: string[]): void;
75
+ export function toggle(el: Element, cls: string, force?: boolean): boolean;
76
+ export function has(el: Element, cls: string): boolean;
77
+
78
+ // --- STYLES ---
79
+ export function css(el: HTMLElement, prop: string): string;
80
+ export function css(el: HTMLElement, prop: string, value: string): void;
81
+ export function css(el: HTMLElement, prop: Partial<CSSStyleDeclaration>): void;
82
+
83
+ // --- TRAVERSAL ---
84
+ export function parent(el: Element): HTMLElement | null;
85
+ export function children(el: Element): Element[];
86
+ export function siblings(el: Element): Element[];
87
+ export function next(el: Element): Element | null;
88
+ export function prev(el: Element): Element | null;
89
+
90
+ // --- UTILITIES ---
91
+ export function ready(cb: () => void): void;
92
+ export function clone<T extends Node>(el: T, deep?: boolean): T;
93
+ export function empty(el: Element): void;
package/index.js ADDED
@@ -0,0 +1,73 @@
1
+ // ============================================
2
+ // @uistate/aliases - Ergonomic DOM Aliases
3
+ // ============================================
4
+
5
+ // --- SELECTION ---
6
+ export const $ = (id) => document.getElementById(id);
7
+ export const qs = (sel, ctx = document) => ctx.querySelector(sel);
8
+ export const qsa = (sel, ctx = document) => ctx.querySelectorAll(sel);
9
+ export const find = (el, sel) => el.closest(sel);
10
+
11
+ // --- CREATION ---
12
+ export const mk = (tag) => document.createElement(tag);
13
+ export const txt = (content) => document.createTextNode(content);
14
+ export const frag = () => document.createDocumentFragment();
15
+
16
+ // --- EVENTS ---
17
+ export const on = (el, ev, cb, opts) => el.addEventListener(ev, cb, opts);
18
+ export const off = (el, ev, cb, opts) => el.removeEventListener(ev, cb, opts);
19
+ export const once = (el, ev, cb) => el.addEventListener(ev, cb, { once: true });
20
+ export const emit = (el, ev, detail) => el.dispatchEvent(new CustomEvent(ev, { detail }));
21
+
22
+ // --- MANIPULATION ---
23
+ export const append = (parent, ...children) => parent.append(...children);
24
+ export const prepend = (parent, ...children) => parent.prepend(...children);
25
+ export const before = (ref, ...nodes) => ref.before(...nodes);
26
+ export const after = (ref, ...nodes) => ref.after(...nodes);
27
+ export const replace = (old, ...nodes) => old.replaceWith(...nodes);
28
+ export const remove = (el) => el.remove();
29
+
30
+ // --- ATTRIBUTES ---
31
+ export const attr = (el, name, value) => {
32
+ if (value === undefined) return el.getAttribute(name);
33
+ if (value === null) el.removeAttribute(name);
34
+ else el.setAttribute(name, value);
35
+ };
36
+
37
+ export const data = (el, key, value) => {
38
+ if (value === undefined) return el.dataset[key];
39
+ el.dataset[key] = value;
40
+ };
41
+
42
+ // --- CLASSES ---
43
+ export const cls = (el, ...classes) => el.classList.add(...classes);
44
+ export const uncls = (el, ...classes) => el.classList.remove(...classes);
45
+ export const toggle = (el, cls, force) => el.classList.toggle(cls, force);
46
+ export const has = (el, cls) => el.classList.contains(cls);
47
+
48
+ // --- STYLES ---
49
+ export const css = (el, prop, value) => {
50
+ if (typeof prop === 'object') {
51
+ Object.assign(el.style, prop);
52
+ } else if (value === undefined) {
53
+ return getComputedStyle(el)[prop];
54
+ } else {
55
+ el.style[prop] = value;
56
+ }
57
+ };
58
+
59
+ // --- TRAVERSAL ---
60
+ export const parent = (el) => el.parentElement;
61
+ export const children = (el) => Array.from(el.children);
62
+ export const siblings = (el) => children(parent(el)).filter(child => child !== el);
63
+ export const next = (el) => el.nextElementSibling;
64
+ export const prev = (el) => el.previousElementSibling;
65
+
66
+ // --- UTILITIES ---
67
+ export const ready = (cb) => {
68
+ if (document.readyState !== 'loading') cb();
69
+ else document.addEventListener('DOMContentLoaded', cb);
70
+ };
71
+
72
+ export const clone = (el, deep = true) => el.cloneNode(deep);
73
+ export const empty = (el) => { el.innerHTML = ''; };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@uistate/aliases",
3
+ "version": "1.0.0",
4
+ "description": "Ergonomic single-character and short-name DOM aliases for vanilla JavaScript - thin wrappers that stay close to the metal with zero abstraction penalty",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [
12
+ "dom",
13
+ "dom-aliases",
14
+ "vanilla-js",
15
+ "ergonomic",
16
+ "lightweight",
17
+ "zero-dependency",
18
+ "framework-free",
19
+ "dom-utilities",
20
+ "shorthand",
21
+ "close-to-metal",
22
+ "uistate"
23
+ ],
24
+ "author": "Ajdin Imsirovic",
25
+ "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/ImsirovicAjdin/uistate-aliases"
29
+ },
30
+ "files": [
31
+ "index.js",
32
+ "index.d.ts",
33
+ "README.md"
34
+ ],
35
+ "exports": {
36
+ ".": {
37
+ "import": "./index.js",
38
+ "types": "./index.d.ts"
39
+ }
40
+ }
41
+ }