This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathpoint-helpers.js
74 lines (63 loc) · 1.82 KB
/
point-helpers.js
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
const assert = require('assert')
exports.ZERO_POINT = Object.freeze({row: 0, column: 0})
exports.compare = function (a, b) {
return primitiveCompare(a.row, a.column, b.row, b.column)
}
function primitiveCompare (rowA, columnA, rowB, columnB) {
if (rowA === rowB) {
return columnA - columnB
} else {
return rowA - rowB
}
}
exports.traverse = function (start, distance) {
if (distance.row === 0)
return {row: start.row, column: start.column + distance.column}
else {
return {row: start.row + distance.row, column: distance.column}
}
}
exports.traversal = function (end, start) {
if (end.row === start.row) {
return {row: 0, column: end.column - start.column}
} else {
return {row: end.row - start.row, column: end.column}
}
}
exports.extentForText = function (text) {
let row = 0
let column = 0
let index = 0
while (index < text.length) {
const char = text[index]
if (char === '\n') {
column = 0
row++
} else {
column++
}
index++
}
return {row, column}
}
exports.characterIndexForPosition = function (text, target) {
// Previously we instantiated a point object here and mutated its fields, so
// that we could use the `compare` function we already export. However, this
// seems to trigger a weird optimization bug on v8 5.6.326.50 which causes
// this function to return unpredictable results, so we use primitive-valued
// variables instead.
let row = 0
let column = 0
let index = 0
while (primitiveCompare(row, column, target.row, target.column) < 0 && index <= text.length) {
if (text[index] === '\n') {
row++
column = 0
} else {
column++
}
index++
}
assert(primitiveCompare(row, column, target.row, target.column) <= 0, 'Target position should not exceed the extent of the given text')
return index
}