Skip to content

Commit

Permalink
Add custom inspection for Node.js and Deno
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Oct 9, 2021
1 parent 24d6855 commit 6ec5f2f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions inspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./inspect/deno.ts";
29 changes: 29 additions & 0 deletions inspect/deno.ts
Original file line number Diff line number Diff line change
@@ -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)}`;
}
20 changes: 20 additions & 0 deletions inspect/node.cjs
Original file line number Diff line number Diff line change
@@ -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)}`;
};
18 changes: 18 additions & 0 deletions inspect/node.mjs
Original file line number Diff line number Diff line change
@@ -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)}`;
}
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,6 +30,8 @@
"src",
"lib",
"browser",
"inspect/node.mjs",
"inspect/node.cjs",
"index.d.ts"
],
"keywords": [
Expand Down

0 comments on commit 6ec5f2f

Please sign in to comment.