Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(associative): re-add support for nodejs REPL inspection
- custom Set/Map impls were showing up as empty in the REPL in recent Node versions (13.x) - adding @Inspectable decorator mixin for all impls to provide hook for Node's inspection mechanism - Ref: nodejs/node#32529
- Loading branch information
1 parent
9017761
commit 49024f7
Showing
10 changed files
with
90 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { mixin } from "@thi.ng/api"; | ||
import { isNode } from "@thi.ng/checks"; | ||
import { map } from "@thi.ng/transducers"; | ||
|
||
const inspect = isNode() ? require("util").inspect : null; | ||
|
||
const inspectSet = (coll: Set<any>, opts: any) => | ||
[...map((x) => inspect(x, opts), coll)].join(", "); | ||
|
||
const inspectMap = (coll: Map<any, any>, opts: any) => | ||
[ | ||
...map(([k, v]) => `${inspect(k, opts)} => ${inspect(v, opts)}`, coll), | ||
].join(", "); | ||
|
||
/** | ||
* NodeJS inspection mixin | ||
* | ||
* @remarks | ||
* Reference: | ||
* https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects | ||
* | ||
* @internal | ||
*/ | ||
export const inspectable = mixin({ | ||
[Symbol.for("nodejs.util.inspect.custom")](depth: number, opts: any) { | ||
const name = this[Symbol.toStringTag]; | ||
const childOpts = { | ||
...opts, | ||
depth: opts.depth === null ? null : opts.depth - 1, | ||
}; | ||
return depth >= 0 | ||
? [ | ||
`${name}(${this.size || 0}) {`, | ||
inspect | ||
? this instanceof Set | ||
? inspectSet(this, childOpts) | ||
: this instanceof Map | ||
? inspectMap(this, childOpts) | ||
: "" | ||
: "", | ||
"}", | ||
].join(" ") | ||
: opts.stylize(`[${name}]`, "special"); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters