@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
57 lines • 2.06 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findExistingDir = exports.readJsonFile = exports.deepMerge = exports.isPlainObject = void 0;
const fs_1 = __importDefault(require("fs"));
const index_1 = require("../../error/index");
const isPlainObject = (value) => {
return (typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
Object.prototype.toString.call(value) === '[object Object]');
};
exports.isPlainObject = isPlainObject;
const deepMerge = (base, override) => {
const out = { ...base };
for (const [key, overrideValue] of Object.entries(override)) {
const baseValue = out[key];
if ((0, exports.isPlainObject)(baseValue) && (0, exports.isPlainObject)(overrideValue)) {
out[key] = (0, exports.deepMerge)(baseValue, overrideValue);
continue;
}
out[key] = overrideValue;
}
return out;
};
exports.deepMerge = deepMerge;
const readJsonFile = (filePath) => {
const content = fs_1.default.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(content);
if (!(0, exports.isPlainObject)(parsed)) {
throw new index_1.BadRequestError({
message: `Invalid locale JSON shape in ${filePath}`,
reason: 'Locale file must contain a plain JSON object',
component: 'I18nNode',
operation: 'readJsonFile',
metadata: { filePath },
});
}
return parsed;
};
exports.readJsonFile = readJsonFile;
const findExistingDir = (candidates) => {
for (const candidate of candidates) {
try {
if (fs_1.default.existsSync(candidate) && fs_1.default.statSync(candidate).isDirectory()) {
return candidate;
}
}
catch {
}
}
return null;
};
exports.findExistingDir = findExistingDir;
//# sourceMappingURL=helpers.js.map