22
33[ ![ github release] ( https://img.shields.io/github/v/release/flex-development/unist-util-stringify-position.svg?include_prereleases&sort=semver )] ( https://github.com/flex-development/unist-util-stringify-position/releases/latest )
44[ ![ npm] ( https://img.shields.io/npm/v/@flex-development/unist-util-stringify-position.svg )] ( https://npmjs.com/package/@flex-development/unist-util-stringify-position )
5- [ ![ codecov] ( https://codecov.io/gh/flex-development/unist-util-stringify-position/graph/badge.svg?token=SXBHbtdEko )] ( https://codecov.io/gh/flex-development/unist-util-stringify-position )
5+ [ ![ codecov] ( https://codecov.io/gh/flex-development/unist-util-stringify-position/graph/badge.svg?token=oB6Ip38ZJt )] ( https://codecov.io/gh/flex-development/unist-util-stringify-position )
66[ ![ module type: esm] ( https://img.shields.io/badge/module%20type-esm-brightgreen )] ( https://github.com/voxpelli/badges-cjs-esm )
77[ ![ license] ( https://img.shields.io/github/license/flex-development/unist-util-stringify-position.svg )] ( LICENSE.md )
88[ ![ conventional commits] ( https://img.shields.io/badge/-conventional%20commits-fe5196?logo=conventional-commits&logoColor=ffffff )] ( https://conventionalcommits.org/ )
99[ ![ typescript] ( https://img.shields.io/badge/-typescript-3178c6?logo=typescript&logoColor=ffffff )] ( https://typescriptlang.org/ )
1010[ ![ vitest] ( https://img.shields.io/badge/-vitest-6e9f18?style=flat&logo=vitest&logoColor=ffffff )] ( https://vitest.dev/ )
1111[ ![ yarn] ( https://img.shields.io/badge/-yarn-2c8ebb?style=flat&logo=yarn&logoColor=ffffff )] ( https://yarnpkg.com/ )
1212
13- [ unist] [ unist ] utility to pretty print positional info
13+ [ unist] [ unist ] utility to serialize the positional info of a node, point, position, or range
1414
1515## Contents
1616
1919- [ Install] ( #install )
2020- [ Use] ( #use )
2121- [ API] ( #api )
22+ - [ ` stringifyPosition([info][, options]) ` ] ( #stringifypositioninfo-options )
2223- [ Types] ( #types )
24+ - [ ` Info ` ] ( #info )
25+ - [ ` LiteralLike ` ] ( #literallike )
26+ - [ ` NodeLike ` ] ( #nodelike )
27+ - [ ` Options ` ] ( #options )
28+ - [ ` ParentLike ` ] ( #parentlike )
29+ - [ ` PointLike ` ] ( #pointlike )
30+ - [ ` PositionLike ` ] ( #positionlike )
31+ - [ ` Range ` ] ( #range )
2332- [ Related] ( #related )
2433- [ Contribute] ( #contribute )
2534
2635## What is this?
2736
28- ** TODO** : what is this?
37+ This is a tiny, but useful, package that takes any [ unist] [ unist ] node, point, position, or range and serializes its
38+ positional info.
2939
3040## When should I use this?
3141
32- ** TODO** : when should I use this?
42+ Use this package when you want a standard format for serialized positional info, such as when inspecting trees, or
43+ throwing errors.
3344
3445## Install
3546
@@ -64,24 +75,151 @@ In browsers with [`esm.sh`][esmsh]:
6475
6576## Use
6677
67- ** TODO** : use
78+ ``` ts
79+ import { u } from ' @flex-development/unist-util-builder'
80+ import {
81+ stringifyPosition ,
82+ type LiteralLike ,
83+ type PointLike ,
84+ type PositionLike ,
85+ type Range
86+ } from ' @flex-development/unist-util-stringify-position'
87+
88+ const node: LiteralLike = u (' text' , {
89+ position: {
90+ end: { column: 13 , line: 1 , offset: 12 },
91+ start: { column: 1 , line: 1 , offset: 0 }
92+ },
93+ value: ' hello world!'
94+ })
95+
96+ const point: PointLike = { column: 9 , line: 6 }
97+
98+ const position: PositionLike = { end: { line: 8 }, start: { line: 7 } }
99+
100+ const range: Range = [{ column: 2 , line: 3 }, { column: 2 , line: 5 }]
101+
102+ console .log (' node:' , stringifyPosition (node , { offsets: true }))
103+ console .log (' point:' , stringifyPosition (point ))
104+ console .log (' position:' , stringifyPosition (position ))
105+ console .log (' range:' , stringifyPosition (range ))
106+ ```
107+
108+ ...yields
109+
110+ ``` sh
111+ node: 1:1-1:13, 0-12
112+ point: 6:9
113+ position: 7:1-8:1
114+ range: 3:2-5:2
115+ ```
68116
69117## API
70118
71- ** TODO** : api
119+ This package exports the identifier [ ` stringifyPosition ` ] ( #stringifypositioninfo-options ) .
120+
121+ There is no default export.
122+
123+ ### ` stringifyPosition([info][, options]) `
124+
125+ Serialize the positional info of a node, point, position, or range.
126+
127+ The serialized info is returned in one the following formats:
128+
129+ - ` ls:cs-le:ce, os-oe ` (node, position, range)
130+ - ` ls:cs-le:ce ` (node, position, range)
131+ - ` l:c ` (point)
132+
133+ where ` l ` stands for line, ` c ` for column, ` o ` for offset, ` s ` for ` start ` , and ` e ` for end.
134+
135+ An empty string (` '' ` ) is returned if the given info is neither node, point, position, nor range.
136+
137+ #### Parameters
138+
139+ - ` info ` ([ ` Info ` ] ( #info ) | ` null ` | ` undefined ` ) &mdash ; node, point, position, or range
140+ - ` options ` ([ ` Options ` ] ( #options ) | ` null ` | ` undefined ` ) &mdash ; configuration options
141+ - ` options.offsets ` (` boolean | null | undefined ` ) &mdash ; serialize offsets if ` info ` is a node, position, or range
142+
143+ #### Returns
144+
145+ (` string ` ) Pretty printed positional info.
72146
73147## Types
74148
75149This package is fully typed with [ TypeScript] [ typescript ] .
76150
151+ ### ` Info `
152+
153+ Union of positional info objects (TypeScript type).
154+
155+ ``` ts
156+ type Info =
157+ | Literal
158+ | LiteralLike
159+ | Node
160+ | NodeLike
161+ | Parent
162+ | ParentLike
163+ | Point
164+ | PointLike
165+ | Position
166+ | PositionLike
167+ | Range
168+ ` ` `
169+
170+ ### ` LiteralLike `
171+
172+ Loose [literal][literal] (TypeScript type).
173+
174+ ### ` NodeLike `
175+
176+ Loose [node][node] (TypeScript type).
177+
178+ ### ` Options `
179+
180+ Configuration options (TypeScript type).
181+
182+ ` ` ` ts
183+ type Options = {
184+ offsets? : boolean | null | undefined
185+ }
186+ ` ` `
187+
188+ #### Fields
189+
190+ - ` offsets ` ( ` boolean | null | undefined ` ) — serialize offsets if positional info is a node, position, or range
191+
192+ ### ` ParentLike `
193+
194+ Loose [parent][parent] (TypeScript type).
195+
196+ ### ` PointLike `
197+
198+ Loose [point][point] (TypeScript type).
199+
200+ ### ` PositionLike `
201+
202+ Loose [position][position] (TypeScript type).
203+
204+ ### ` Range `
205+
206+ List, where the first value is the place of the first character in a source region, and the last is the place of the
207+ last character in the region. (TypeScript type).
208+
209+ ` ` ` ts
210+ type Range = [
211+ start ? : Point | PointLike | null | undefined ,
212+ end ? : Point | PointLike | null | undefined
213+ ]
214+ ` ` `
215+
77216## Related
78217
79- - [ ` unist-util-generated ` ] [ unist-util-generated ] &mdash ; [ unist] [ unist ] utility to check if a node is generated
80- - [ ` unist-util-position ` ] [ unist-util-position ] &mdash ; [ unist] [ unist ] utility to get positional info of nodes
81- - [ ` unist-util-remove-position ` ] [ unist-util-remove-position ] &mdash ; [ unist] [ unist ] utility to remove positional info
82- - [ ` unist-util-source ` ] [ unist-util-source ] &mdash ; [ unist] [ unist ] utility to get the source of a value (node or
83- position) in a file
84- - [ ` unist-util-types ` ] [ unist-util-types ] &mdash ; [ unist] [ unist ] utility types
218+ - [ ` unist -util -generated ` ][unist-util-generated] — check if a node is generated
219+ - [ ` unist -util -position ` ][unist-util-position] — get positional info of nodes
220+ - [ ` unist -util -remove -position ` ][unist-util-remove-position] — remove positional info from trees
221+ - [ ` unist -util -source ` ][unist-util-source] — get the source of a value (node or position) in a file
222+ - [ ` unist -util -types ` ][unist-util-types] — utility types
85223
86224## Contribute
87225
@@ -90,13 +228,18 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md).
90228This project has a [code of conduct](CODE_OF_CONDUCT.md). By interacting with this repository, organization, or
91229community you agree to abide by its terms.
92230
93- [ yarn ] : https://yarnpkg.com
94- [ unist ] : https://github.com/syntax-tree/unist
95- [ unist-util-types ] : https://github.com/flex-development/unist-util-types
96- [ unist-util-source ] : https://github.com/syntax-tree/unist-util-source
97- [ unist-util-remove-position ] : https://github.com/syntax-tree/unist-util-remove-position
98- [ unist-util-position ] : https://github.com/syntax-tree/unist-util-position
99- [ unist-util-generated ] : https://github.com/syntax-tree/unist-util-generated
100- [ typescript ] : https://www.typescriptlang.org
101- [ esmsh ] : https://esm.sh/
102231[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
232+ [esmsh]: https://esm.sh/
233+ [literal]: https://github.com/syntax-tree/unist#literal
234+ [node]: https://github.com/syntax-tree/unist#node
235+ [parent]: https://github.com/syntax-tree/unist#parent
236+ [point]: https://github.com/syntax-tree/unist#point
237+ [position]: https://github.com/syntax-tree/unist#position
238+ [typescript]: https://www.typescriptlang.org
239+ [unist-util-generated]: https://github.com/syntax-tree/unist-util-generated
240+ [unist-util-position]: https://github.com/syntax-tree/unist-util-position
241+ [unist-util-remove-position]: https://github.com/syntax-tree/unist-util-remove-position
242+ [unist-util-source]: https://github.com/syntax-tree/unist-util-source
243+ [unist-util-types]: https://github.com/flex-development/unist-util-types
244+ [unist]: https://github.com/syntax-tree/unist
245+ [yarn]: https://yarnpkg.com
0 commit comments