dbgate-tools
Version:
Auxiliary tools for other DbGate packages.
709 lines (708 loc) • 27.9 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSqlFrontMatter = exports.removeSqlFrontMatter = exports.getSqlFrontMatter = exports.parseNumberSafe = exports.deserializeJsTypesReviver = exports.serializeJsTypesReplacer = exports.deserializeJsTypesFromJsonParse = exports.serializeJsTypesForJsonStringify = exports.jsonLinesParse = exports.jsonLinesStringify = exports.pinoLogRecordToMessageRecord = exports.getLimitedQuery = exports.safeFormatDate = exports.extractErrorLogData = exports.extractErrorStackTrace = exports.extractErrorMessage = exports.getConvertValueMenu = exports.detectTypeIcon = exports.detectCellDataType = exports.parseSqlDefaultValue = exports.getAsImageSrc = exports.arrayBufferToBase64 = exports.isWktGeometry = exports.getIconForRedisType = exports.isJsonLikeLongString = exports.shouldOpenMultilineDialog = exports.safeCompileRegExp = exports.safeJsonParse = exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = exports.MAX_GRID_TEXT_LENGTH = void 0;
const isString_1 = __importDefault(require("lodash/isString"));
const isArray_1 = __importDefault(require("lodash/isArray"));
const isDate_1 = __importDefault(require("lodash/isDate"));
const isNumber_1 = __importDefault(require("lodash/isNumber"));
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
const pad_1 = __importDefault(require("lodash/pad"));
const cloneDeepWith_1 = __importDefault(require("lodash/cloneDeepWith"));
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
const omitBy_1 = __importDefault(require("lodash/omitBy"));
const isPlainObject_2 = __importDefault(require("lodash/isPlainObject"));
exports.MAX_GRID_TEXT_LENGTH = 1000; // maximum length of text in grid cell, longer text is truncated
const dateTimeStorageRegex = /^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|()|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
const dateTimeParseRegex = /^(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})(\.[0-9]+)?(([Zz])|()|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
function arrayToHexString(byteArray) {
return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '').toUpperCase();
}
exports.arrayToHexString = arrayToHexString;
function hexStringToArray(inputString) {
var hex = inputString.toString();
var res = [];
for (var n = 0; n < hex.length; n += 2) {
res.push(parseInt(hex.substr(n, 2), 16));
}
return res;
}
exports.hexStringToArray = hexStringToArray;
function parseCellValue(value, editorTypes) {
if (!(0, isString_1.default)(value))
return value;
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseSqlNull) {
if (value == '(NULL)')
return null;
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseHexAsBuffer) {
const mHex = value.match(/^0x([0-9a-fA-F][0-9a-fA-F])+$/);
if (mHex) {
return {
type: 'Buffer',
data: hexStringToArray(value.substring(2)),
};
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseObjectIdAsDollar) {
const mOid = value.match(/^ObjectId\("([0-9a-f]{24})"\)$/);
if (mOid) {
return { $oid: mOid[1] };
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseDateAsDollar) {
const m = value.match(dateTimeParseRegex);
if (m) {
return {
$date: `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z`,
};
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseGeopointAsDollar) {
const m = value.match(/^([\d\.]+)\s*°\s*([NS]),\s*([\d\.]+)\s*°\s*([EW])$/i);
if (m) {
let latitude = parseFloat(m[1]);
const latDir = m[2].toUpperCase();
let longitude = parseFloat(m[3]);
const lonDir = m[4].toUpperCase();
if (latDir === 'S')
latitude = -latitude;
if (lonDir === 'W')
longitude = -longitude;
return {
$geoPoint: {
latitude,
longitude,
},
};
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseFsDocumentRefAsDollar) {
const trimmedValue = value.replace(/\s/g, '');
if (trimmedValue.startsWith('$ref:')) {
return {
$fsDocumentRef: {
documentPath: trimmedValue.slice(5),
},
};
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonNull) {
if (value == 'null')
return null;
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonBoolean) {
if (value == 'true')
return true;
if (value == 'false')
return false;
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseNumber) {
if (/^-?[0-9]+(?:\.[0-9]+)?$/.test(value)) {
return parseNumberSafe(value);
}
}
if ((editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonArray) || (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonObject)) {
const jsonValue = safeJsonParse(value);
if ((0, isPlainObject_1.default)(jsonValue) && (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonObject))
return jsonValue;
if ((0, isArray_1.default)(jsonValue) && (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonArray))
return jsonValue;
}
return value;
}
exports.parseCellValue = parseCellValue;
function parseFunc_ObjectIdAsDollar(value) {
if (value === null || value === void 0 ? void 0 : value.$oid)
return value;
if ((0, isString_1.default)(value)) {
if (value.match(/^[0-9a-f]{24}$/))
return { $oid: value };
const mOid = value.match(/^ObjectId\("([0-9a-f]{24})"\)$/);
if (mOid) {
return { $oid: mOid[1] };
}
}
return value;
}
function parseFunc_DateAsDollar(value) {
if (value === null || value === void 0 ? void 0 : value.$date)
return value;
if ((0, isString_1.default)(value)) {
const m = value.match(dateTimeParseRegex);
if (m) {
return { $date: `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z` };
}
}
return value;
}
function makeBulletString(value) {
return (0, pad_1.default)('', value.length, '•');
}
function highlightSpecialCharacters(value) {
value = value.replace(/\n/g, '↲');
value = value.replace(/\r/g, '');
value = value.replace(/^(\s+)/, makeBulletString);
value = value.replace(/(\s+)$/, makeBulletString);
value = value.replace(/(\s\s+)/g, makeBulletString);
return value;
}
function stringifyJsonToGrid(value) {
if ((0, isPlainObject_1.default)(value)) {
const svalue = JSON.stringify(value, undefined, 2);
if (svalue.length < 100) {
return { value: svalue, gridStyle: 'nullCellStyle' };
}
else {
return { value: '(JSON)', gridStyle: 'nullCellStyle', gridTitle: svalue };
}
}
if ((0, isArray_1.default)(value)) {
return {
value: `[${value.length} items]`,
gridStyle: 'nullCellStyle',
gridTitle: value.map(x => JSON.stringify(x)).join('\n'),
};
}
return { value: '(JSON)', gridStyle: 'nullCellStyle' };
}
function stringifyCellValue(value, intent, editorTypes, gridFormattingOptions, jsonParsedValue) {
var _a;
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseSqlNull) {
if (value === null) {
switch (intent) {
case 'exportIntent':
return { value: '' };
default:
return { value: '(NULL)', gridStyle: 'nullCellStyle' };
}
}
}
if (value === undefined) {
switch (intent) {
case 'gridCellIntent':
return { value: '(No Field)', gridStyle: 'nullCellStyle' };
default:
return { value: '' };
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseJsonNull) {
if (value === null) {
return { value: 'null', gridStyle: 'valueCellStyle' };
}
}
if (value === true)
return { value: 'true', gridStyle: 'valueCellStyle' };
if (value === false)
return { value: 'false', gridStyle: 'valueCellStyle' };
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseHexAsBuffer) {
if ((value === null || value === void 0 ? void 0 : value.type) == 'Buffer' && (0, isArray_1.default)(value.data)) {
return { value: '0x' + arrayToHexString(value.data), gridStyle: 'valueCellStyle' };
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseObjectIdAsDollar) {
if (value === null || value === void 0 ? void 0 : value.$oid) {
switch (intent) {
case 'exportIntent':
case 'stringConversionIntent':
return { value: value.$oid };
default:
return { value: `ObjectId("${value.$oid}")`, gridStyle: 'valueCellStyle' };
}
}
}
if (value === null || value === void 0 ? void 0 : value.$bigint) {
return {
value: value.$bigint,
gridStyle: 'valueCellStyle',
};
}
if (typeof value === 'bigint') {
return {
value: value.toString(),
gridStyle: 'valueCellStyle',
};
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseDateAsDollar) {
if (value === null || value === void 0 ? void 0 : value.$date) {
const dateString = (0, isDate_1.default)(value.$date) ? value.$date.toISOString() : value.$date.toString();
switch (intent) {
case 'exportIntent':
case 'stringConversionIntent':
case 'clipboardIntent':
return { value: dateString };
default:
const m = dateString.match(dateTimeStorageRegex);
if (m) {
return { value: `${m[1]}-${m[2]}-${m[3]} ${m[4]}:${m[5]}:${m[6]}`, gridStyle: 'valueCellStyle' };
}
else {
return { value: dateString.replace('T', ' '), gridStyle: 'valueCellStyle' };
}
}
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseGeopointAsDollar) {
if (value === null || value === void 0 ? void 0 : value.$geoPoint) {
const { latitude, longitude } = value.$geoPoint;
if ((0, isNumber_1.default)(latitude) && (0, isNumber_1.default)(longitude)) {
const latAbs = Math.abs(latitude);
const lonAbs = Math.abs(longitude);
const latDir = latitude >= 0 ? 'N' : 'S';
const lonDir = longitude >= 0 ? 'E' : 'W';
return {
value: `${latAbs}° ${latDir}, ${lonAbs}° ${lonDir}`,
gridStyle: 'valueCellStyle',
};
}
}
}
if (editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.parseFsDocumentRefAsDollar) {
if (value === null || value === void 0 ? void 0 : value.$fsDocumentRef) {
return {
value: `$ref: ${(_a = value.$fsDocumentRef.documentPath) !== null && _a !== void 0 ? _a : ''}`,
gridStyle: 'valueCellStyle',
};
}
}
if ((0, isArray_1.default)(value)) {
switch (intent) {
case 'gridCellIntent':
return stringifyJsonToGrid(value);
case 'multilineEditorIntent':
return { value: JSON.stringify(value, null, 2) };
default:
return { value: JSON.stringify(value), gridStyle: 'valueCellStyle' };
}
}
if ((0, isPlainObject_1.default)(value)) {
switch (intent) {
case 'gridCellIntent':
return stringifyJsonToGrid(value);
case 'multilineEditorIntent':
return { value: JSON.stringify(value, null, 2) };
default:
return { value: JSON.stringify(value), gridStyle: 'valueCellStyle' };
}
}
if ((0, isNumber_1.default)(value)) {
switch (intent) {
case 'gridCellIntent':
return {
value: (gridFormattingOptions === null || gridFormattingOptions === void 0 ? void 0 : gridFormattingOptions.useThousandsSeparator) && (value >= 10000 || value <= -10000)
? value.toLocaleString()
: value.toString(),
gridStyle: 'valueCellStyle',
};
default:
return { value: value.toString() };
}
}
if ((0, isString_1.default)(value)) {
switch (intent) {
case 'gridCellIntent':
if (jsonParsedValue && !(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.explicitDataType)) {
return stringifyJsonToGrid(jsonParsedValue);
}
else {
if (!(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.explicitDataType)) {
// reformat datetime for implicit date types
const m = value.match(dateTimeStorageRegex);
if (m) {
return {
value: `${m[1]}-${m[2]}-${m[3]} ${m[4]}:${m[5]}:${m[6]}`,
gridStyle: 'valueCellStyle',
};
}
}
const valueLimited = value.length > exports.MAX_GRID_TEXT_LENGTH ? value.substring(0, exports.MAX_GRID_TEXT_LENGTH) + '...' : value;
return { value: highlightSpecialCharacters(valueLimited), gridStyle: 'textCellStyle' };
}
default:
return { value: value };
}
}
if (value === null || value === undefined) {
switch (intent) {
case 'gridCellIntent':
return { value: '(n/a)', gridStyle: 'nullCellStyle' };
default:
return { value: '' };
}
}
switch (intent) {
case 'gridCellIntent':
return { value: '(Unknown)', gridStyle: 'nullCellStyle' };
default:
return { value: '' };
}
}
exports.stringifyCellValue = stringifyCellValue;
function safeJsonParse(json, defaultValue, logError = false) {
if ((0, isArray_1.default)(json) || (0, isPlainObject_1.default)(json)) {
return json;
}
try {
return JSON.parse(json);
}
catch (err) {
if (logError) {
console.error(`Error parsing JSON value "${json}"`, err);
}
return defaultValue;
}
}
exports.safeJsonParse = safeJsonParse;
function safeCompileRegExp(regex, flags) {
try {
return new RegExp(regex, flags);
}
catch (err) {
return null;
}
}
exports.safeCompileRegExp = safeCompileRegExp;
function shouldOpenMultilineDialog(value) {
if ((0, isString_1.default)(value)) {
if (value.includes('\n')) {
return true;
}
const parsed = safeJsonParse(value);
if (parsed && ((0, isPlainObject_1.default)(parsed) || (0, isArray_1.default)(parsed))) {
return true;
}
}
if (value === null || value === void 0 ? void 0 : value.$oid) {
return false;
}
if (value === null || value === void 0 ? void 0 : value.$date) {
return false;
}
if (value === null || value === void 0 ? void 0 : value.$bigint) {
return false;
}
if ((0, isPlainObject_1.default)(value) || (0, isArray_1.default)(value)) {
return true;
}
return false;
}
exports.shouldOpenMultilineDialog = shouldOpenMultilineDialog;
function isJsonLikeLongString(value) {
return (0, isString_1.default)(value) && value.length > 100 && value.match(/^\s*\{.*\}\s*$|^\s*\[.*\]\s*$/m);
}
exports.isJsonLikeLongString = isJsonLikeLongString;
function getIconForRedisType(type) {
switch (type) {
case 'dir':
return 'img folder';
case 'string':
return 'img type-string';
case 'hash':
return 'img type-hash';
case 'set':
return 'img type-set';
case 'list':
return 'img type-list';
case 'zset':
return 'img type-zset';
case 'stream':
return 'img type-stream';
case 'binary':
return 'img type-binary';
case 'ReJSON-RL':
return 'img type-rejson';
default:
return null;
}
}
exports.getIconForRedisType = getIconForRedisType;
function isWktGeometry(s) {
if (!(0, isString_1.default)(s))
return false;
// return !!s.match(/^POINT\s*\(|/)
return !!s.match(/^POINT\s*\(|^LINESTRING\s*\(|^POLYGON\s*\(|^MULTIPOINT\s*\(|^MULTILINESTRING\s*\(|^MULTIPOLYGON\s*\(|^GEOMCOLLECTION\s*\(|^GEOMETRYCOLLECTION\s*\(/);
}
exports.isWktGeometry = isWktGeometry;
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = [].slice.call(new Uint8Array(buffer));
bytes.forEach(b => (binary += String.fromCharCode(b)));
return btoa(binary);
}
exports.arrayBufferToBase64 = arrayBufferToBase64;
function getAsImageSrc(obj) {
if ((obj === null || obj === void 0 ? void 0 : obj.type) == 'Buffer' && (0, isArray_1.default)(obj === null || obj === void 0 ? void 0 : obj.data)) {
return `data:image/png;base64, ${arrayBufferToBase64(obj === null || obj === void 0 ? void 0 : obj.data)}`;
}
if ((0, isString_1.default)(obj) && (obj.startsWith('http://') || obj.startsWith('https://'))) {
return obj;
}
return null;
}
exports.getAsImageSrc = getAsImageSrc;
function parseSqlDefaultValue(value) {
if (!value)
return undefined;
if (!(0, isString_1.default)(value))
return undefined;
if (value.startsWith("'") && value.endsWith("'")) {
return value.slice(1, -1);
}
if (!isNaN(value) && !isNaN(parseFloat(value))) {
return parseFloat(value);
}
return undefined;
}
exports.parseSqlDefaultValue = parseSqlDefaultValue;
function detectCellDataType(value) {
if (value === null)
return 'null';
if (value === null || value === void 0 ? void 0 : value.$oid)
return 'objectid';
if (value === null || value === void 0 ? void 0 : value.$date)
return 'date';
if ((0, isString_1.default)(value))
return 'string';
if ((0, isNumber_1.default)(value))
return 'number';
if ((0, isPlainObject_1.default)(value))
return 'object';
if ((0, isArray_1.default)(value))
return 'array';
if (value === true || value === false)
return 'boolean';
return 'unknown';
}
exports.detectCellDataType = detectCellDataType;
function detectTypeIcon(value) {
switch (detectCellDataType(value)) {
case 'null':
return 'icon type-null';
case 'objectid':
return 'icon type-objectid';
case 'date':
return 'icon type-date';
case 'string':
return 'icon type-string';
case 'number':
return 'icon type-number';
case 'object':
return 'icon type-object';
case 'array':
return 'icon type-array';
case 'boolean':
return 'icon type-boolean';
default:
return 'icon type-unknown';
}
}
exports.detectTypeIcon = detectTypeIcon;
function getConvertValueMenu(value, onSetValue, editorTypes) {
return [
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportStringType) && {
text: 'String',
onClick: () => onSetValue(stringifyCellValue(value, 'stringConversionIntent', editorTypes).value),
},
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportNumberType) && { text: 'Number', onClick: () => onSetValue(parseFloat(value)) },
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportNullType) && { text: 'Null', onClick: () => onSetValue(null) },
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportBooleanType) && {
text: 'Boolean',
onClick: () => { var _a; return onSetValue(((_a = value === null || value === void 0 ? void 0 : value.toString()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) == 'true' || value == '1'); },
},
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportObjectIdType) && {
text: 'ObjectId',
onClick: () => onSetValue(parseFunc_ObjectIdAsDollar(value)),
},
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportDateType) && { text: 'Date', onClick: () => onSetValue(parseFunc_DateAsDollar(value)) },
(editorTypes === null || editorTypes === void 0 ? void 0 : editorTypes.supportJsonType) && {
text: 'JSON',
onClick: () => {
const jsonValue = safeJsonParse(value);
if (jsonValue != null) {
onSetValue(jsonValue);
}
},
},
];
}
exports.getConvertValueMenu = getConvertValueMenu;
function extractErrorMessage(err, defaultMessage = 'Unknown error') {
if (!err) {
return defaultMessage;
}
if ((0, isArray_1.default)(err.errors)) {
try {
return err.errors.map(x => x.message).join('\n');
}
catch (e2) { }
}
if (err.message) {
return err.message;
}
const s = `${err}`;
if (s && (!s.endsWith('Error') || s.includes(' '))) {
return s;
}
return defaultMessage;
}
exports.extractErrorMessage = extractErrorMessage;
function extractErrorStackTrace(err) {
const { stack } = err;
if (!(0, isString_1.default)(stack))
return undefined;
if (stack.length > 1000)
return stack.substring(0, 1000) + '... (truncated)';
return stack;
}
exports.extractErrorStackTrace = extractErrorStackTrace;
function extractErrorLogData(err, additionalFields = {}) {
if (!err)
return null;
return {
errorMessage: extractErrorMessage(err),
errorObject: err,
errorStack: extractErrorStackTrace(err),
...additionalFields,
};
}
exports.extractErrorLogData = extractErrorLogData;
function safeFormatDate(date) {
try {
const v = new Date(date);
return v.toISOString().substring(0, 10);
}
catch (e) {
return date === null || date === void 0 ? void 0 : date.toString();
}
}
exports.safeFormatDate = safeFormatDate;
function getLimitedQuery(sql) {
if (!sql) {
return sql;
}
if (sql.length > 1000) {
return sql.substring(0, 1000) + '...';
}
return sql;
}
exports.getLimitedQuery = getLimitedQuery;
function pinoLogRecordToMessageRecord(logRecord, defaultSeverity = 'info') {
var _a;
const { level, time, msg, ...rest } = logRecord;
const levelToSeverity = {
10: 'debug',
20: 'debug',
30: 'info',
40: 'info',
50: 'error',
60: 'error',
};
return {
...rest,
time,
message: msg,
severity: (_a = levelToSeverity[level]) !== null && _a !== void 0 ? _a : defaultSeverity,
};
}
exports.pinoLogRecordToMessageRecord = pinoLogRecordToMessageRecord;
function jsonLinesStringify(jsonArray) {
return jsonArray.map(json => JSON.stringify(json)).join('\n');
}
exports.jsonLinesStringify = jsonLinesStringify;
function jsonLinesParse(jsonLines) {
return jsonLines
.split('\n')
.filter(x => x.trim())
.map(line => {
try {
return JSON.parse(line);
}
catch (e) {
return null;
}
})
.filter(x => x);
}
exports.jsonLinesParse = jsonLinesParse;
function serializeJsTypesForJsonStringify(obj, replacer = null) {
return (0, cloneDeepWith_1.default)(obj, value => {
if (typeof value === 'bigint') {
return { $bigint: value.toString() };
}
if (replacer) {
return replacer(value);
}
});
}
exports.serializeJsTypesForJsonStringify = serializeJsTypesForJsonStringify;
function deserializeJsTypesFromJsonParse(obj) {
return (0, cloneDeepWith_1.default)(obj, value => {
if (value === null || value === void 0 ? void 0 : value.$bigint) {
return BigInt(value.$bigint);
}
});
}
exports.deserializeJsTypesFromJsonParse = deserializeJsTypesFromJsonParse;
function serializeJsTypesReplacer(key, value) {
if (typeof value === 'bigint') {
return { $bigint: value.toString() };
}
return value;
}
exports.serializeJsTypesReplacer = serializeJsTypesReplacer;
function deserializeJsTypesReviver(key, value) {
if (value === null || value === void 0 ? void 0 : value.$bigint) {
return BigInt(value.$bigint);
}
return value;
}
exports.deserializeJsTypesReviver = deserializeJsTypesReviver;
function parseNumberSafe(value) {
if (/^-?[0-9]+$/.test(value)) {
const parsed = parseInt(value);
if (Number.isSafeInteger(parsed)) {
return parsed;
}
return BigInt(value);
}
return parseFloat(value);
}
exports.parseNumberSafe = parseNumberSafe;
const frontMatterRe = /^--\ >>>[ \t\r]*\n(.*)\n-- <<<[ \t\r]*\n/s;
function getSqlFrontMatter(text, yamlModule) {
if (!text || !(0, isString_1.default)(text))
return null;
const match = text.match(frontMatterRe);
if (!match)
return null;
const yamlContentMapped = match[1].replace(/^--[ ]?/gm, '');
return yamlModule.load(yamlContentMapped);
}
exports.getSqlFrontMatter = getSqlFrontMatter;
function removeSqlFrontMatter(text) {
if (!text || !(0, isString_1.default)(text))
return null;
return text.replace(frontMatterRe, '');
}
exports.removeSqlFrontMatter = removeSqlFrontMatter;
function setSqlFrontMatter(text, data, yamlModule) {
const textClean = removeSqlFrontMatter(text);
if (!(0, isPlainObject_2.default)(data)) {
return textClean;
}
const dataClean = (0, omitBy_1.default)(data, v => v === undefined);
if ((0, isEmpty_1.default)(dataClean)) {
return textClean;
}
const yamlContent = yamlModule.dump(dataClean);
const yamlContentMapped = yamlContent
.trimRight()
.split('\n')
.map(line => '-- ' + line)
.join('\n');
const frontMatterContent = `-- >>>\n${yamlContentMapped}\n-- <<<\n`;
return frontMatterContent + (textClean || '');
}
exports.setSqlFrontMatter = setSqlFrontMatter;