@just-in/core
Version:
A TypeScript-first framework for building adaptive digital health interventions.
55 lines • 2.13 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toObjectId = void 0;
const mongoDB = __importStar(require("mongodb"));
const logger_manager_1 = require("../../logger/logger-manager");
/**
* Safely converts a string to a MongoDB `ObjectId`.
*
* This function attempts to create an `ObjectId` from a given string `id`.
* If the conversion fails, it logs an error indicating an invalid format
* and returns `null` instead of throwing an exception.
*
* @param id - The string to convert into a MongoDB `ObjectId`.
*
* @returns The created `ObjectId` if the conversion is successful;
* otherwise, returns `null` if the `id` format is invalid.
*/
const toObjectId = (id) => {
if (!id || typeof id !== 'string') {
logger_manager_1.Log.error(`Invalid ObjectId format: ${id}`);
return null;
}
try {
return new mongoDB.ObjectId(id);
}
catch {
logger_manager_1.Log.error(`Invalid ObjectId format: ${id}`);
return null;
}
};
exports.toObjectId = toObjectId;
//# sourceMappingURL=mongo.helpers.js.map