-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
81 lines (73 loc) · 2.01 KB
/
Copy pathutil.ts
File metadata and controls
81 lines (73 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @file util
* @module unist-util-stringify-position/util
*/
import type { PointLike, PositionLike, Range } from './types'
import type State from './types/state'
/**
* Standardize a column, line, or offset.
*
* > 👉 If `offset` is `true`, `value` is expected to already be a number.
*
* @internal
*
* @param {number | null | undefined} value - Column, line, or offset
* @param {boolean | null | undefined} [offset] - `value` is offset?
* @return {number} Column, line, or offset
*/
function index(
value: number | null | undefined,
offset: boolean | null | undefined
): number {
return offset ? value! : value && typeof value === 'number' ? value : 1
}
/**
* Serialize a point.
*
* @internal
*
* @param {PointLike | null | undefined} point - Point to serialize
* @return {string} Pretty printed `point`
*/
function point(point: PointLike | null | undefined): string {
return index(point?.line, false) + ':' + index(point?.column, false)
}
/**
* Serialize a position.
*
* @internal
*
* @param {PositionLike | null | undefined} pos - Position to serialize
* @param {State} state - Info passed around
* @return {string} Pretty printed `position`
*/
function position(pos: PositionLike | null | undefined, state: State): string {
/**
* Serialized position.
*
* @var {string} serialized
*/
let serialized: string = point(pos?.start) + '-' + point(pos?.end)
if (
state.offsets &&
typeof pos?.end?.offset === 'number' &&
typeof pos.start?.offset === 'number'
) {
serialized += ', ' + index(pos.start.offset, state.offsets) + '-'
serialized += index(pos.end.offset, state.offsets)
}
return serialized
}
/**
* Serialize a range.
*
* @internal
*
* @param {Range | null | undefined} range - Range to serialize
* @param {State} state - Info passed around
* @return {string} Pretty printed `range`
*/
function range(range: Range | null | undefined, state: State): string {
return position({ end: range?.[1], start: range?.[0] }, state)
}
export { point, position, range }