@cocreate/element-prototype 1.25.0 → 1.26.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,17 @@
1
+ # [1.26.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.25.0...v1.26.0) (2024-09-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * removed commented code ([67b9cb4](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/67b9cb43a399b0c856ac8c285b4aa13d3ea93859))
7
+ * valueType default empty string to support startsWith ([4515bc2](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/4515bc2b6a1466eca1d210692002977fa6113540))
8
+
9
+
10
+ ### Features
11
+
12
+ * 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))
13
+ * 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))
14
+
1
15
  # [1.25.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.1...v1.25.0) (2024-08-24)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/element-prototype",
3
- "version": "1.25.0",
3
+ "version": "1.26.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",
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]
@@ -178,33 +178,6 @@ const getValue = (element) => {
178
178
  }
179
179
  }
180
180
 
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
181
  try {
209
182
  let replace = element.getAttribute('value-replace');
210
183
  let replaceAll = element.getAttribute('value-replaceall');
@@ -301,19 +274,52 @@ const getValue = (element) => {
301
274
  if (uppercase || uppercase === '')
302
275
  value = value.toUpperCase()
303
276
 
304
- if (!Array.isArray(value)) {
305
- if (prefix || suffix)
277
+ // Apply prefix and suffix first, before JSON parsing
278
+ if (typeof value === 'string' || typeof value === 'number') {
279
+ if (prefix || suffix) {
306
280
  value = prefix + value + suffix;
307
-
308
- if (valueType == 'array')
309
- value = [value];
281
+ }
310
282
  }
311
283
 
312
- if (value && (valueType == 'object' || valueType == 'json')) {
284
+ // Handle JSON parsing for objects, arrays, or when valueType starts with 'array'
285
+ if (value && (valueType === 'object' || valueType === 'json' || valueType.startsWith('array'))) {
313
286
  try {
314
- value = JSON.parse(value)
287
+ value = JSON.parse(value);
315
288
  } catch (error) {
316
- value = value
289
+ const jsonRegex = /(\{[\s\S]*\}|\[[\s\S]*\])/;
290
+ const match = value.match(jsonRegex);
291
+
292
+ if (match) {
293
+ try {
294
+ value = JSON.parse(match[0]);
295
+ } catch (e) {
296
+ console.error('Failed to parse JSON after regex extraction:', e);
297
+ }
298
+ } else {
299
+ console.error('No valid JSON structure found in the string.');
300
+ }
301
+ }
302
+ }
303
+
304
+ // Now handle array-specific logic if valueType starts with 'array'
305
+ if (valueType.startsWith('array')) {
306
+ if (!Array.isArray(value)) {
307
+ // If the parsed value is an object, apply array conversion based on operators
308
+ if (typeof value === 'object') {
309
+ if (valueType === 'array.$keys') {
310
+ value = Object.keys(value);
311
+ } else if (valueType === 'array.$values') {
312
+ value = Object.values(value);
313
+ } else if (valueType === 'array.$entries') {
314
+ value = Object.entries(value);
315
+ } else {
316
+ // Default behavior: wrap the object in an array
317
+ value = [value];
318
+ }
319
+ } else {
320
+ // If it's not an object (i.e., a primitive), wrap the value in an array
321
+ value = [value];
322
+ }
317
323
  }
318
324
  }
319
325