@cocreate/element-prototype 1.25.0 → 1.27.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/CHANGELOG.md CHANGED
@@ -1,3 +1,32 @@
1
+ # [1.27.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.26.0...v1.27.0) (2024-11-04)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * pretier.config.js and file formating ([1428fd9](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/1428fd96719f9411d97af5f067a4f95e99a94eed))
7
+ * removed exec returning null ([240543b](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/240543b19eafe8cd2714ee7f81f9edf0c20774bf))
8
+
9
+
10
+ ### Features
11
+
12
+ * add prettier.config.js and format files ([0acda3c](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/0acda3c61bd6f832a3544d202a7e59a87ee464d3))
13
+ * loop through regex improved value handling ([3ac5f8d](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/3ac5f8dfc978cfc25322b5ef927b8df995c0ab50))
14
+ * value-dispatch attribute to dispatch event even if value is null or undefined ([7f2ca4b](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/7f2ca4b18ec668f77895b3d369b379d4420aa046))
15
+
16
+ # [1.26.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.25.0...v1.26.0) (2024-09-21)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * removed commented code ([67b9cb4](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/67b9cb43a399b0c856ac8c285b4aa13d3ea93859))
22
+ * valueType default empty string to support startsWith ([4515bc2](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/4515bc2b6a1466eca1d210692002977fa6113540))
23
+
24
+
25
+ ### Features
26
+
27
+ * if value-type object and parse fails snitizze to remove any string outside of object and attempt parse again ([9344106](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/93441066dc255bb5becc85594bc5c3d11c1766d5))
28
+ * value-type array.$keys array.$value and array.$entries will return an array from object ([25df40d](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/25df40d1adbbff43f06029b9a22e29f9ec4371c4))
29
+
1
30
  # [1.25.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.1...v1.25.0) (2024-08-24)
2
31
 
