forked from mrvautin/adminMongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoEJSON.js
93 lines (83 loc) · 3.3 KB
/
toEJSON.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var toEJSON = (function (){
var serialize_BinData = function (bsonString){
var bson_full = bsonString.match(/(BinData\s?\([^)]+\))/g);
if(bson_full){
for(var i = 0; i < bson_full.length; i++){
var bson_value = bson_full[i].match(/\((.*?)\)/i);
var bson_data = bson_value[1].split(',');
var ejson = '{ "$binary": ' + bson_data[1] + ', "$type": "' + bson_data[0] + '" }';
bsonString = bsonString.replace(bson_full[i], ejson);
}
}
return bsonString;
};
var serialize_Date = function (bsonString){
var bson_full = bsonString.match(/(new Date\s?)\(.?\)/g);
if(bson_full){
for(var i = 0; i < bson_full.length; i++){
var dte = new Date();
var ejson = '{ "$date": "' + dte.toISOString() + '" }';
bsonString = bsonString.replace(bson_full[i], ejson);
}
}
return bsonString;
};
var serialize_ISODate = function (bsonString){
var bson_full = bsonString.match(/(ISODate\s?\([^)]+\))/g);
if(bson_full){
for(var i = 0; i < bson_full.length; i++){
var bson_value = bson_full[i].match(/\((.*?)\)/i);
var ejson = '{ "$date": ' + bson_value[1] + ' }';
bsonString = bsonString.replace(bson_full[i], ejson);
}
}
return bsonString;
};
var serialize_Timestamp = function (bsonString){
var bson_full = bsonString.match(/(Timestamp\s?\([^)]+\))/g);
if(bson_full){
for(var i = 0; i < bson_full.length; i++){
var bson_value = bson_full[i].match(/\((.*?)\)/i);
var bson_data = bson_value[1].split(',');
var ejson = '{ "$timestamp": { "$t": ' + bson_data[0] + ', "$i": ' + bson_data[1] + '}}';
bsonString = bsonString.replace(bson_full[i], ejson);
}
}
return bsonString;
};
var serialize_Regex = function (bsonString){
// TODO: Implement a regex fixer
return bsonString;
};
var serialize_ObjectId = function (bsonString){
var bson_full = bsonString.match(/(ObjectId\s?\([^)]+\))/g);
if(bson_full){
for(var i = 0; i < bson_full.length; i++){
var bson_value = bson_full[i].match(/\((.*?)\)/i);
var ejson = '{ "$oid": ' + bson_value[1] + '}';
bsonString = bsonString.replace(bson_full[i], ejson);
}
}
return bsonString;
};
var serialize_DBRef = function (bsonString){
// TODO: possibly implement something in the future here
return bsonString;
};
var serializeString = function (bsonString){
if(bsonString){
bsonString = serialize_BinData(bsonString);
bsonString = serialize_Date(bsonString);
bsonString = serialize_ISODate(bsonString);
bsonString = serialize_Timestamp(bsonString);
bsonString = serialize_Regex(bsonString);
bsonString = serialize_ObjectId(bsonString);
bsonString = serialize_DBRef(bsonString);
}
var eJsonString = bsonString;
return eJsonString;
};
return{
serializeString: serializeString
};
})();