UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

77 lines (76 loc) 3.02 kB
import { check } from '@augment-vir/assert'; import { extractErrorMessage, perEnv, RuntimeEnv, stringify, } from '@augment-vir/core'; import { filterMap } from '../array/filter.js'; import { removeSuffix } from '../string/suffix.js'; import { LogColorKey } from './log-colors.js'; async function createToLogString() { return await perEnv({ /** We calculate coverage in web, so the node code will never run in coverage tests. */ /* node:coverage disable */ async [RuntimeEnv.Node]() { const { inspect } = await import('node:util'); return ({ args, colorKey, options }) => { const argStrings = args.map((arg) => { if (typeof arg === 'string') { return arg; } else { return inspect(arg); } }); const colorsString = options.omitColors ? '' : options.colorConfig[colorKey].colors.join(''); const text = [ colorsString, argStrings.join('\n'), options.omitColors ? '' : options.colorConfig[LogColorKey.Reset].colors.join(''), ].join(''); return { text, css: undefined }; }; }, /** * We have no way to test color output in the browser console so this block is ignored in * coverage as well. */ [RuntimeEnv.Web]() { return ({ args, colorKey, options }) => { const css = options.omitColors ? undefined : filterMap(options.colorConfig[colorKey].colors, (cssString) => removeSuffix({ value: cssString, suffix: ';', }), check.isTruthy).join('; '); const argStrings = args.map((arg) => { if (typeof arg === 'string') { return arg; } else if (arg instanceof Error) { return extractErrorMessage(arg); } else { return stringify(arg); } }); const text = [ argStrings.join('\n'), options.omitColors ? '' : options.colorConfig[LogColorKey.Reset].colors.join(''), ].join(''); return { text, css }; }; }, /* node:coverage enable */ }); } /** * Converts log arguments into a single {@link LogWriterParams}. * * @category Log : Util * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export const toLogString = await createToLogString();