@nestjs-cqrs-eventsourcing/core
Version:
Event sourcing for nestjs CQRS
88 lines (87 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dedupe = exports.sleep = exports.getRandomNonce = exports.randomNonce = exports.generateRandomNonce = exports.getUpdateDiff = exports.errSection = exports.logSection = exports.nowIsoGmt = exports.nowIso = exports.isValidDate = void 0;
exports.logSection1 = logSection1;
const deep_object_diff_1 = require("deep-object-diff");
const isValidDate = (d) => d instanceof Date && !Number.isNaN(d) && d.getTime() === d.getTime();
exports.isValidDate = isValidDate;
const nowIso = () => (new Date()).toISOString();
exports.nowIso = nowIso;
function toIsoString(date) {
const tzo = -date.getTimezoneOffset();
const dif = tzo >= 0 ? '+' : '-';
const pad = (num) => `${(num < 10 ? '0' : '')}${num}`;
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds())}${dif}${pad(Math.floor(Math.abs(tzo) / 60))}:${pad(Math.abs(tzo) % 60)}`;
}
const nowIsoGmt = () => toIsoString(new Date());
exports.nowIsoGmt = nowIsoGmt;
const logSection = (section, ...message) => {
console.log(section, ':', ...message);
};
exports.logSection = logSection;
const errSection = (section, ...message) => {
console.error('[ERROR]', section, ':', ...message);
};
exports.errSection = errSection;
const getUpdateDiff = (oldObj, newObj, nullableFields) => {
const theDiff = (0, deep_object_diff_1.detailedDiff)(oldObj, newObj);
const addedKeys = Reflect.ownKeys(theDiff.added);
const updatedKeys = Reflect.ownKeys(theDiff.updated);
const deletedKeys = Reflect.ownKeys(theDiff.deleted);
const allModifiedFields = [...addedKeys, ...updatedKeys, ...deletedKeys];
const update = {};
for (const key of allModifiedFields) {
if (addedKeys.includes(key) || updatedKeys.includes(key)) {
update[key] = newObj[key];
}
if (oldObj[key] && deletedKeys.includes(key) && nullableFields?.includes(key)) {
update[key] = null;
}
if (deletedKeys.includes(key) && typeof theDiff.deleted[key] === 'object') {
update[key] = newObj[key];
}
}
if (Reflect.ownKeys(update).length > 0) {
return update;
}
return false;
};
exports.getUpdateDiff = getUpdateDiff;
const generateRandomNonce = (length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') => {
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
exports.generateRandomNonce = generateRandomNonce;
const randomNonce = (length) => (0, exports.generateRandomNonce)(length);
exports.randomNonce = randomNonce;
const getRandomNonce = (length) => (0, exports.generateRandomNonce)(length);
exports.getRandomNonce = getRandomNonce;
const sleep = async (ms) => new Promise((resolve) => {
setTimeout(resolve, ms);
});
exports.sleep = sleep;
const dedupe = (a) => [...new Set(a)];
exports.dedupe = dedupe;
function logSection1(level, section, action, ...rest) {
const addSign = (prefix1, level1, sign, inflate = 4) => {
for (let i = 0; i < level1 * inflate; i += 1) {
prefix1.push(sign);
}
};
const prefix = [];
addSign(prefix, level, '>');
if (action === 'request') {
addSign(prefix, level, '?', 2);
}
else if (action === 'acquire') {
addSign(prefix, level, '+', 2);
}
else if (action === 'release') {
addSign(prefix, level, '-', 2);
}
console.log(prefix.join(''), `[${section.toUpperCase()}]`, `${action} lock`, ...rest);
}