* Return an anchor of a cursor in a block as a {line,offset} object * * @param {String} text * @param {Number} offset * @param {String} sep (optional) * @return {LineAnchor}
(text, offset, sep)
| 26 | * @return {LineAnchor} |
| 27 | */ |
| 28 | function getLineAnchorForOffset(text, offset, sep) { |
| 29 | sep = sep || getNewLine(text); |
| 30 | |
| 31 | var lineIndex = 0; |
| 32 | var nextLineIndex = 0; |
| 33 | var lastLineIndex = 0; |
| 34 | |
| 35 | while (nextLineIndex >= 0 && nextLineIndex < offset) { |
| 36 | lineIndex++; |
| 37 | |
| 38 | lastLineIndex = nextLineIndex; |
| 39 | nextLineIndex = text.indexOf(sep, nextLineIndex + sep.length); |
| 40 | } |
| 41 | |
| 42 | return new LineAnchor({ |
| 43 | line: lineIndex - 1, |
| 44 | offset: offset - lastLineIndex |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | module.exports = getLineAnchorForOffset; |
no test coverage detected