forked from nbredikhin/fivem-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
39 lines (34 loc) · 989 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const { ObjectID } = require("mongodb");
function exportDocument(document) {
if (!document) return;
if (document._id && typeof document._id !== "string") {
document._id = document._id.toString();
}
return document;
};
function exportDocuments(documents) {
if (!Array.isArray(documents)) return;
return documents.map((document => exportDocument(document)));
}
function safeObjectArgument(object) {
if (!object) return {};
if (Array.isArray(object)) {
return object.reduce((acc, value, index) => {
acc[index] = value;
return acc;
}, {});
}
if (typeof object !== "object") return {};
if (object._id) object._id = ObjectID(object._id);
return object;
}
function safeCallback(cb, ...args) {
if (typeof cb === "function") return setImmediate(() => cb(...args));
else return false;
}
module.exports = {
exportDocument,
exportDocuments,
safeObjectArgument,
safeCallback
}