diff --git a/.github/workflows/bb.yml b/.github/workflows/bb.yml index 291ab09..0198fc3 100644 --- a/.github/workflows/bb.yml +++ b/.github/workflows/bb.yml @@ -2,7 +2,7 @@ name: bb on: issues: types: [opened, reopened, edited, closed, labeled, unlabeled] - pull_request: + pull_request_target: types: [opened, reopened, edited, closed, labeled, unlabeled] jobs: main: diff --git a/index.js b/index.js index 2d408a0..1b4a320 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,14 @@ -var own = {}.hasOwnProperty - /** - * @typedef {import('unist').Node} Node - * @typedef {import('unist').Position} Position * @typedef {import('unist').Point} Point + * @typedef {import('unist').Position} Position + * @typedef {Record & {type: string, position?: Position|undefined}} NodeLike */ /** * Stringify one point, a position (start and end points), or a node’s * positional information. * - * @param {Node|Position|Point} [value] + * @param {NodeLike|Position|Point|null} [value] * @returns {string} */ export function stringifyPosition(value) { @@ -20,20 +18,17 @@ export function stringifyPosition(value) { } // Node. - if (own.call(value, 'position') || own.call(value, 'type')) { - // @ts-ignore looks like a node. + if ('position' in value || 'type' in value) { return position(value.position) } // Position. - if (own.call(value, 'start') || own.call(value, 'end')) { - // @ts-ignore looks like a position. + if ('start' in value || 'end' in value) { return position(value) } // Point. - if (own.call(value, 'line') || own.call(value, 'column')) { - // @ts-ignore looks like a point. + if ('line' in value || 'column' in value) { return point(value) } @@ -42,7 +37,7 @@ export function stringifyPosition(value) { } /** - * @param {Point} point + * @param {Point|undefined} point * @returns {string} */ function point(point) { @@ -50,7 +45,7 @@ function point(point) { } /** - * @param {Position} pos + * @param {Position|undefined} pos * @returns {string} */ function position(pos) { @@ -58,7 +53,7 @@ function position(pos) { } /** - * @param {number} value + * @param {number|undefined} value * @returns {number} */ function index(value) { diff --git a/package.json b/package.json index 3ecb5b5..38ea1f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unist-util-stringify-position", - "version": "3.0.0", + "version": "3.0.1", "description": "unist utility to serialize a node, position, or point as a human readable location", "license": "MIT", "keywords": [ @@ -40,13 +40,13 @@ "@types/tape": "^4.0.0", "c8": "^7.0.0", "prettier": "^2.0.0", - "remark-cli": "^9.0.0", - "remark-preset-wooorm": "^8.0.0", + "remark-cli": "^10.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.38.0" + "xo": "^0.48.0" }, "scripts": { "prepack": "npm run build && npm run format", @@ -65,11 +65,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "rules": { - "no-var": "off", - "prefer-arrow-callback": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ diff --git a/readme.md b/readme.md index 7f6b656..5fe2e1d 100644 --- a/readme.md +++ b/readme.md @@ -8,31 +8,64 @@ [![Backers][backers-badge]][collective] [![Chat][chat-badge]][chat] -[**unist**][unist] utility to pretty print the positional information of a node. +**[unist][]** utility to pretty print the positional information of a node. -## Install +## Contents + +* [What is this?](#what-is-this) +* [When should I use this?](#when-should-i-use-this) +* [Install](#install) +* [Use](#use) +* [API](#api) + * [`stringifyPosition(node|position|point)`](#stringifypositionnodepositionpoint) +* [Types](#types) +* [Compatibility](#compatibility) +* [Security](#security) +* [Related](#related) +* [Contribute](#contribute) +* [License](#license) + +## What is this? + +This package is a utility that takes any [unist][] (whether mdast, hast, etc) +node, position, or point, and serializes its positional info. -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. +## When should I use this? + +This utility is useful to display where something occurred in the original +document, in one standard way, for humans. +For example, when throwing errors or warning messages about something. + +## Install -[npm][]: +This package is [ESM only][esm]. +In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: ```sh npm install unist-util-stringify-position ``` -## Use +In Deno with [`esm.sh`][esmsh]: ```js -import {stringifyPosition} from 'unist-util-stringify-position' +import {stringifyPosition} from 'https://esm.sh/unist-util-stringify-position@3' +``` + +In browsers with [`esm.sh`][esmsh]: + +```html + +``` -// Point -stringifyPosition({line: 2, column: 3}) // => '2:3' +## Use -// Position -stringifyPosition({start: {line: 2}, end: {line: 3}}) // => '2:1-3:1' +```js +import {stringifyPosition} from 'unist-util-stringify-position' -// Node +stringifyPosition({line: 2, column: 3}) // => '2:3' (point) +stringifyPosition({start: {line: 2}, end: {line: 3}}) // => '2:1-3:1' (position) stringifyPosition({ type: 'text', value: '!', @@ -40,27 +73,26 @@ stringifyPosition({ start: {line: 5, column: 11}, end: {line: 5, column: 12} } -}) // => '5:11-5:12' +}) // => '5:11-5:12' (node) ``` ## API -This package exports the following identifiers: `stringifyPosition`. +This package exports the identifier `stringifyPosition`. There is no default export. ### `stringifyPosition(node|position|point)` -Stringify one [point][], a [position][] (start and end [point][]s), or a node’s -[positional information][positional-information]. +Stringify a [point][], [position][], or a [node][]. ###### Parameters * `node` ([`Node`][node]) - — Node whose `'position'` property to stringify + — node whose `'position'` property to stringify * `position` ([`Position`][position]) - — Position whose `'start'` and `'end'` points to stringify + — position whose `'start'` and `'end'` points to stringify * `point` ([`Point`][point]) - — Point whose `'line'` and `'column'` to stringify + — point whose `'line'` and `'column'` to stringify ###### Returns @@ -70,16 +102,32 @@ Stringify one [point][], a [position][] (start and end [point][]s), or a node’ An empty string (`''`) is returned if the given value is neither `node`, `position`, nor `point`. +## Types + +This package is fully typed with [TypeScript][]. +There are no additional types exported. + +## Compatibility + +Projects maintained by the unified collective are compatible with all maintained +versions of Node.js. +As of now, that is Node.js 12.20+, 14.14+, and 16.0+. +Our projects sometimes work with older versions, but this is not guaranteed. + +## Security + +This project is safe. + ## Related * [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) - — Check if a node is generated + — check if a node is generated * [`unist-util-position`](https://github.com/syntax-tree/unist-util-position) - — Get positional info of nodes + — get positional info of nodes * [`unist-util-remove-position`](https://github.com/syntax-tree/unist-util-remove-position) - — Remove positional info from trees + — remove positional info from trees * [`unist-util-source`](https://github.com/syntax-tree/unist-util-source) - — Get the source of a value (node or position) in a file + — get the source of a value (node or position) in a file ## Contribute @@ -129,6 +177,12 @@ abide by its terms. [author]: https://wooorm.com +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c + +[esmsh]: https://esm.sh + +[typescript]: https://www.typescriptlang.org + [contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md [support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md @@ -142,5 +196,3 @@ abide by its terms. [position]: https://github.com/syntax-tree/unist#position [point]: https://github.com/syntax-tree/unist#point - -[positional-information]: https://github.com/syntax-tree/unist#positional-information diff --git a/test.js b/test.js index 531fcf2..5ae7b4d 100644 --- a/test.js +++ b/test.js @@ -13,19 +13,19 @@ test('stringifyPosition', function (t) { 'should return empty `string` with `null`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition('foo'), '', 'should return empty `string` with `string`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition(5), '', 'should return empty `string` with `number`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({}), '', 'should return empty `string` with `{}`' @@ -38,7 +38,7 @@ test('stringifyPosition', function (t) { ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({type: 'text', position: 3}), '1:1-1:1', 'should return a range for `node` with invalid `position` #1' @@ -47,7 +47,7 @@ test('stringifyPosition', function (t) { t.equal( stringifyPosition({ type: 'text', - // @ts-ignore runtime. + // @ts-expect-error runtime. position: {start: {}, end: {}} }), '1:1-1:1', @@ -58,7 +58,9 @@ test('stringifyPosition', function (t) { stringifyPosition({ type: 'text', position: { + // @ts-expect-error runtime. start: {line: null, column: null}, + // @ts-expect-error runtime. end: {line: null, column: null} } }), @@ -79,26 +81,28 @@ test('stringifyPosition', function (t) { ) t.equal( + // @ts-expect-error runtime. stringifyPosition({start: null, end: null}), '1:1-1:1', 'should return a range for a `position` without `point`s' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({start: 3, end: 6}), '1:1-1:1', 'should return a range for `position` with invalid `point`s #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({start: {}, end: {}}), '1:1-1:1', 'should return range for `position` with invalid `point`s #1' ) t.equal( + // @ts-expect-error runtime. stringifyPosition({ start: {line: null, column: null}, end: {line: null, column: null} @@ -117,27 +121,28 @@ test('stringifyPosition', function (t) { ) t.equal( + // @ts-expect-error runtime. stringifyPosition({line: null, column: null}), '1:1', 'should return a point for a `point` without indices' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({line: 'foo', column: 'bar'}), '1:1', 'should return a point for a `point` with invalid indices #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({line: 4}), '4:1', 'should return a point for a partially valid `point` #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({column: 12}), '1:12', 'should return a point for a partially valid `point` #1' diff --git a/tsconfig.json b/tsconfig.json index be08abe..e31adf8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "declaration": true, "emitDeclarationOnly": true, "allowSyntheticDefaultImports": true, - "skipLibCheck": true + "skipLibCheck": true, + "strict": true } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy