Skip to content

Commit 64613b6

Browse files
committed
feat: stringifyPosition
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 06b2a5a commit 64613b6

38 files changed

+1183
-51
lines changed

.codecov.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ ignore:
8989
- '!src/index.ts'
9090

9191
profiling:
92-
critical_files_paths: []
92+
critical_files_paths:
93+
- src/stringify-position.ts

.github/infrastructure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ repository:
168168
automated_security_fixes: true
169169
default_branch: main
170170
delete_branch_on_merge: true
171-
description: unist utility to serialize a node, position, point, or range
171+
description: unist utility to serialize the positional info of a node, point, position, or range
172172
has_issues: true
173173
has_projects: true
174174
has_wiki: false

README.md

Lines changed: 164 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
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

@@ -19,17 +19,28 @@
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

75149
This 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`) &mdash; 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] &mdash; check if a node is generated
219+
- [`unist-util-position`][unist-util-position] &mdash; get positional info of nodes
220+
- [`unist-util-remove-position`][unist-util-remove-position] &mdash; remove positional info from trees
221+
- [`unist-util-source`][unist-util-source] &mdash; get the source of a value (node or position) in a file
222+
- [`unist-util-types`][unist-util-types] &mdash; utility types
85223
86224
## Contribute
87225
@@ -90,13 +228,18 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md).
90228
This project has a [code of conduct](CODE_OF_CONDUCT.md). By interacting with this repository, organization, or
91229
community 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

__tests__/interfaces/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

__tests__/interfaces/mock-instance.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

__tests__/setup/faker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @file Test Setup - faker
3+
* @module tests/setup/faker
4+
* @see https://github.com/faker-js/faker
5+
*/
6+
7+
import { faker } from '@faker-js/faker'
8+
9+
global.faker = faker

__tests__/setup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* @module tests/setup
44
*/
55

6-
export {}
6+
import './faker'

eslint.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ export default [
2121
'**/dist/',
2222
'__fixtures__/underscore-1.5.2.js'
2323
]
24+
},
25+
{
26+
files: ['src/stringify-position.ts'],
27+
rules: {
28+
'@typescript-eslint/unified-signatures': 0
29+
}
2430
}
2531
]

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@commitlint/cli": "19.3.0",
8282
"@commitlint/types": "19.0.3",
8383
"@eslint/js": "9.6.0",
84+
"@faker-js/faker": "9.0.0-alpha.1",
8485
"@flex-development/commitlint-config": "1.0.1",
8586
"@flex-development/decorator-regex": "2.0.0",
8687
"@flex-development/esm-types": "2.0.0",
@@ -89,6 +90,7 @@
8990
"@flex-development/mlly": "1.0.0-alpha.18",
9091
"@flex-development/pathe": "2.0.0",
9192
"@flex-development/tutils": "6.0.0-alpha.25",
93+
"@flex-development/unist-util-builder": "1.0.0",
9294
"@stylistic/eslint-plugin": "2.3.0",
9395
"@types/chai": "4.3.16",
9496
"@types/eslint": "8.56.10",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`functional:stringifyPosition > node > should serialize node (invalid position #1) 1`] = `"1:1-1:1"`;
4+
5+
exports[`functional:stringifyPosition > node > should serialize node (invalid position #2) 1`] = `"1:1-1:1"`;
6+
7+
exports[`functional:stringifyPosition > node > should serialize node (invalid position #3) 1`] = `"1:1-1:1"`;
8+
9+
exports[`functional:stringifyPosition > node > should serialize node (no position) 1`] = `"1:1-1:1"`;
10+
11+
exports[`functional:stringifyPosition > node > should serialize node (valid) 1`] = `"63:8-63:55"`;
12+
13+
exports[`functional:stringifyPosition > node > should serialize node (valid, offsets) 1`] = `"114:76-115:1, 2606-2607"`;
14+
15+
exports[`functional:stringifyPosition > point > should serialize point (column only) 1`] = `"1:3"`;
16+
17+
exports[`functional:stringifyPosition > point > should serialize point (line only) 1`] = `"13:1"`;
18+
19+
exports[`functional:stringifyPosition > point > should serialize point (no indices) 1`] = `"1:1"`;
20+
21+
exports[`functional:stringifyPosition > point > should serialize point (valid) 1`] = `"2:3"`;
22+
23+
exports[`functional:stringifyPosition > position > should serialize position (invalid points #1) 1`] = `"1:1-1:1"`;
24+
25+
exports[`functional:stringifyPosition > position > should serialize position (invalid points #2) 1`] = `"1:1-1:1"`;
26+
27+
exports[`functional:stringifyPosition > position > should serialize position (invalid points #3) 1`] = `"1:1-1:1"`;
28+
29+
exports[`functional:stringifyPosition > position > should serialize position (no points) 1`] = `"1:1-1:1"`;
30+
31+
exports[`functional:stringifyPosition > position > should serialize position (valid) 1`] = `"111:6-128:42"`;
32+
33+
exports[`functional:stringifyPosition > position > should serialize position (valid, offsets) 1`] = `"1:1-4:4, 0-73"`;
34+
35+
exports[`functional:stringifyPosition > range > should serialize range (no points) 1`] = `"1:1-1:1"`;
36+
37+
exports[`functional:stringifyPosition > range > should serialize range (valid) 1`] = `"42:6-38:3"`;
38+
39+
exports[`functional:stringifyPosition > range > should serialize range (valid, offsets) 1`] = `"13:4-13:51, 347-521"`;

0 commit comments

Comments
 (0)