@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
69 lines (60 loc) • 2.33 kB
JavaScript
import { isPrimitive } from '../utils/is.js';
import { normalize } from '../utils/normalize.js';
import { GLOBAL_OBJ } from '../utils/worldwide.js';
/**
* Formats the given values into a string.
*
* @param values - The values to format.
* @param normalizeDepth - The depth to normalize the values.
* @param normalizeMaxBreadth - The max breadth to normalize the values.
* @returns The formatted string.
*/
function formatConsoleArgs(values, normalizeDepth, normalizeMaxBreadth) {
return 'util' in GLOBAL_OBJ && typeof (GLOBAL_OBJ ).util.format === 'function'
? (GLOBAL_OBJ ).util.format(...values)
: safeJoinConsoleArgs(values, normalizeDepth, normalizeMaxBreadth);
}
/**
* Joins the given values into a string.
*
* @param values - The values to join.
* @param normalizeDepth - The depth to normalize the values.
* @param normalizeMaxBreadth - The max breadth to normalize the values.
* @returns The joined string.
*/
function safeJoinConsoleArgs(values, normalizeDepth, normalizeMaxBreadth) {
return values
.map(value =>
isPrimitive(value) ? String(value) : JSON.stringify(normalize(value, normalizeDepth, normalizeMaxBreadth)),
)
.join(' ');
}
/**
* Checks if a string contains console substitution patterns like %s, %d, %i, %f, %o, %O, %c.
*
* @param str - The string to check
* @returns true if the string contains console substitution patterns
*/
function hasConsoleSubstitutions(str) {
// Match console substitution patterns: %s, %d, %i, %f, %o, %O, %c
return /%[sdifocO]/.test(str);
}
/**
* Creates template attributes for multiple console arguments.
*
* @param args - The console arguments
* @returns An object with template and parameter attributes
*/
function createConsoleTemplateAttributes(firstArg, followingArgs) {
const attributes = {};
// Create template with placeholders for each argument
const template = new Array(followingArgs.length).fill('{}').join(' ');
attributes['sentry.message.template'] = `${firstArg} ${template}`;
// Add each argument as a parameter
followingArgs.forEach((arg, index) => {
attributes[`sentry.message.parameter.${index}`] = arg;
});
return attributes;
}
export { createConsoleTemplateAttributes, formatConsoleArgs, hasConsoleSubstitutions, safeJoinConsoleArgs };
//# sourceMappingURL=utils.js.map