@cocreate/element-prototype 1.24.1 → 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,34 @@
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
+
15
+ # [1.25.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.1...v1.25.0) (2024-08-24)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * init getAttribute before getValue and setValue ([e0cc56a](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/e0cc56a2a1dffc69d3916c7653d94269d429309c))
21
+ * removed element.value as already exist ([280b1c6](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/280b1c6b8798d0bd45f4f9665539bdf4b18a0ff6))
22
+ * use hasOwnProperty to avoid prototype ([b7736db](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b7736db4102d9b5f88c2ff9aa508a847a8a7b6eb))
23
+
24
+
25
+ ### Features
26
+
27
+ * Add support for various regex operations in getValue function ([b87b1a2](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b87b1a2e82677cd3f6eb149712ffb68e95f0f2be))
28
+ * encode and decode value ([4550990](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/455099035eebc934fdfb28ac5660db719d82dcd6))
29
+ * support toLocaleString and locale atttribute ([8ebfbd6](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/8ebfbd6f61e7f1efdf661cbdaddfa0b0905c5aa0))
30
+ * supports regex parsing of value ([b2e1a81](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b2e1a817799dbfb7b541543eb24a00c5f1cc88b1))
31
+
1
32
  ## [1.24.1](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.0...v1.24.1) (2024-06-19)
2
33
 
3
34
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/element-prototype",
3
- "version": "1.24.1",
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",
@@ -28,7 +28,7 @@ Element.prototype.getAttribute = function (name) {
28
28
  '$session_id': 'session_id'
29
29
  };
30
30
 
