@boost/internal
Version:
Boost internals.
94 lines (88 loc) • 2.82 kB
JavaScript
// Bundled with Packemon: https://packemon.dev
// Platform: browser, Support: stable, Format: esm
import debug from 'debug';
/* eslint-disable no-magic-numbers, sort-keys */
// https://github.com/chalk/ansi-styles/blob/master/index.js#L75
function createColor(open) {
return message => `\u001B[${open}m${String(message)}\u001B[39m`;
}
const color = {
// States
fail: createColor(31),
mute: createColor(90),
pass: createColor(32),
// Types
filePath: createColor(36),
moduleName: createColor(33),
projectName: createColor(34),
symbol: createColor(35)
};
function sentenceCase(value) {
return String(value).replace(/[A-Z]/gu, match => ` ${match.toLocaleLowerCase()}`).trim();
}
debug.formatters.S = sentenceCase;
function createInternalDebugger(namespace) {
return debug(`boost:${namespace}`);
}
const internalErrors = {
INVALID_SCOPE_NAME: 'Error scope must be 3 characters and all uppercase.',
UNKNOWN_ERROR: 'An unknown error has occurred.'
};
const TOKEN_PATTERN = /\{(\d+)\}/gu;
function createScopedError(scope, name, errors) {
function msg(code, messages, params = []) {
if (!messages[code]) {
return '';
}
return `${messages[code].replace(TOKEN_PATTERN, (match, index) => String(params[index]))} [${scope}:${code}]`;
}
if (process.env.NODE_ENV !== "production" && (scope.length !== 3 || scope !== scope.toUpperCase())) {
throw new Error(msg('INVALID_SCOPE_NAME', internalErrors));
}
return class InternalError extends Error {
constructor(code, params) {
super(msg(code, errors, params));
this.code = void 0;
this.scope = scope;
this.code = code;
this.name = name;
// If a message was not loaded, we are throwing an unknown error
if (!this.message) {
this.code = 'UNKNOWN_ERROR';
this.message = msg('UNKNOWN_ERROR', internalErrors);
}
}
};
}
let envVars = {};
if (global.process !== undefined) {
envVars = process.env;
} else if ('window' in global && global.window !== undefined) {
// @ts-expect-error Allow type mismatch
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
envVars = window;
}
function env(key, value) {
const name = `BOOSTJS_${key}`;
if (value === null) {
delete envVars[name];
return undefined;
}
if (typeof value === 'string') {
envVars[name] = value;
return value;
}
return envVars[name];
}
/**
* For compatibility with ES modules, this function is used to extract the
* default export from an incompatible module.
*/
function interopDefault(result) {
if (result && typeof result === 'object' && 'default' in result) {
return result.default;
}
return result;
}
export { color, createInternalDebugger, createScopedError, env, interopDefault, sentenceCase };
//# sourceMappingURL=index.js.map