3
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/element-prototype",
3
- "version": "1.25.0",
3
+ "version": "1.27.0",
4
4
  "description": "A simple element-prototype component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "element-prototype",
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ tabWidth: 4,
3
+ semi: true,
4
+ trailingComma: "none",
5
+ bracketSameLine: true,
6
+ useTabs: true,
7
+ overrides: [
8
+ {
9
+ files: ["*.json", "*.yml", "*.yaml"],
10
+ options: {
11
+ tabWidth: 2,
12
+ useTabs: false
13
+ },
14
+ }
15
+ ],
16
+ };
package/src/getValue.js CHANGED
@@ -26,7 +26,7 @@ const getValue = (element) => {
26
26
 
27
27
  let prefix = element.getAttribute('value-prefix') || "";
28
28
  let suffix = element.getAttribute('value-suffix') || "";
29
- let valueType = element.getAttribute('value-type');
29
+ let valueType = element.getAttribute('value-type') || "";
30
30
 
31
31
  if (element.type === "checkbox") {
32
32
  let inputs = [element]
@@ -128,10 +128,26 @@ const getValue = (element) => {
128
128
  value = element.srcdoc;
129
129
  } else if (element.hasAttribute('value')) {
130
130
  value = element.getAttribute('value');
131
- } else if (valueType === 'text') {
132
- value = element.innerText;
133
131
  } else {
134
- value = element.innerHTML;
132
+ let targetElement = element;
133
+
134
+ // If value-exclude-selector exists, clone the element and remove the specified selectors
135
+ const excludeSelector = element.getAttribute('value-remove-selector');
136
+ if (excludeSelector) {
137
+ targetElement = element.cloneNode(true);
138
+
139
+ // Remove matching elements from the cloned element
140
+ targetElement.querySelectorAll(excludeSelector).forEach(el => el.remove());
141
+ }
142
+
143
+ // Determine whether to use outerHTML, innerHTML, or innerText based on valueType
144
+ if (valueType === 'text') {
145
+ value = targetElement.innerText;
146
+ } else if (valueType === 'outerHTML') {
147
+ value = targetElement.outerHTML;
148
+ } else {
149
+ value = targetElement.innerHTML;
150
+ }
135
151
  }
136
152
 
137
153
  if (valueType === 'boolean') {
@@ -178,109 +194,88 @@ const getValue = (element) => {
178
194
  }
179
195
  }
180
196
 
181
- // try {
182
- // let replace = element.getAttribute('value-replace');
183
- // let replaceAll = element.getAttribute('value-replaceall');
184
-
185
- // if (replace || replaceAll) {
186
- // let { regex, replacement } = regexParser(replace || replaceAll)
187
- // if (regex) {
188
- // if (replace)
189
- // replace = regex
190
- // else if (replaceAll)
191
- // replaceAll = regex
192
- // }
193
-
194
-
195
- // replacement = replacement || element.getAttribute('value-replacement') || "";
196
-
197
- // if (replacement !== undefined) {
198
- // if (replace)
199
- // value = value.replace(replace, replacement);
200
- // else
201
- // value = value.replaceAll(replaceAll, replacement);
202
- // }
203
- // }
204
- // } catch (error) {
205
- // console.error('getValue() replace error:', error, element);
206
- // }
207
-
208
197
  try {
209
- let replace = element.getAttribute('value-replace');
210
- let replaceAll = element.getAttribute('value-replaceall');
211
- let test = element.getAttribute('value-test');
212
- let match = element.getAttribute('value-match');
213
- let split = element.getAttribute('value-split');
214
- let lastIndex = element.getAttribute('value-lastindex');
215
- let search = element.getAttribute('value-search');
216
- let exec = element.getAttribute('value-exec');
217
-
218
- if (replace || replaceAll || test || match || split || lastIndex || search || exec) {
219
- let { regex, replacement } = regexParser(replace || replaceAll || test || match || split || lastIndex || search || exec);
220
-
221
- if (regex) {
222
- if (replace)
223
- replace = regex;
224
- else if (replaceAll)
225
- replaceAll = regex;
226
- else if (test)
227
- test = regex;
228
- else if (match)
229
- match = regex;
230
- else if (split)
231
- split = regex;
232
- else if (lastIndex)
233
- lastIndex = regex;
234
- else if (search)
235
- search = regex;
236
- else if (exec)
237
- exec = regex;
238
- }
198
+
199
+ const attributes = element.attributes; // Get all attributes of the element
200
+ const regexAttribute = [
201
+ 'value-replace',
202
+ 'value-replaceall',
203
+ 'value-test',
204
+ 'value-match',
205
+ 'value-split',
206
+ 'value-lastindex',
207
+ 'value-search',
208
+ 'value-exec'
209
+ ];
210
+ // Process each attribute in order
211
+ for (let i = 0; i < attributes.length; i++) {
212
+ if (value === null || value === undefined)
213
+ break
214
+
215
+ if (!regexAttribute.includes(attributes[i].name))
216
+ continue
217
+
218
+ let regexAttributeValue = attributes[i].value
219
+
220
+ if (!regexAttributeValue)
221
+ continue
222
+
223
+ let { regex, replacement } = regexParser(regexAttributeValue);
224
+
225
+ if (regex)
226
+ regexAttributeValue = regex
239
227
 
240
228
  replacement = replacement || element.getAttribute('value-replacement') || "";
241
229
 
242
- if (replacement !== undefined) {
243
- if (replace) {
244
- value = value.replace(replace, replacement);
245
- } else if (replaceAll) {
246
- value = value.replaceAll(replaceAll, replacement);
247
- }
248
- }
230
+ switch (attributes[i].name) {
231
+ case 'value-replace':
232
+ value = value.replace(regexAttributeValue, replacement);
233
+ break;
249
234
 
250
- if (test) {
251
- value = regex.test(value);
252
- }
235
+ case 'value-replaceall':
236
+ value = value.replaceAll(regexAttributeValue, replacement);
237
+ break;
253
238
 
254
- if (match) {
255
- const matches = value.match(match);
256
- if (matches) {
257
- value = matches[1] || matches[0]; // prioritize capturing group match if available
258
- }
259
- }
239
+ case 'value-test':
240
+ value = regex.test(value);
241
+ break;
260
242
 
261
- if (split) {
262
- value = value.split(split);
263
- }
243
+ case 'value-match':
244
+ const matches = value.match(regexAttributeValue);
245
+ if (matches) {
246
+ value = matches[1] || matches[0]; // Prioritize capturing group if available
247
+ }
248
+ break;
264
249
 
265
- if (lastIndex) {
266
- regex.lastIndex = 0; // Ensure starting index is 0
267
- regex.test(value);
268
- value = regex.lastIndex;
269
- }
250
+ case 'value-split':
251
+ value = value.split(regexAttributeValue);
252
+ break;
270
253
 
271
- if (search) {
272
- value = value.search(search);
273
- }
254
+ case 'value-lastindex':
255
+ regex.lastIndex = 0; // Ensure starting index is 0
256
+ regex.test(value);
257
+ value = regex.lastIndex;
258
+ break;
274
259
 
275
- if (exec) {
276
- const execResult = regex.exec(value);
277
- if (execResult) {
278
- value = execResult[1] || execResult[0]; // prioritize capturing group match if available
279
- } else {
280
- value = null;
281
- }
260
+ case 'value-search':
261
+ value = value.search(regexAttributeValue);
262
+ break;
263
+
264
+ case 'value-exec':
265
+ const execResult = regex.exec(value);
266
+ if (execResult) {
267
+ value = execResult[1] || execResult[2] || execResult[0]; // Prioritize capturing group if available
268
+ } else {
269
+ // value = null;
270
+ }
271
+ break;
272
+
273
+ default:
274
+ // Ignore other attributes
275
+ break;
282
276
  }
283
277
  }
278
+
284
279
  } catch (error) {
285
280
  console.error('getValue() error:', error, element);
286
281
  }
@@ -301,19 +296,52 @@ const getValue = (element) => {
301
296
  if (uppercase || uppercase === '')
302
297
  value = value.toUpperCase()
303
298
 
304
- if (!Array.isArray(value)) {
305
- if (prefix || suffix)
299
+ // Apply prefix and suffix first, before JSON parsing
300
+ if (typeof value === 'string' || typeof value === 'number') {
301
+ if (prefix || suffix) {
306
302
  value = prefix + value + suffix;
307
-
308
- if (valueType == 'array')
309
- value = [value];
303
+ }
310
304
  }
311
305
 
312
- if (value && (valueType == 'object' || valueType == 'json')) {
306
+ // Handle JSON parsing for objects, arrays, or when valueType starts with 'array'
307
+ if (value && (valueType === 'object' || valueType === 'json' || valueType.startsWith('array'))) {
313
308
  try {
314
- value = JSON.parse(value)
309
+ value = JSON.parse(value);
315
310
  } catch (error) {
316
- value = value
311
+ const jsonRegex = /(\{[\s\S]*\}|\[[\s\S]*\])/;
312
+ const match = value.match(jsonRegex);
313
+
314
+ if (match) {
315
+ try {
316
+ value = JSON.parse(match[0]);
317
+ } catch (e) {
318
+ console.error('Failed to parse JSON after regex extraction:', e);
319
+ }
320
+ } else {
321
+ console.error('No valid JSON structure found in the string.');
322
+ }
323
+ }
324
+ }
325
+
326
+ // Now handle array-specific logic if valueType starts with 'array'
327
+ if (valueType.startsWith('array')) {
328
+ if (!Array.isArray(value)) {
329
+ // If the parsed value is an object, apply array conversion based on operators
330
+ if (typeof value === 'object') {
331
+ if (valueType === 'array.$keys') {
332
+ value = Object.keys(value);
333
+ } else if (valueType === 'array.$values') {
334
+ value = Object.values(value);
335
+ } else if (valueType === 'array.$entries') {
336
+ value = Object.entries(value);
337
+ } else {
338
+ // Default behavior: wrap the object in an array
339
+ value = [value];
340
+ }
341
+ } else {
342
+ // If it's not an object (i.e., a primitive), wrap the value in an array
343
+ value = [value];
344
+ }
317
345
  }
318
346
  }
319
347
 
package/src/setValue.js CHANGED
@@ -14,7 +14,8 @@ HTMLHeadingElement.prototype.setValue = function (value, dispatch) {
14
14
 
15
15
  // TODO: check if using a a switch case will provide better performance
16
16
  const setValue = (el, value, dispatch) => {
17
- if (value === '$false') {
17
+ let valueDispatch = el.getAttribute('value-dispatch')
18
+ if ((valueDispatch || valueDispatch === '') && (value === '$false' || value === undefined || value === null)) {
18
19
  return dispatchEvents(el, dispatch)
19
20
  };
20
21
 
@@ -72,7 +73,7 @@ const setValue = (el, value, dispatch) => {
72
73
  } else if (el.tagName == "SELECT" && el.hasAttribute('multiple') && Array.isArray(value)) {
73
74
  let options = el.options;
74
75
  for (let i = 0; i < options.length; i++) {
75
- if (value.includes(options[i].value)) {
76
+ if (value.includes && value.includes(options[i].value)) {
76
77
  options[i].selected = "selected";
77
78
  } else {
78
79
  options[i].selected = "";