|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Alexander Akait @alexander-akait
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | class LocConverter {
|
|---|
| 9 | /**
|
|---|
| 10 | * Creates an instance of LocConverter.
|
|---|
| 11 | * @param {string} input input
|
|---|
| 12 | */
|
|---|
| 13 | constructor(input) {
|
|---|
| 14 | this._input = input;
|
|---|
| 15 | this.line = 1;
|
|---|
| 16 | this.column = 0;
|
|---|
| 17 | this.pos = 0;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Returns location converter.
|
|---|
| 22 | * @param {number} pos position
|
|---|
| 23 | * @returns {LocConverter} location converter
|
|---|
| 24 | */
|
|---|
| 25 | get(pos) {
|
|---|
| 26 | if (this.pos !== pos) {
|
|---|
| 27 | if (this.pos < pos) {
|
|---|
| 28 | const str = this._input.slice(this.pos, pos);
|
|---|
| 29 | let i = str.lastIndexOf("\n");
|
|---|
| 30 | if (i === -1) {
|
|---|
| 31 | this.column += str.length;
|
|---|
| 32 | } else {
|
|---|
| 33 | this.column = str.length - i - 1;
|
|---|
| 34 | this.line++;
|
|---|
| 35 | while (i > 0 && (i = str.lastIndexOf("\n", i - 1)) !== -1) {
|
|---|
| 36 | this.line++;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | } else {
|
|---|
| 40 | let i = this._input.lastIndexOf("\n", this.pos);
|
|---|
| 41 | while (i >= pos) {
|
|---|
| 42 | this.line--;
|
|---|
| 43 | i = i > 0 ? this._input.lastIndexOf("\n", i - 1) : -1;
|
|---|
| 44 | }
|
|---|
| 45 | this.column = i === -1 ? pos : pos - i - 1;
|
|---|
| 46 | }
|
|---|
| 47 | this.pos = pos;
|
|---|
| 48 | }
|
|---|
| 49 | return this;
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | module.exports = LocConverter;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.