Skip to content

Commit 70b6b7d

Browse files
committed
Merge branch 'dev'
2 parents 1a3e2bc + a0d6fa1 commit 70b6b7d

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,25 @@
3939

4040
}
4141

42-
const ObjectId = (rnd = r16 => Math.floor(r16).toString(16)) =>
43-
rnd(Date.now() / 1000) + ' '.repeat(16).replace(/./g, () => rnd(Math.random() * 16));
42+
/**
43+
* Generates an ObjectId
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
59+
}
60+
};
4461

4562
function checkValue(value) {
4663
if (/{{\s*([\w\W]+)\s*}}/g.test(value))
@@ -49,10 +66,28 @@
4966
return true
5067
}
5168

69+
function isValidDate(value) {
70+
// Check if the value is a string and can be converted to a Date object
71+
if (typeof value === 'string'
72+
&& !isNaN(value)
73+
&& !(/^[0-9a-fA-F]{24}$/.test(value))
74+
&& !(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z|Mon|Tue|Wed|Thu|Fri|Sat|Sun [A-Za-z]{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} [A-Za-z]{3} \+\d{4}|\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|Sun|Mon|Tue|Wed|Thu|Fri|Sat),? .+$/.test(value))
75+
) {
76+
const dateObject = new Date(value);
77+
78+
// Check if the result of the Date constructor is a valid Date object
79+
if (!isNaN(dateObject) && dateObject.toString() !== 'Invalid Date') {
80+
return dateObject; // It's a valid Date object
81+
}
82+
}
83+
84+
return value; // It's not a valid Date object
85+
}
86+
5287
function dotNotationToObject(data, obj = {}) {
5388
try {
5489
for (const key of Object.keys(data)) {
55-
let value = data[key]
90+
let value = isValidDate(data[key])
5691
let newObject = obj
5792
let oldObject = new Object(obj)
5893
let keys = key.split('.');
@@ -602,6 +637,7 @@
602637
return {
603638
ObjectId,
604639
checkValue,
640+
isValidDate,
605641
dotNotationToObject,
606642
getValueFromObject,
607643
isObjectEmpty,

0 commit comments

Comments
 (0)