@cocreate/unique 1.20.1 → 1.20.2

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,10 @@
1
+ ## [1.20.2](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.20.1...v1.20.2) (2025-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update observer observe param to type and and attributeName to attributeFilter ([81b8d45](https://github.com/CoCreate-app/CoCreate-unique/commit/81b8d45850eb719076fa25334ad3954ec1326fff))
7
+
1
8
  ## [1.20.1](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.20.0...v1.20.1) (2024-12-09)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/unique",
3
- "version": "1.20.1",
3
+ "version": "1.20.2",
4
4
  "description": "A simple unique component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "unique",
package/src/client.js CHANGED
@@ -1,6 +1,5 @@
1
- import observer from '@cocreate/observer';
2
- import crud from '@cocreate/crud-client';
3
-
1
+ import observer from "@cocreate/observer";
2
+ import crud from "@cocreate/crud-client";
4
3
 
5
4
  /**
6
5
  * Initializes elements to ensure their values are unique. This function targets elements with the 'unique' attribute and uses CRUD operations to check if the element's value already exists.
@@ -11,81 +10,75 @@ import crud from '@cocreate/crud-client';
11
10
  * - If null or omitted, all elements in the document with the 'unique' attribute are queried and processed for uniqueness checks.
12
11
  */
13
12
  function init(elements) {
14
- if (!elements)
15
- elements = document.querySelectorAll('[unique]')
16
- else if (!Array.isArray(elements))
17
- elements = [elements]
18
- for (let element of elements)
19
- setInputEvent(element)
13
+ if (!elements) elements = document.querySelectorAll("[unique]");
14
+ else if (!Array.isArray(elements)) elements = [elements];
15
+ for (let element of elements) setInputEvent(element);
20
16
  }
21
17
 
22
18
  /**
23
- * Sets the input event to prevent duplicate. This is necessary because IE doesn't support unique = " true " in HTML5.
24
- *
25
- * @param input - The input element to listen for changes on. Note that it's an object
26
- */
19
+ * Sets the input event to prevent duplicate. This is necessary because IE doesn't support unique = " true " in HTML5.
20
+ *
21
+ * @param input - The input element to listen for changes on. Note that it's an object
22
+ */
27
23
  function setInputEvent(input) {
28
- let delayTimer
29
- input.addEventListener('input', function () {
30
- input.setAttribute('unique', true);
24
+ let delayTimer;
25
+ input.addEventListener("input", function () {
26
+ input.setAttribute("unique", true);
31
27
 
32
- clearTimeout(delayTimer);
33
- delayTimer = setTimeout(function () {
34
- isUnique(input)
35
- }, 1000);
36
- });
28
+ clearTimeout(delayTimer);
29
+ delayTimer = setTimeout(function () {
30
+ isUnique(input);
31
+ }, 1000);
32
+ });
37
33
  }
38
34
 
39
35
  /**
40
- * Checks if a value is unique. This is a helper function for CRUD operations that need to be performed on objects in order to make sure they are unique.
41
- *
42
- * @param element - The element that is being checked for uniqueness. It should have the attribute ` name `
43
- */
36
+ * Checks if a value is unique. This is a helper function for CRUD operations that need to be performed on objects in order to make sure they are unique.
37
+ *
38
+ * @param element - The element that is being checked for uniqueness. It should have the attribute ` name `
39
+ */
44
40
  async function isUnique(element) {
45
- let key = element.getAttribute('key');
46
- let value = await element.getValue();
47
- let data = {
48
- method: 'object.read',
49
- storage: 'indexeddb',
50
- array: element.getAttribute('array'),
51
- $filter: {
52
- query: {
53
- [key]: value
54
- },
55
- limit: 1
56
- }
57
- };
58
-
59
- data = await crud.send(data)
41
+ let key = element.getAttribute("key");
42
+ let value = await element.getValue();
43
+ let data = {
44
+ method: "object.read",
45
+ storage: "indexeddb",
46
+ array: element.getAttribute("array"),
47
+ $filter: {
48
+ query: {
49
+ [key]: value
50
+ },
51
+ limit: 1
52
+ }
53
+ };
60
54
 
61
- let response = {}
62
- // If a object is returned, unique is set to false
63
- if (data.object && data.object.length) {
64
- response.unique = false;
65
- } else
66
- response.unique = true
55
+ data = await crud.send(data);
67
56
 
68
- // If indexedb response is unique is true, check server response
69
- if (response.unique) {
70
- delete data.storage
71
- data.method = 'isUnique'
72
- response = await crud.socket.send(data)
73
- }
57
+ let response = {};
58
+ // If a object is returned, unique is set to false
59
+ if (data.object && data.object.length) {
60
+ response.unique = false;
61
+ } else response.unique = true;
74
62
 
75
- // Set unique attribute on the element
76
- element.setAttribute('unique', response.unique);
77
- return response.unique
63
+ // If indexedb response is unique is true, check server response
64
+ if (response.unique) {
65
+ delete data.storage;
66
+ data.method = "isUnique";
67
+ response = await crud.socket.send(data);
68
+ }
78
69
 
70
+ // Set unique attribute on the element
71
+ element.setAttribute("unique", response.unique);
72
+ return response.unique;
79
73
  }
80
74
 
81
75
  observer.init({
82
- name: 'CoCreateUnique',
83
- observe: ['addedNodes'],
84
- selector: '[unique]',
85
- callback: mutation =>
86
- init(mutation.target)
76
+ name: "CoCreateUnique",
77
+ types: ["addedNodes"],
78
+ selector: "[unique]",
79
+ callback: (mutation) => init(mutation.target)
87
80
  });
88
81
 
89
82
  init();
90
83
 
91
- export default { init, isUnique }
84
+ export default { init, isUnique };