Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bbgse committed Nov 4, 2023
1 parent 0e8ce54 commit c26b5bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -50,8 +48,5 @@
"typescript": "^5.2.2",
"unbuild": "^2.0.0",
"vitest": "^0.34.6"
},
"simple-git-hooks": {
"pre-commit": "npx prettier --check ."
}
}
55 changes: 14 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, InspectFn<any>> = {
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<CtorFn, InspectFn>();
const stringTagMap: Record<string, InspectFn> = {};
const baseTypesMap: Record<string, InspectFn> = {
const inspectors: Record<string, InspectFn> = {
undefined: (_, options) => options.colorize("undefined", "undefined"),
null: (_, options) => options.colorize("null", "null"),

Expand All @@ -65,7 +48,6 @@ const baseTypesMap: Record<string, InspectFn> = {
Function: inspectFunction,

symbol: inspectSymbol,
// A Symbol polyfill will return `Symbol` not `symbol` from typedetect
Symbol: inspectSymbol,

Array: inspectArray,
Expand Down Expand Up @@ -97,36 +79,32 @@ const baseTypesMap: Record<string, InspectFn> = {
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<Options> = {},
opts: Partial<Options> = {}
): 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);
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit c26b5bf

Please sign in to comment.