@briannorman9/eli-utils 1.0.0 → 1.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +177 -35
  3. package/index.js +1100 -11
  4. package/package.json +17 -13
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Brian Norman
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @briannorman9/eli-utils
2
2
 
3
- Utility functions for ELI web experiments.
3
+ Utility functions for web experiments with ELI (Experimentation Local Interface).
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,72 +10,214 @@ npm install @briannorman9/eli-utils
10
10
 
11
11
  ## Usage
12
12
 
13
- In your ELI project code, you can import utils using the special `@eli/utils` import syntax (the server will automatically resolve it to the installed `eli-utils` package):
13
+ Import the utils in your ELI variant code:
14
14
 
15
15
  ```javascript
16
16
  import utils from '@eli/utils';
17
+ ```
18
+
19
+ **Note:** ELI's import system automatically resolves `@eli/utils` to `@briannorman9/eli-utils` from your project's `node_modules`.
20
+
21
+ ## API
17
22
 
18
- // Wait for an element to appear
23
+ ### DOM Utilities
24
+
25
+ #### `waitForElement(selector)`
26
+ Wait for the first element matching the selector to appear in the DOM. Note: This only matches the first element. Use `waitForElements()` to match all elements.
27
+
28
+ ```javascript
19
29
  utils.waitForElement('#myElement').then(element => {
20
30
  console.log('Element found:', element);
21
31
  });
32
+ ```
33
+
34
+ #### `waitForElements(selector, context)`
35
+ Wait for all elements matching the selector to appear in the DOM. Note: This matches all elements. Use `waitForElement()` to match only the first element.
36
+
37
+ ```javascript
38
+ utils.waitForElements('.product-card').then(elements => {
39
+ elements.forEach(card => card.style.border = '2px solid red');
40
+ });
41
+ ```
42
+
43
+ #### `waitUntil(condition, interval)`
44
+ Wait until a condition function returns true.
22
45
 
23
- // Wait until a condition is met
46
+ ```javascript
24
47
  utils.waitUntil(() => document.readyState === 'complete');
48
+ ```
49
+
50
+ #### `select(selector, context)`
51
+ Select a single element (returns immediately, does not wait).
25
52
 
26
- // DOM manipulation
53
+ ```javascript
54
+ const button = utils.select('#myButton');
55
+ ```
56
+
57
+ #### `observeSelector(selector, callback, options)`
58
+ Observe mutations on the first element matching the selector. The callback is called every time the element is mutated (attributes, children, text, etc.). Note: This only observes the first matching element. Use `observeSelectors()` to observe all elements.
59
+
60
+ **Mutation types:**
61
+ - `'childList'` - Watch for child nodes being added/removed (default: true)
62
+ - `'attributes'` - Watch for attribute changes (default: true)
63
+ - `'characterData'` - Watch for text content changes (default: false)
64
+ - `'subtree'` - Watch all descendants, not just direct children (default: true)
65
+ - `'attributeOldValue'` - Include old attribute value in mutation record (default: false)
66
+ - `'characterDataOldValue'` - Include old text value in mutation record (default: false)
67
+ - `'attributeFilter'` - Array of attribute names to observe (only these attributes will trigger)
68
+
69
+ ```javascript
70
+ // Observe attribute changes on a button
71
+ const stopObserving = utils.observeSelector('#myButton', (element, mutation) => {
72
+ console.log('Button mutated:', mutation.type);
73
+ if (mutation.type === 'attributes') {
74
+ console.log('Attribute changed:', mutation.attributeName);
75
+ }
76
+ }, {
77
+ mutations: ['attributes'],
78
+ attributeFilter: ['class', 'disabled']
79
+ });
80
+
81
+ // Observe when children are added/removed
82
+ utils.observeSelector('.product-list', (element, mutation) => {
83
+ if (mutation.type === 'childList') {
84
+ console.log('Children changed:', mutation.addedNodes.length, 'added');
85
+ }
86
+ }, {
87
+ mutations: ['childList', 'subtree']
88
+ });
89
+ ```
90
+
91
+ #### `observeSelectors(selector, callback, options)`
92
+ Observe mutations on all elements matching the selector. The callback is called every time any matching element is mutated. Note: This observes all matching elements. Use `observeSelector()` to observe only the first element.
93
+
94
+ ```javascript
95
+ // Observe all product cards for attribute changes
96
+ const stopObserving = utils.observeSelectors('.product-card', (element, mutation) => {
97
+ if (mutation.type === 'attributes' && mutation.attributeName === 'data-price') {
98
+ console.log('Price changed on:', element);
99
+ }
100
+ }, {
101
+ mutations: ['attributes'],
102
+ attributeFilter: ['data-price', 'class']
103
+ });
104
+ ```
105
+
106
+ ### Class Manipulation
107
+
108
+ #### `addClass(element, className)`
109
+ Add a class to an element.
110
+
111
+ ```javascript
27
112
  utils.addClass('#myButton', 'active');
113
+ ```
114
+
115
+ #### `removeClass(element, className)`
116
+ Remove a class from an element.
117
+
118
+ ```javascript
28
119
  utils.removeClass('#myButton', 'inactive');
120
+ ```
121
+
122
+ #### `toggleClass(element, className)`
123
+ Toggle a class on an element.
124
+
125
+ ```javascript
126
+ utils.toggleClass('#myButton', 'active');
127
+ ```
128
+
129
+ #### `hasClass(element, className)`
130
+ Check if an element has a class.
131
+
132
+ ```javascript
133
+ if (utils.hasClass('#myButton', 'active')) {
134
+ console.log('Button is active');
135
+ }
136
+ ```
137
+
138
+ ### Event Handling
29
139
 
