@lou.codes/test
Version:
✅ Equality test with enforced readability
47 lines (46 loc) • 2.59 kB
JavaScript
import { foregroundBlue, foregroundBrightGreen, foregroundBrightRed, foregroundYellow, } from "@lou.codes/ansi";
import { isArray } from "@lou.codes/constants/Array.js";
import { entries } from "@lou.codes/constants/Object.js";
import { asyncIterator, iterator } from "@lou.codes/constants/Symbol.js";
import { EMPTY_STRING } from "@lou.codes/constants/empty.js";
import { formatValue } from "./formatValue.js";
/**
* Dictionary type->formatter to be used by `formatValue`.
*
* @category Output
*/
export const formatValueDictionary = {
bigint: (value) => `${foregroundBrightGreen `${value}`}${foregroundBlue `n`}`,
boolean: (value) => foregroundBlue `${value}`,
function: () => foregroundYellow `Function`,
null: () => foregroundBlue `null`,
number: (value) => foregroundBrightGreen `${value}`,
object: (value) => isArray(value) ?
`${foregroundBrightGreen `Array`}([ ${value
.map(formatValue)
.join(", ")} ])`
: value instanceof Date ?
`${foregroundBrightGreen `Date`}(${foregroundBrightRed `"${value.toISOString()}"`})`
: value instanceof RegExp ?
`${foregroundBrightGreen `RegExp`}(${foregroundBrightRed(value.toString())})`
: value instanceof URL ?
`${foregroundBrightGreen `URL`}(${foregroundBrightRed `"${value.href}"`})`
: value instanceof Error ?
`${foregroundBrightGreen(value.name)}(${foregroundBrightRed `"${value.message}"`})`
: `${foregroundBrightGreen((value.constructor?.name ?? "") ||
(iterator in value ? "Iterable"
: asyncIterator in value ? "AsyncIterable"
: "Object"))}({ ${((typeof value.entries ===
"function") ?
[...value.entries()]
: entries(value))
.map(([key, propertyValue]) => `${foregroundBrightRed `"${key}"`}: ${formatValue(propertyValue)}`)
.join(", ") || "…"} })`,
string: (value) => foregroundBrightRed `"${value.replaceAll(
// eslint-disable-next-line no-control-regex
/[\u001B\u009B][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/gu, EMPTY_STRING)}"`,
symbol: (value) => foregroundBrightGreen `Symbol${value.description === undefined ?
EMPTY_STRING
: `(${foregroundBrightRed `"${value.description}"`})`}`,
undefined: () => foregroundBlue `undefined`,
};