unist-util-position
Advanced tools
Comparing version 4.0.3 to 4.0.4
@@ -1,33 +0,1 @@ | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {NodeLike|Node} [node] | ||
* @returns {Position} | ||
*/ | ||
export function position(node?: NodeLike | Node): Position | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {NodeLike|Node} [node] | ||
* @returns {Point} | ||
*/ | ||
export function pointStart(node?: NodeLike | Node): Point | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {NodeLike|Node} [node] | ||
* @returns {Point} | ||
*/ | ||
export function pointEnd(node?: NodeLike | Node): Point | ||
export type Position = import('unist').Position | ||
export type Node = import('unist').Node | ||
export type NodeLike = Record<string, unknown> & { | ||
type: string | ||
position?: PositionLike | undefined | ||
} | ||
export type Point = import('unist').Point | ||
export type PointLike = Partial<Point> | ||
export type PositionLike = { | ||
start?: PointLike | ||
end?: PointLike | ||
} | ||
export {pointEnd, pointStart, position} from './lib/index.js' |
51
index.js
@@ -1,50 +0,1 @@ | ||
/** | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {Record<string, unknown> & {type: string, position?: PositionLike|undefined}} NodeLike | ||
* @typedef {import('unist').Point} Point | ||
* | ||
* @typedef {Partial<Point>} PointLike | ||
* | ||
* @typedef PositionLike | ||
* @property {PointLike} [start] | ||
* @property {PointLike} [end] | ||
*/ | ||
export const pointStart = point('start') | ||
export const pointEnd = point('end') | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {NodeLike|Node} [node] | ||
* @returns {Position} | ||
*/ | ||
export function position(node) { | ||
return {start: pointStart(node), end: pointEnd(node)} | ||
} | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {'start'|'end'} type | ||
*/ | ||
function point(type) { | ||
return point | ||
/** | ||
* Get the positional info of `node`. | ||
* | ||
* @param {NodeLike|Node} [node] | ||
* @returns {Point} | ||
*/ | ||
function point(node) { | ||
const point = (node && node.position && node.position[type]) || {} | ||
return { | ||
line: point.line || null, | ||
column: point.column || null, | ||
offset: point.offset > -1 ? point.offset : null | ||
} | ||
} | ||
} | ||
export {pointEnd, pointStart, position} from './lib/index.js' |
{ | ||
"name": "unist-util-position", | ||
"version": "4.0.3", | ||
"version": "4.0.4", | ||
"description": "unist utility to get the position of a node", | ||
@@ -30,2 +30,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -38,19 +39,17 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.48.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -57,0 +56,0 @@ }, |
172
readme.md
@@ -11,11 +11,44 @@ # unist-util-position | ||
[**unist**][unist] utility to get the positional info of nodes. | ||
[unist][] utility to get positional info of nodes. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`position(node)`](#positionnode) | ||
* [`pointEnd(node)`](#pointendnode) | ||
* [`pointStart(node)`](#pointstartnode) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This utility helps with accessing positional info on a potentially dirty tree. | ||
## When should I use this? | ||
The positional info is typically consistent and proper in unist trees generated | ||
by our ecosystem, but, user plugins could mess that up. | ||
If you’re making a reusable plugin, and accessing the positional info often, you | ||
might want to guard against that by using this utility. | ||
You might also find the utility [`unist-util-generated`][unist-util-generated] | ||
useful to check whether a node is considered to be generated (not in the | ||
original input file). | ||
You might also enjoy | ||
[`unist-util-stringify-position`][unist-util-stringify-position] when you want | ||
to display positional info to users. | ||
## Install | ||
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. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -25,9 +58,23 @@ npm install unist-util-position | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {position, pointStart, pointEnd} from 'https://esm.sh/unist-util-position@4' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {position, pointStart, pointEnd} from 'https://esm.sh/unist-util-position@4?bundle' | ||
</script> | ||
``` | ||
## Use | ||
```js | ||
import {remark} from 'remark' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
import {position, pointStart, pointEnd} from 'unist-util-position' | ||
const tree = remark().parse('# foo\n\n* bar\n') | ||
const tree = fromMarkdown('# foo\n\n* bar\n') | ||
@@ -37,6 +84,2 @@ console.log(position(tree)) | ||
console.log(pointEnd(tree)) | ||
console.log(position()) | ||
console.log(pointStart()) | ||
console.log(pointEnd()) | ||
``` | ||
@@ -50,5 +93,2 @@ | ||
{line: 4, column: 1, offset: 13} | ||
{start: {line: null, column: null, offset: null}, end: {line: null, column: null, offset: null}} | ||
{line: null, column: null, offset: null} | ||
{line: null, column: null, offset: null} | ||
``` | ||
@@ -58,22 +98,74 @@ | ||
This package exports the following identifiers: `position`, `pointStart`, and | ||
`pointEnd`. | ||
This package exports the identifiers [`pointEnd`][pointend], | ||
[`pointStart`][pointstart], and [`position`][position]. | ||
There is no default export. | ||
### `position(node?)` | ||
### `position(node)` | ||
Get the positional info of `node` ([`Node?`][node]). | ||
Returns [`Position`][position]. | ||
Get the positional info of `node`. | ||
### `pointStart(node?)` | ||
###### Parameters | ||
### `pointEnd(node?)` | ||
* `node` ([`Node`][node]) | ||
— node | ||
Get the start or end points in the positional info of `node` ([`Node?`][node]). | ||
Returns [`Point`][point]. | ||
###### Returns | ||
Position ([`Position`][unist-position]). | ||
### `pointEnd(node)` | ||
Get the ending point of `node`. | ||
###### Parameters | ||
* `node` ([`Node`][node]) | ||
— node | ||
###### Returns | ||
Point ([`Point`][unist-point]). | ||
### `pointStart(node)` | ||
Get the starting point of `node`. | ||
###### Parameters | ||
* `node` ([`Node`][node]) | ||
— node | ||
###### Returns | ||
Point ([`Point`][unist-point]). | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Related | ||
* [`unist-util-stringify-position`](https://github.com/syntax-tree/unist-util-stringify-position) | ||
— serialize a node, position, or point as a human readable location | ||
* [`unist-util-position-from-estree`](https://github.com/syntax-tree/unist-util-position-from-estree) | ||
— get a position from an estree node | ||
* [`unist-util-remove-position`](https://github.com/syntax-tree/unist-util-remove-position) | ||
— remove positions from tree | ||
* [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) | ||
— check if a node is generated | ||
* [`unist-util-source`](https://github.com/syntax-tree/unist-util-source) | ||
— get the source of a node | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -117,2 +209,10 @@ | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -122,9 +222,9 @@ | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[health]: https://github.com/syntax-tree/.github | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
@@ -135,4 +235,14 @@ [unist]: https://github.com/syntax-tree/unist | ||
[position]: https://github.com/syntax-tree/unist#position | ||
[unist-position]: https://github.com/syntax-tree/unist#position | ||
[point]: https://github.com/syntax-tree/unist#point | ||
[unist-point]: https://github.com/syntax-tree/unist#point | ||
[unist-util-generated]: https://github.com/syntax-tree/unist-util-generated | ||
[unist-util-stringify-position]: https://github.com/syntax-tree/unist-util-stringify-position | ||
[position]: #positionnode | ||
[pointend]: #pointendnode | ||
[pointstart]: #pointstartnode |
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
12435
8
7
118
241
1