unist-util-inspect
Advanced tools
Comparing version 6.0.1 to 7.0.0
@@ -1,1 +0,1 @@ | ||
module.exports = false | ||
export var color = false |
@@ -1,1 +0,1 @@ | ||
module.exports = true | ||
export var color = true |
133
index.js
@@ -1,9 +0,16 @@ | ||
'use strict' | ||
import {color} from './color.js' | ||
var color = require('./color') | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Point} Point | ||
* | ||
* @typedef {Object} InspectOptions | ||
* @property {boolean} [showPositions=true] | ||
*/ | ||
module.exports = color ? inspect : /* istanbul ignore next */ noColor | ||
/* c8 ignore next */ | ||
export var inspect = color ? inspectColor : inspectNoColor | ||
inspect.color = noColor.color = inspect | ||
inspect.noColor = noColor.noColor = noColor | ||
var own = {}.hasOwnProperty | ||
@@ -16,22 +23,46 @@ var bold = ansiColor(1, 22) | ||
// ANSI color regex. | ||
/* eslint-disable-next-line no-control-regex */ | ||
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g | ||
// Inspects a node, without using color. | ||
function noColor(node) { | ||
return inspect(node).replace(colorExpression, '') | ||
/** | ||
* Inspects a node, without using color. | ||
* | ||
* @param {unknown} node | ||
* @param {InspectOptions} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectNoColor(node, options) { | ||
return inspectColor(node, options).replace(colorExpression, '') | ||
} | ||
// Inspects a node. | ||
function inspect(tree, options) { | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @param {unknown} tree | ||
* @param {InspectOptions} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectColor(tree, options) { | ||
var positions = | ||
!options || options.showPositions == null ? true : options.showPositions | ||
!options || | ||
options.showPositions === null || | ||
options.showPositions === undefined | ||
? true | ||
: options.showPositions | ||
return inspectValue(tree) | ||
/** | ||
* @param {unknown} node | ||
* @returns {string} | ||
*/ | ||
function inspectValue(node) { | ||
if (node && typeof node === 'object' && 'length' in node) { | ||
// @ts-ignore looks like a list of nodes. | ||
return inspectNodes(node) | ||
} | ||
// @ts-ignore looks like a single node. | ||
if (node && node.type) { | ||
// @ts-ignore looks like a single node. | ||
return inspectTree(node) | ||
@@ -43,2 +74,6 @@ } | ||
/** | ||
* @param {unknown} value | ||
* @returns {string} | ||
*/ | ||
function inspectNonTree(value) { | ||
@@ -48,3 +83,8 @@ return JSON.stringify(value) | ||
/** | ||
* @param {Node[]} nodes | ||
* @returns {string} | ||
*/ | ||
function inspectNodes(nodes) { | ||
/** @type {Array.<string>} */ | ||
var result = [] | ||
@@ -68,9 +108,20 @@ var index = -1 | ||
/** | ||
* @param {Object.<string, unknown>} object | ||
* @returns {string} | ||
*/ | ||
function inspectFields(object) { | ||
/** @type {Array.<string>} */ | ||
var result = [] | ||
/** @type {string} */ | ||
var key | ||
/** @type {unknown} */ | ||
var value | ||
/** @type {string} */ | ||
var formatted | ||
for (key in object) { | ||
/* c8 ignore next 1 */ | ||
if (!own.call(object, key)) continue | ||
value = object[key] | ||
@@ -96,2 +147,3 @@ | ||
typeof value === 'object' && | ||
// @ts-ignore looks like a node. | ||
value.type && | ||
@@ -102,2 +154,3 @@ key !== 'data' && | ||
) { | ||
// @ts-ignore looks like a node. | ||
formatted = inspectTree(value) | ||
@@ -113,2 +166,3 @@ } | ||
) { | ||
// @ts-ignore looks like a list of nodes. | ||
formatted = '\n' + inspectNodes(value) | ||
@@ -126,9 +180,15 @@ } else { | ||
result.join('\n'), | ||
(object.children && object.children.length ? dim('│') : ' ') + ' ' | ||
// @ts-ignore looks like a parent node. | ||
(object.children && object.children.length > 0 ? dim('│') : ' ') + ' ' | ||
) | ||
} | ||
function inspectTree(node, pad) { | ||
var result = [formatNode(node, pad)] | ||
/** | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
function inspectTree(node) { | ||
var result = [formatNode(node)] | ||
var fields = inspectFields(node) | ||
// @ts-ignore looks like a parent. | ||
var content = inspectNodes(node.children || []) | ||
@@ -140,3 +200,8 @@ if (fields) result.push(fields) | ||
// Colored node formatter. | ||
/** | ||
* Colored node formatter. | ||
* | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
function formatNode(node) { | ||
@@ -152,5 +217,6 @@ var result = [bold(node.type)] | ||
if (node.children) { | ||
// @ts-ignore looks like a parent. | ||
result.push(dim('['), yellow(node.children.length), dim(']')) | ||
} else if (typeof node.value === 'string') { | ||
result.push(' ', green(inspectNonTree(node.value, ''))) | ||
result.push(' ', green(inspectNonTree(node.value))) | ||
} | ||
@@ -166,2 +232,8 @@ | ||
/** | ||
* @param {string} value | ||
* @param {string} indentation | ||
* @param {boolean} [ignoreFirst=false] | ||
* @returns {string} | ||
*/ | ||
function indent(value, indentation, ignoreFirst) { | ||
@@ -180,7 +252,15 @@ var lines = value.split('\n') | ||
// Compile a position. | ||
/** | ||
* @param {Position} value | ||
* @returns {string} | ||
*/ | ||
function stringifyPosition(value) { | ||
/** @type {Position} */ | ||
// @ts-ignore | ||
var position = value || {} | ||
/** @type {Array.<string>} */ | ||
var result = [] | ||
/** @type {Array.<string>} */ | ||
var positions = [] | ||
/** @type {Array.<string>} */ | ||
var offsets = [] | ||
@@ -191,7 +271,10 @@ | ||
if (positions.length) result.push(positions.join('-')) | ||
if (offsets.length) result.push(offsets.join('-')) | ||
if (positions.length > 0) result.push(positions.join('-')) | ||
if (offsets.length > 0) result.push(offsets.join('-')) | ||
return result.join(', ') | ||
/** | ||
* @param {Point} value | ||
*/ | ||
function point(value) { | ||
@@ -208,6 +291,16 @@ if (value) { | ||
// Factory to wrap values in ANSI colours. | ||
/** | ||
* Factory to wrap values in ANSI colours. | ||
* | ||
* @param {number} open | ||
* @param {number} close | ||
* @returns {function(string): string} | ||
*/ | ||
function ansiColor(open, close) { | ||
return color | ||
/** | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
function color(value) { | ||
@@ -214,0 +307,0 @@ return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm' |
{ | ||
"name": "unist-util-inspect", | ||
"version": "6.0.1", | ||
"version": "7.0.0", | ||
"description": "unist utility to inspect nodes", | ||
@@ -26,8 +26,5 @@ "license": "MIT", | ||
], | ||
"files": [ | ||
"index.js", | ||
"color.js", | ||
"color-browser.js", | ||
"types/index.d.ts" | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"browser": { | ||
@@ -39,11 +36,19 @@ "./color.js": "./color-browser.js" | ||
}, | ||
"types": "types/index.d.ts", | ||
"dependencies": {}, | ||
"types": "index.d.ts", | ||
"files": [ | ||
"color.d.ts", | ||
"color.js", | ||
"color-browser.d.ts", | ||
"color-browser.js", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"@types/unist": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/unist": "^2.0.0", | ||
"browserify": "^17.0.0", | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"chalk": "^4.0.0", | ||
"dtslint": "^4.0.0", | ||
"hastscript": "^6.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^2.0.0", | ||
@@ -53,19 +58,19 @@ "remark-cli": "^9.0.0", | ||
"retext": "^7.0.0", | ||
"rimraf": "^3.0.0", | ||
"strip-ansi": "^6.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-builder": "^2.0.0", | ||
"xast-util-from-xml": "^1.0.0", | ||
"xastscript": "^2.0.0", | ||
"xast-util-from-xml": "^1.0.0", | ||
"xo": "^0.34.0" | ||
"xo": "^0.38.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify . -s unistUtilInspect > unist-util-inspect.js", | ||
"build-mangle": "browserify . -s unistUtilInspect -p tinyify > unist-util-inspect.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test-types": "dtslint types", | ||
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types" | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
@@ -82,31 +87,9 @@ "prettier": { | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"complexity": "off", | ||
"eqeqeq": [ | ||
"error", | ||
"always", | ||
{ | ||
"null": "ignore" | ||
} | ||
], | ||
"guard-for-in": "off", | ||
"no-control-regex": "off", | ||
"no-eq-null": "off", | ||
"no-multi-assign": "off", | ||
"unicorn/explicit-length-check": "off", | ||
"unicorn/prefer-number-properties": "off", | ||
"unicorn/prefer-includes": "off" | ||
}, | ||
"ignore": [ | ||
"types", | ||
"unist-util-inspect.js" | ||
] | ||
"import/no-mutable-exports": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"remarkConfig": { | ||
@@ -116,3 +99,8 @@ "plugins": [ | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -15,2 +15,5 @@ # unist-util-inspect | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -55,2 +58,6 @@ | ||
This package exports the following identifiers: `inspect`, `inspectColor`, and | ||
`inspectNoColor`. | ||
There is no default export. | ||
### `inspect(node[, options])` | ||
@@ -70,9 +77,10 @@ | ||
### `inspect.<style>[.<style>…](node[, options])` | ||
### `inspectColor(node[, options])` | ||
Where `<style>` is either `color` or `noColor`. | ||
Inspect, with ANSI color sequences (default in Node). | ||
To explicitly add or remove ANSI sequences, use `inspect.color(node)` or | ||
`inspect.noColor(node)`. | ||
### `inspectNoColor(node[, options])` | ||
Inspect, but without ANSI color sequences (default in browser). | ||
## Contribute | ||
@@ -94,5 +102,5 @@ | ||
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-inspect.svg | ||
[build-badge]: https://github.com/syntax-tree/unist-util-inspect/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/syntax-tree/unist-util-inspect | ||
[build]: https://github.com/syntax-tree/unist-util-inspect/actions | ||
@@ -99,0 +107,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-inspect.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14654
9
289
140
Yes
1
1
+ Added@types/unist@^2.0.0
+ Added@types/unist@2.0.11(transitive)