31
- if (localKeys[value]) {
31
+ if (localKeys.hasOwnProperty(value)) {
32
32
  let newValue = localStorage.getItem(localKeys[value]);
33
33
 
34
34
  if (!attributes.has(localKeys[value])) {
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]
@@ -105,6 +105,10 @@ const getValue = (element) => {
105
105
  case 'toUnixTimestamp':
106
106
  value = Math.floor(value.getTime() / 1000);
107
107
  break;
108
+ case 'toLocaleString':
109
+ let locale = element.getAttribute('locale') || 'en-US'
110
+ value = value[valueType](locale);
111
+ break;
108
112
  default:
109
113
  if (typeof value[valueType] === 'function') {
110
114
  value = value[valueType]();
@@ -130,22 +134,6 @@ const getValue = (element) => {
130
134
  value = element.innerHTML;
131
135
  }
132
136
 
133
- if (!Array.isArray(value)) {
134
- if (prefix || suffix)
135
- value = prefix + value + suffix;
136
-
137
- if (valueType == 'array')
138
- value = [value];
139
- }
140
-
141
- if (value && (valueType == 'object' || valueType == 'json')) {
142
- try {
143
- value = JSON.parse(value)
144
- } catch (error) {
145
- value = value
146
- }
147
- }
148
-
149
137
  if (valueType === 'boolean') {
150
138
  if (!value || value === 'fasle')
151
139
  return false
@@ -190,30 +178,95 @@ const getValue = (element) => {
190
178
  }
191
179
  }
192
180
 
193
- let replace = element.getAttribute('value-replace');
194
- let replaceAll = element.getAttribute('value-replaceall');
181
+ try {
182
+ let replace = element.getAttribute('value-replace');
183
+ let replaceAll = element.getAttribute('value-replaceall');
184
+ let test = element.getAttribute('value-test');
185
+ let match = element.getAttribute('value-match');
186
+ let split = element.getAttribute('value-split');
187
+ let lastIndex = element.getAttribute('value-lastindex');
188
+ let search = element.getAttribute('value-search');
189
+ let exec = element.getAttribute('value-exec');
195
190
 
196
- if (replace || replaceAll) {
197
- let replaceWith = element.getAttribute('value-replace-with') || "";
198
- let replaceRegex = element.getAttribute('value-replace-regex');
191
+ if (replace || replaceAll || test || match || split || lastIndex || search || exec) {
192
+ let { regex, replacement } = regexParser(replace || replaceAll || test || match || split || lastIndex || search || exec);
199
193
 
200
- if (replaceRegex) {
201
- try {
194
+ if (regex) {
202
195
  if (replace)
203
- value = value.replace(replaceRegex, replaceWith);
204
- else
205
- value = value.replaceAll(replaceRegex, replaceWith);
206
- } catch (error) {
207
- console.error('getValue() Regex error:', error, element);
196
+ replace = regex;
197
+ else if (replaceAll)
198
+ replaceAll = regex;
199
+ else if (test)
200
+ test = regex;
201
+ else if (match)
202
+ match = regex;
203
+ else if (split)
204
+ split = regex;
205
+ else if (lastIndex)
206
+ lastIndex = regex;
207
+ else if (search)
208
+ search = regex;
209
+ else if (exec)
210
+ exec = regex;
211
+ }
212
+
213
+ replacement = replacement || element.getAttribute('value-replacement') || "";
214
+
215
+ if (replacement !== undefined) {
216
+ if (replace) {
217
+ value = value.replace(replace, replacement);
218
+ } else if (replaceAll) {
219
+ value = value.replaceAll(replaceAll, replacement);
220
+ }
221
+ }
222
+
223
+ if (test) {
224
+ value = regex.test(value);
225
+ }
226
+
227
+ if (match) {
228
+ const matches = value.match(match);
229
+ if (matches) {
230
+ value = matches[1] || matches[0]; // prioritize capturing group match if available
231
+ }
232
+ }
233
+
234
+ if (split) {
235
+ value = value.split(split);
236
+ }
237
+
238
+ if (lastIndex) {
239
+ regex.lastIndex = 0; // Ensure starting index is 0
240
+ regex.test(value);
241
+ value = regex.lastIndex;
242
+ }
243
+
244
+ if (search) {
245
+ value = value.search(search);
246
+ }
247
+
248
+ if (exec) {
249
+ const execResult = regex.exec(value);
250
+ if (execResult) {
251
+ value = execResult[1] || execResult[0]; // prioritize capturing group match if available
252
+ } else {
253
+ value = null;
254
+ }
208
255
  }
209
- } else {
210
- if (replace)
211
- value = value.replace(replace, replaceWith);
212
- else
213
- value = value.replaceAll(replaceAll, replaceWith);
214
256
  }
257
+ } catch (error) {
258
+ console.error('getValue() error:', error, element);
215
259
  }
216
260
 
261
+ // TODO: encode and decode needs a method to prevent multiple encode of an already encoded value
262
+ let encode = element.getAttribute('value-encode')
263
+ if (encode)
264
+ value = encodeValue(value, encode)
265
+
266
+ let decode = element.getAttribute('value-decode')
267
+ if (decode)
268
+ value = decodeValue(value, decode)
269
+
217
270
  let lowercase = element.getAttribute('value-lowercase')
218
271
  if (lowercase || lowercase === '')
219
272
  value = value.toLowerCase()
@@ -221,8 +274,123 @@ const getValue = (element) => {
221
274
  if (uppercase || uppercase === '')
222
275
  value = value.toUpperCase()
223
276
 
277
+ // Apply prefix and suffix first, before JSON parsing
278
+ if (typeof value === 'string' || typeof value === 'number') {
279
+ if (prefix || suffix) {
280
+ value = prefix + value + suffix;
281
+ }
282
+ }
283
+
284
+ // Handle JSON parsing for objects, arrays, or when valueType starts with 'array'
285
+ if (value && (valueType === 'object' || valueType === 'json' || valueType.startsWith('array'))) {
286
+ try {
287
+ value = JSON.parse(value);
288
+ } catch (error) {
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
+ }
323
+ }
324
+ }
325
+
224
326
  return value;
225
327
 
226
328
  };
227
329
 
330
+ function regexParser(string) {
331
+ let regex, replacement;
332
+ let regexMatch = string.match(/\/(.+)\/([gimuy]*)/);
333
+ if (regexMatch) {
334
+ regex = new RegExp(regexMatch[1], regexMatch[2]);
335
+ const splitReplace = string.split(', ');
336
+ replacement = splitReplace.length > 1 ? splitReplace[1].slice(1, -1) : "";
337
+ }
338
+
339
+ return { regex, replacement }
340
+ }
341
+
342
+ function encodeValue(value, encodingType) {
343
+ switch (encodingType.toLowerCase()) {
344
+ case 'url':
345
+ case 'uri':
346
+ return encodeURI(value.replace(/ /g, "%20"));
347
+ case 'uri-component':
348
+ return encodeURIComponent(value.replace(/ /g, "%20"));
349
+ case 'base64':
350
+ case 'atob':
351
+ const encoder = new TextEncoder();
352
+ const uint8Array = encoder.encode(value);
353
+ return btoa(String.fromCharCode(...uint8Array));
354
+ case 'html-entities':
355
+ return value.replace(/[\u00A0-\u9999<>\&]/g, (i) => {
356
+ return `&#${i.charCodeAt(0)};`;
357
+ });
358
+ case 'json':
359
+ return JSON.stringify(value);
360
+ default:
361
+ throw new Error(`Unsupported encoding type: ${encodingType}`);
362
+ }
363
+ }
364
+
365
+ function decodeValue(value, decodingType) {
366
+ switch (decodingType.toLowerCase()) {
367
+ case 'url':
368
+ case 'uri':
369
+ return decodeURI(value);
370
+ case 'uri-component':
371
+ return decodeURIComponent(value);
372
+ case 'base64':
373
+ case 'btoa': // New case for Base64 decoding (alias for 'base64')
374
+ try {
375
+ const decodedArray = Uint8Array.from(atob(value), (c) => c.charCodeAt(0));
376
+ const decoder = new TextDecoder();
377
+ return decoder.decode(decodedArray);
378
+ } catch (error) {
379
+ throw new Error(`Invalid Base64 string: ${error.message}`);
380
+ }
381
+ case 'html-entities':
382
+ const tempElement = document.createElement('div');
383
+ tempElement.innerHTML = value;
384
+ return tempElement.textContent;
385
+ case 'json':
386
+ try {
387
+ return JSON.parse(value);
388
+ } catch (error) {
389
+ throw new Error(`Invalid JSON string: ${error.message}`);
390
+ }
391
+ default:
392
+ throw new Error(`Unsupported decoding type: ${decodingType}`);
393
+ }
394
+ }
395
+
228
396
  export { getValue, storage };
package/src/index.js CHANGED
@@ -20,8 +20,8 @@
20
20
  // you must obtain a commercial license from CoCreate LLC.
21
21
  // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
22
22
 
23
+ import { getAttribute } from './getAttribute';
23
24
  import { setValue } from './setValue';
24
25
  import { getValue } from './getValue';
25
- import { getAttribute } from './getAttribute';
26
26
 
27
- export default { getValue, setValue, getAttribute }
27
+ export default { getAttribute, getValue, setValue }