diff --git a/package.json b/package.json index cda2b1f..42562c9 100644 --- a/package.json +++ b/package.json @@ -25,15 +25,13 @@ ], "scripts": { "test": "vitest", - "compile": "tsc -p tsconfig.json", + "compile": "tsc -p tsconfig.json --noEmit", "clean": "rm -rf lib", - "build": "npm run clean && npm run build:cjs && npm run build:types", - "build:esm": "esbuild src/index.ts --outfile=lib/index.mjs --sourcemap --bundle --platform=node --target=node18 --format=esm", - "build:cjs": "esbuild src/index.ts --outfile=lib/index.cjs --sourcemap --bundle --platform=node --target=node18 --format=cjs", - "build:types": "tsc -p tsconfig-types.json", + "build": "unbuild", "release": "bumpp && npm publish", "prepare": "simple-git-hooks", - "format": "prettier --write .", + "format:all": "prettier --write .", + "format:staged": "git diff --name-only HEAD | xargs -I {} prettier --write \"{}\"", "prepublishOnly": "npm run build" }, "dependencies": { @@ -50,8 +48,5 @@ "typescript": "^5.2.2", "unbuild": "^2.0.0", "vitest": "^0.34.6" - }, - "simple-git-hooks": { - "pre-commit": "npx prettier --check ." } } diff --git a/src/index.ts b/src/index.ts index a115cb3..26526a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,33 +18,16 @@ import inspectError from "./error.js"; import { mergeOptions, type InspectFn, type Options } from "./options.js"; import { getTypeName, - isClass, isInstanceOfClass, isObject, isPlainObject, - toString, } from "./helpers.js"; interface CtorFn { new (...args: unknown[]): unknown; } -const inspectors: Record> = { - undefined: (value, options) => options.colorize("undefined", "undefined"), - null: (_, options) => options.colorize("null", "null"), - boolean: (value: boolean, options) => - options.colorize(String(value), "boolean"), - number: inspectNumber, - bigint: inspectBigInt, - string: inspectString, - function: inspectFunction, - symbol: inspectSymbol, - Array: inspectArray, -}; - -const constructorMap = new WeakMap(); -const stringTagMap: Record = {}; -const baseTypesMap: Record = { +const inspectors: Record = { undefined: (_, options) => options.colorize("undefined", "undefined"), null: (_, options) => options.colorize("null", "null"), @@ -65,7 +48,6 @@ const baseTypesMap: Record = { Function: inspectFunction, symbol: inspectSymbol, - // A Symbol polyfill will return `Symbol` not `symbol` from typedetect Symbol: inspectSymbol, Array: inspectArray, @@ -97,36 +79,32 @@ const baseTypesMap: Record = { ArrayBuffer: () => "", Error: inspectError, -} as const; +}; type CustomInspectValue = any | { inspect?: InspectFn }; const inspectCustom = ( value: CustomInspectValue, options: Options, - type: string, + type: string ): string => { if ("inspect" in value && typeof value.inspect === "function") { return value.inspect(options.depth, options); } - if (stringTagMap[type]) { - return stringTagMap[type](value, options); - } - return ""; }; export const inspect = ( value: unknown, - opts: Partial = {}, + opts: Partial = {} ): string => { const type = getTypeName(value); const options = mergeOptions({ ...opts, inspect }); // If it is a base value that we already support - if (type in baseTypesMap) { - const fn = baseTypesMap[type as keyof typeof baseTypesMap]; + if (type in inspectors) { + const fn = inspectors[type as keyof typeof inspectors]; return fn(value, options); } @@ -158,22 +136,17 @@ export const inspect = ( return options.colorize(String(value), "string"); }; -export function registerInspector(ctor: CtorFn, inspector: InspectFn) { - if (isClass(ctor)) { - return false; - } - if (constructorMap.has(ctor)) { +/** + * Register a custom inspector function for a given type. + * @param type The string tag of the object, ex "Array", "Map", "Set", etc. + * @param inspector The inspector function to use for the object + */ +export function registerInspector(type: string, inspector: InspectFn) { + if (type in inspectors) { return false; } - constructorMap.set(ctor, inspector); - return true; -} -export function registerStringTag(stringTag: string, inspector: InspectFn) { - if (stringTag in stringTagMap) { - return false; - } - stringTagMap[stringTag] = inspector; + inspectors[type] = inspector; return true; }