(originalObj)
| 49 | } |
| 50 | |
| 51 | function jsonObjToMongo(originalObj) { |
| 52 | let obj = _.clone(originalObj); |
| 53 | if (!obj) return obj; |
| 54 | if (Array.isArray(obj)) return obj.map(d => jsonObjToMongo(d)); |
| 55 | else if (typeof obj === 'string' && objectIdAsStringRegex.test(obj)) return stringToMongoObjectId(obj); |
| 56 | else if (typeof obj === 'string' && mongoIdRegex.test(obj)) return stringToMongoObjectId(`ObjectId("${obj}")`); |
| 57 | else if (typeof obj === 'string' && dateAsStringRegex.test(obj)) return stringToDate(obj); |
| 58 | else if (typeof obj === 'string' && regExpRegex.test(obj)) return stringToRegExp(obj); |
| 59 | else if (isJSONObject(obj)) { |
| 60 | obj = convertToRegularObject(obj); |
| 61 | for (let key in obj) { |
| 62 | if (!obj[key]) continue; |
| 63 | else if (typeof obj[key] === 'string') { |
| 64 | // TODO label a key as ObjectId better (not through a string) |
| 65 | if (objectIdAsStringRegex.test(obj[key])) obj[key] = stringToMongoObjectId(obj[key]); |
| 66 | else if (mongoIdRegex.test(obj[key])) obj[key] = stringToMongoObjectId(`ObjectId("${obj[key]}")`); |
| 67 | else if (dateAsStringRegex.test(obj[key])) obj[key] = stringToDate(obj[key]); |
| 68 | else if (regExpRegex.test(obj[key])) obj[key] = stringToRegExp(obj[key]); |
| 69 | } else if (obj[key]._bsontype === "ObjectID") { |
| 70 | continue; |
| 71 | } else if (isJSONObject(obj[key]) || Array.isArray(obj[key])) { |
| 72 | obj[key] = jsonObjToMongo(obj[key]); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return obj; |
| 77 | } |
| 78 | |
| 79 | function stringToMongoObjectId(str) { |
| 80 | let idValue = str.match(objectIdAsStringRegex); |
no test coverage detected