@cocreate/unique 1.20.0 → 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,17 @@
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
+
8
+ ## [1.20.1](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.20.0...v1.20.1) (2024-12-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * formating ([3bfb89f](https://github.com/CoCreate-app/CoCreate-unique/commit/3bfb89fec9dfb643655f3e0ba093a98e447ee7c3))
14
+
1
15
  # [1.20.0](https://github.com/CoCreate-app/CoCreate-unique/compare/v1.19.8...v1.20.0) (2024-11-04)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/unique",
3
- "version": "1.20.0",
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 };
package/src/server.js CHANGED
@@ -1,47 +1,46 @@
1
1
  class CoCreateUnique {
2
- /**
3
- * @param crud
4
- */
5
- constructor(crud) {
6
- this.wsManager = crud.wsManager
7
- this.crud = crud
8
- this.init();
9
- }
2
+ /**
3
+ * @param crud
4
+ */
5
+ constructor(crud) {
6
+ this.wsManager = crud.wsManager;
7
+ this.crud = crud;
8
+ this.init();
9
+ }
10
10
 
11
- init() {
12
- if (this.wsManager) {
13
- this.wsManager.on('isUnique', (data) => this.isUnique(data));
14
- }
15
- }
11
+ init() {
12
+ if (this.wsManager) {
13
+ this.wsManager.on("isUnique", (data) => this.isUnique(data));
14
+ }
15
+ }
16
16
 
17
+ /**
18
+ * Checks if a object is unique by sending a message to the socket. This is a blocking call so it returns a Promise
19
+ *
20
+ * @param socket - the socket to send the message to
21
+ * @param data - the data to read from the database usually an object
22
+ *
23
+ * @return { Promise } - resolves with the response from the server or rejects with an error if there was a
24
+ */
25
+ async isUnique(data) {
26
+ const self = this;
27
+ try {
28
+ data.method = "object.read";
29
+ this.crud.send(data).then((data) => {
30
+ data.method = "isUnique";
17
31
 
18
- /**
19
- * Checks if a object is unique by sending a message to the socket. This is a blocking call so it returns a Promise
20
- *
21
- * @param socket - the socket to send the message to
22
- * @param data - the data to read from the database usually an object
23
- *
24
- * @return { Promise } - resolves with the response from the server or rejects with an error if there was a
25
- */
26
- async isUnique(data) {
27
- const self = this
28
- try {
29
- data.method = 'object.read'
30
- this.crud.send(data).then((data) => {
31
- data.method = 'isUnique'
32
-
33
- if (data.object.length) {
34
- data.unique = false;
35
- } else
36
- data.unique = true;
37
-
38
- return self.wsManager.send(data);
39
- })
40
- } catch (error) {
41
- console.log(error);
42
- }
43
- }
32
+ if (data.object.length) {
33
+ data.unique = false;
34
+ } else {
35
+ data.unique = true;
36
+ }
44
37
 
38
+ return self.wsManager.send(data);
39
+ });
40
+ } catch (error) {
41
+ console.log(error);
42
+ }
43
+ }
45
44
  }
46
45
 
47
- module.exports = CoCreateUnique;
46
+ module.exports = CoCreateUnique;