@cocreate/utils 1.25.3 → 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-utils/compare/v1.25.4...v1.26.0) (2023-10-22)
2
+
3
+
4
+ ### Features
5
+
6
+ * ObjectId() returns an object containg the parts iof the _id along with a toString() function ([9cd7b81](https://github.com/CoCreate-app/CoCreate-utils/commit/9cd7b817282af1e0b3adaab7df5d059df2f1c0bc))
7
+
8
+ ## [1.25.4](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.25.3...v1.25.4) (2023-10-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * bump dependencies ([a6e8f6d](https://github.com/CoCreate-app/CoCreate-utils/commit/a6e8f6d38ad30053f356be53cb5bf75454e1991a))
14
+
1
15
  ## [1.25.3](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.25.2...v1.25.3) (2023-10-09)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.25.3",
3
+ "version": "1.26.0",
4
4
  "description": "A simple utils component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "utils",
package/src/index.js CHANGED
@@ -42,22 +42,38 @@
42
42
  /**
43
43
  * Generates an ObjectId
44
44
  */
45
- const ObjectId = (id) => {
46
- // Define the rnd function
47
- const rnd = (r16) => Math.floor(r16).toString(16);
48
-
49
- if (id === undefined) {
50
- // If id is undefined, generate a new ObjectId
51
- return rnd(Date.now() / 1000) + '0'.repeat(16).replace(/./g, () => rnd(Math.random() * 16));
52
- } else {
53
- // Check if the provided id is a valid ObjectId
54
- const validIdRegex = /^[0-9a-fA-F]{24}$/;
55
- if (!validIdRegex.test(id)) {
56
- throw new Error('Invalid ObjectId');
57
- }
58
- return id; // Return the valid ObjectId as a string
45
+ let counter = 0;
46
+ function ObjectId(inputId) {
47
+ if (inputId && /^[0-9a-fA-F]{24}$/.test(inputId)) {
48
+ // If a valid ObjectId is provided, return it as a custom ObjectId object
49
+ return {
50
+ timestamp: inputId.substring(0, 8),
51
+ processId: inputId.substring(8, 20),
52
+ counter: inputId.substring(20),
53
+ toString: function () {
54
+ return this.timestamp + this.processId + this.counter;
55
+ }
56
+ };
57
+ } else if (inputId) {
58
+ throw new Error('Invalid ObjectId provided.');
59
59
  }
60
- };
60
+
61
+ // Generate a new custom ObjectId
62
+ const timestampHex = Math.floor(Date.now() / 1000).toString(16).padStart(8, '0');
63
+ const processIdHex = Math.floor(Math.random() * 0x100000000000).toString(16).padStart(12, '0');
64
+ counter = (counter + 1) % 10000;
65
+ const counterHex = counter.toString(16).padStart(4, '0');
66
+
67
+ // Return the custom ObjectId object with a toString() method
68
+ return {
69
+ timestamp: timestampHex,
70
+ processId: processIdHex,
71
+ counter: counterHex,
72
+ toString: function () {
73
+ return this.timestamp + this.processId + this.counter;
74
+ }
75
+ };
76
+ }
61
77
 
62
78
  function checkValue(value) {
63
79
  if (/{{\s*([\w\W]+)\s*}}/g.test(value))