Skip to content

Commit 5bf2368

Browse files
committed
feat: nilable file
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 268b081 commit 5bf2368

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [Install](#install)
2020
- [Use](#use)
2121
- [API](#api)
22-
- [`Location(file[, start])`](#locationfile-start)
22+
- [`Location([file][, start])`](#locationfile-start)
2323
- [`Location#offset([point])`](#locationoffsetpoint)
2424
- [`Location#point([offset])`](#locationpointoffset)
2525
- [`Point`](#point)
@@ -94,14 +94,14 @@ console.log(loc.point(pt.offset)) // => pt
9494

9595
This package exports the identifier [`Location`](#locationfile-start). There is no default export.
9696

97-
### `Location(file[, start])`
97+
### `Location([file][, start])`
9898

9999
Create a new location index to translate between point and offset based locations in `file`.
100100

101101
Pass a `start` point to make relative conversions. Any point or offset accessed will be relative to the given point.
102102

103-
- `file` ([`Value`][vfile-value] | [`VFile`][vfile-api]) &mdash; file to index
104-
- `start` ([`Point`](#point) | `null` | `undefined`) &mdash; point before first character in `file`
103+
- `file` ([`Value`][vfile-value] | [`VFile`][vfile-api] | `null` | `undefined`) &mdash; file to index
104+
- `start` ([`Point`](#point) | `null` | `undefined`) &mdash; point before first character
105105

106106
#### `Location#offset([point])`
107107

@@ -111,11 +111,11 @@ Get an offset for `point`.
111111
112112
##### Parameters
113113

114-
- `point` ([`unist.Point`][point] | `null` | `undefined`) &mdash; place in source file
114+
- `point` ([`unist.Point`][point] | `null` | `undefined`) &mdash; place in file
115115

116116
##### Returns
117117

118-
([`Offset`][offset]) Index of character in source file or `-1`.
118+
([`Offset`][offset]) Index of character in file or `-1`.
119119

120120
#### `Location#point([offset])`
121121

@@ -126,11 +126,11 @@ Get a point for `offset`.
126126
127127
##### Parameters
128128

129-
- `offset` ([`Offset`][offset] | `null` | `undefined`) &mdash; index of character in source file
129+
- `offset` ([`Offset`][offset] | `null` | `undefined`) &mdash; index of character in file
130130

131131
##### Returns
132132

133-
([`Point`](#point)) Place in source file.
133+
([`Point`](#point)) Place in file.
134134

135135
### `Point`
136136

src/location.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Location {
3131
readonly #indices: Record<Offset, Readonly<Point>> & Record<string, Offset>
3232

3333
/**
34-
* Point before first character in source file.
34+
* Point before first character in file.
3535
*
3636
* @see {@linkcode Point}
3737
*
@@ -53,34 +53,39 @@ class Location {
5353
* @see {@linkcode VFile}
5454
* @see {@linkcode Value}
5555
*
56-
* @param {Value | VFile} file - File to index
57-
* @param {(Point | null)?} [start] - Point before first character in `file`
56+
* @param {Value | VFile | null | undefined} [file] - File to index
57+
* @param {Point | null | undefined} [start] - Point before first character
5858
*/
59-
constructor(file: Value | VFile, start?: Point | null) {
59+
constructor(
60+
file?: Value | VFile | null | undefined,
61+
start?: Point | null | undefined
62+
) {
6063
this.#indices = {}
6164
this.start = Object.assign({}, start ?? { column: 1, line: 1, offset: 0 })
6265
this.start = Object.freeze(this.start)
6366

64-
/**
65-
* Iteration point.
66-
*
67-
* @const {Point} point
68-
*/
69-
const point: Point = { ...this.start }
70-
7167
// index file
72-
for (const char of String(file) + '\n') {
73-
this.#indices[point.offset] = { ...point }
74-
this.#indices[`${point.line}:${point.column}`] = point.offset
68+
if (file !== null && file !== undefined) {
69+
/**
70+
* Iteration point.
71+
*
72+
* @const {Point} point
73+
*/
74+
const point: Point = { ...this.start }
75+
76+
for (const char of String(file) + '\n') {
77+
this.#indices[point.offset] = { ...point }
78+
this.#indices[`${point.line}:${point.column}`] = point.offset
7579

76-
// advance point
77-
if (/[\n\r]/.test(char)) {
78-
point.column = 1
79-
point.line++
80-
point.offset++
81-
} else {
82-
point.column++
83-
point.offset++
80+
// advance point
81+
if (/[\n\r]/.test(char)) {
82+
point.column = 1
83+
point.line++
84+
point.offset++
85+
} else {
86+
point.column++
87+
point.offset++
88+
}
8489
}
8590
}
8691
}
@@ -97,10 +102,10 @@ class Location {
97102
* @public
98103
* @instance
99104
*
100-
* @param {(unist.Point | null)?} [point] - Place in source file
101-
* @return {Offset} Index of character in source file or `-1`
105+
* @param {unist.Point | null | undefined} [point] - Place in file
106+
* @return {Offset} Index of character in file or `-1`
102107
*/
103-
public offset(point?: unist.Point | null): Offset {
108+
public offset(point?: unist.Point | null | undefined): Offset {
104109
return this.#indices[`${point?.line}:${point?.column}`] ?? -1
105110
}
106111

@@ -116,10 +121,10 @@ class Location {
116121
* @public
117122
* @instance
118123
*
119-
* @param {(Offset | null)?} [offset] - Index of character in source file
120-
* @return {Point} Place in source file
124+
* @param {Offset | null | undefined} [offset] - Index of character in file
125+
* @return {Point} Place in file
121126
*/
122-
public point(offset?: Offset | null): Point {
127+
public point(offset?: Offset | null | undefined): Point {
123128
return this.#indices[offset ?? Number.NaN] ?? {
124129
column: -1,
125130
line: -1,

0 commit comments

Comments
 (0)