@cocreate/utils 1.25.4 → 1.26.1
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 +14 -0
- package/package.json +1 -1
- package/src/index.js +36 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.26.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.26.0...v1.26.1) (2023-10-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* initialize counter with a random number from 1000 - 5000 ([0d82bf2](https://github.com/CoCreate-app/CoCreate-utils/commit/0d82bf289743a5db7ab431261b3ff343f9cc6c19))
|
|
7
|
+
|
|
8
|
+
# [1.26.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.25.4...v1.26.0) (2023-10-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* 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))
|
|
14
|
+
|
|
1
15
|
## [1.25.4](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.25.3...v1.25.4) (2023-10-14)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -42,22 +42,43 @@
|
|
|
42
42
|
/**
|
|
43
43
|
* Generates an ObjectId
|
|
44
44
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
+
}
|
|
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
|
+
|
|
65
|
+
counter = (counter + 1) % 10000;
|
|
66
|
+
if (counter < 2) {
|
|
67
|
+
counter = Math.floor(Math.random() * (5000 - 100 + 1)) + 100;
|
|
59
68
|
}
|
|
60
|
-
|
|
69
|
+
|
|
70
|
+
const counterHex = counter.toString(16).padStart(4, '0');
|
|
71
|
+
|
|
72
|
+
// Return the custom ObjectId object with a toString() method
|
|
73
|
+
return {
|
|
74
|
+
timestamp: timestampHex,
|
|
75
|
+
processId: processIdHex,
|
|
76
|
+
counter: counterHex,
|
|
77
|
+
toString: function () {
|
|
78
|
+
return this.timestamp + this.processId + this.counter;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
61
82
|
|
|
62
83
|
function checkValue(value) {
|
|
63
84
|
if (/{{\s*([\w\W]+)\s*}}/g.test(value))
|