Files
bruno/packages/bruno-lang/v2/src/utils.js
2025-12-04 01:37:20 +05:30

79 lines
1.6 KiB
JavaScript

// safely parse json
const safeParseJson = (json) => {
try {
return JSON.parse(json);
} catch (e) {
return null;
}
};
const indentString = (str, levels = 1) => {
if (!str || !str.length) {
return str || '';
}
const indent = ' '.repeat(levels);
return str
.split(/\r\n|\r|\n/)
.map((line) => indent + line)
.join('\n');
};
const outdentString = (str, spaces = 2) => {
if (!str || !str.length) {
return str || '';
}
const spacesRegex = new RegExp(`^ {${spaces}}`);
return str
.split(/\r\n|\r|\n/)
.map((line) => line.replace(spacesRegex, ''))
.join('\n');
};
const getValueString = (value) => {
// Handle null, undefined, and empty strings
if (!value) {
return '';
}
const hasNewLines = value.includes('\n') || value.includes('\r');
if (!hasNewLines) {
return value;
}
// Wrap multiline values in triple quotes with 2-space indentation
return `'''\n${indentString(value)}\n'''`;
};
const getKeyString = (key) => {
const quotableChars = [':', '"', '{', '}', ' '];
return quotableChars.some((char) => key.includes(char)) ? ('"' + key.replaceAll('"', '\\"') + '"') : key;
};
const getValueUrl = (url) => {
// Handle null, undefined, and empty strings
if (!url) {
return '';
}
const hasNewLines = url.includes('\n') || url.includes('\r');
if (!hasNewLines) {
return url;
}
// Wrap multiline values in triple quotes with 4-space indentation (2 levels)
return `'''\n${indentString(url, 2)}\n'''`;
};
module.exports = {
safeParseJson,
indentString,
outdentString,
getValueString,
getKeyString,
getValueUrl
};