diff --git a/README.md b/README.md index c64ef9f1..16b5c14d 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,39 @@ gl.enableVertexAttribArray(location); See JSDoc comments in `src/Float16Array.mjs` for details. If you don't write hacky code, you shouldn't have any problems. + +## `Float16Array` Custom inspection + +Provides custom inspection for Node.js and Deno, which makes the results of `console.log` more readable. + +### Node.js + +```js +// ES Modules +import { Float16Array } from "@petamoriken/float16"; +import { customInspect } from "@petamoriken/float16/inspect"; + +Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect; +``` + +```js +// CommonJS +const { Float16Array } = require("@petamoriken/float16"); +const { customInspect } = require("@petamoriken/float16/inspect"); + +Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect; +``` + +### Deno + +```ts +import { Float16Array } from "https://deno.land/x/float16/mod.ts"; +import { customInspect } from "https://deno.land/x/float16/inspect.ts"; + +// deno-lint-ignore no-explicit-any +(Float16Array.prototype as any)[Symbol.for("Deno.customInspect")] = customInspect; +``` + ## Build First, download devDependencies. diff --git a/inspect.ts b/inspect.ts new file mode 100644 index 00000000..4624a3cd --- /dev/null +++ b/inspect.ts @@ -0,0 +1 @@ +export * from "./inspect/deno.ts"; diff --git a/inspect/deno.ts b/inspect/deno.ts new file mode 100644 index 00000000..d1f3bec4 --- /dev/null +++ b/inspect/deno.ts @@ -0,0 +1,29 @@ +const DEFAULT_INSPECT_OPTIONS = { + depth: 4, + indentLevel: 0, + sorted: false, + trailingComma: false, + compact: true, + iterableLimit: 100, + showProxy: false, + colors: false, + getters: false, + showHidden: false, +}; + +/** + * @example + * ``` + * // deno-lint-ignore no-explicit-any + * (Float16Array.prototype as any)[Symbol.for("Deno.customInspect")] = customInspect; + * ``` + * @todo Current custom inspector cannot access options. See [enhance custom inspect API proposal](https://github.com/denoland/deno/issues/8099) + */ +export function customInspect(inspect: typeof Deno.inspect): string { + const array = []; + for (let i = 0; i < this.length; ++i) { + array[i] = this[i]; + } + + return `Float16Array(${this.length}) ${inspect(array, DEFAULT_INSPECT_OPTIONS)}`; +} diff --git a/inspect/node.cjs b/inspect/node.cjs new file mode 100644 index 00000000..30a789a3 --- /dev/null +++ b/inspect/node.cjs @@ -0,0 +1,20 @@ +/* eslint-env node */ + +"use strict"; + +const util = require("util"); + +/** + * @example + * ``` + * Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect; + * ``` + */ +module.exports.customInspect = function customInspect(_deps, options) { + const array = []; + for (let i = 0; i < this.length; ++i) { + array[i] = this[i]; + } + + return `Float16Array(${this.length}) ${util.inspect(array, options)}`; +}; diff --git a/inspect/node.mjs b/inspect/node.mjs new file mode 100644 index 00000000..16a628b8 --- /dev/null +++ b/inspect/node.mjs @@ -0,0 +1,18 @@ +/* eslint-env node */ + +import util from "util"; + +/** + * @example + * ``` + * Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect; + * ``` + */ +export function customInspect(_deps, options) { + const array = []; + for (let i = 0; i < this.length; ++i) { + array[i] = this[i]; + } + + return `Float16Array(${this.length}) ${util.inspect(array, options)}`; +} diff --git a/package.json b/package.json index 3fcfd52e..12ba870d 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,14 @@ "main": "./lib/index.cjs", "module": "./src/index.mjs", "exports": { - "require": "./lib/index.cjs", - "import": "./src/index.mjs" + ".": { + "import": "./src/index.mjs", + "require": "./lib/index.cjs" + }, + "./inspect": { + "import": "./inspect/node.mjs", + "require": "./inspect/node.cjs" + } }, "types": "index.d.ts", "sideEffects": false, @@ -24,6 +30,8 @@ "src", "lib", "browser", + "inspect/node.mjs", + "inspect/node.cjs", "index.d.ts" ], "keywords": [