30
- // Event handling
140
+ #### `on(element, event, handler)`
141
+ Add an event listener.
142
+
143
+ ```javascript
31
144
  utils.on('#myButton', 'click', () => console.log('Clicked!'));
145
+ ```
32
146
 
33
- // Cookies
34
- const userId = utils.getCookie('userId');
35
- utils.setCookie('userId', '12345', 30);
147
+ #### `off(element, event, handler)`
148
+ Remove an event listener.
36
149
 
37
- // Query parameters
38
- const campaign = utils.getQueryParam('campaign');
150
+ ```javascript
151
+ utils.off('#myButton', 'click', handler);
152
+ ```
39
153
 
40
- // Custom events
41
- utils.triggerEvent('experimentLoaded', { variant: 'v1' });
154
+ #### `delegate(parent, selector, event, handler)`
155
+ Event delegation - attach event to parent, handle on children.
156
+
157
+ ```javascript
158
+ utils.delegate('#container', '.button', 'click', (e) => {
159
+ console.log('Button clicked:', e.target);
160
+ });
42
161
  ```
43
162
 
44
- ## API Reference
163
+ #### `triggerEvent(eventName, data, target)`
164
+ Trigger a custom event.
45
165
 
46
- ### Element Utilities
166
+ ```javascript
167
+ utils.triggerEvent('experimentLoaded', { variant: 'v1' });
168
+ ```
47
169
 
48
- - `waitForElement(selector)` - Wait for element to appear (waits indefinitely)
49
- - `waitUntil(condition, interval)` - Wait until condition is true (waits indefinitely, checks every `interval` ms, default: 100ms)
50
- - `select(selector, context)` - Select single element
51
- - `selectAll(selector, context)` - Select multiple elements
52
- - `addClass(element, className)` - Add class to element
53
- - `removeClass(element, className)` - Remove class from element
54
- - `toggleClass(element, className)` - Toggle class on element
55
- - `hasClass(element, className)` - Check if element has class
170
+ ### Cookies
56
171
 
57
- ### Event Utilities
172
+ #### `getCookie(name)`
173
+ Get a cookie value by name.
58
174
 
59
- - `on(element, event, handler)` - Add event listener
60
- - `off(element, event, handler)` - Remove event listener
61
- - `delegate(parent, selector, event, handler)` - Event delegation
62
- - `triggerEvent(eventName, data, target)` - Trigger custom event
175
+ ```javascript
176
+ const userId = utils.getCookie('userId');
177
+ ```
63
178
 
64
- ### Cookie Utilities
179
+ #### `setCookie(name, value, days, path)`
180
+ Set a cookie.
65
181
 
66
- - `getCookie(name)` - Get cookie value
67
- - `setCookie(name, value, days, path)` - Set cookie
182
+ ```javascript
183
+ utils.setCookie('userId', '12345', 30);
184
+ ```
68
185
 
69
186
  ### URL Utilities
70
187
 
71
- - `getQueryParam(name, url)` - Get URL query parameter
188
+ #### `getQueryParam(name, url)`
189
+ Get a URL query parameter value.
190
+
191
+ ```javascript
192
+ const campaign = utils.getQueryParam('campaign');
193
+ ```
72
194
 
73
195
  ### Viewport Utilities
74
196
 
75
- - `getViewport()` - Get viewport dimensions
76
- - `isInViewport(element, threshold)` - Check if element is in viewport
77
- - `scrollIntoView(element, options)` - Scroll element into view
197
+ #### `getViewport()`
198
+ Get the viewport dimensions.
199
+
200
+ ```javascript
201
+ const { width, height } = utils.getViewport();
202
+ ```
203
+
204
+ #### `isInViewport(element, threshold)`
205
+ Check if element is in viewport.
206
+
207
+ ```javascript
208
+ if (utils.isInViewport('#myElement')) {
209
+ console.log('Element is visible');
210
+ }
211
+ ```
212
+
213
+ #### `scrollIntoView(element, options)`
214
+ Scroll element into view.
215
+
216
+ ```javascript
217
+ utils.scrollIntoView('#myElement', { behavior: 'smooth', block: 'center' });
218
+ ```
78
219
 
79
220
  ## License
80
221
 
81
222
  MIT
223
+