riot/compiler

View on GitHub
src/utils/get-line-and-column-by-position.js

Summary

Maintainability
A
0 mins
Test Coverage
import splitStringByEOL from './split-string-by-EOL.js'

/**
 * Get the line and the column of a source text based on its position in the string
 * @param   { string } string - target string
 * @param   { number } position - target position
 * @returns { Object } object containing the source text line and column
 */
export default function getLineAndColumnByPosition(string, position) {
  const lines = splitStringByEOL(string.slice(0, position))

  return {
    line: lines.length,
    column: lines[lines.length - 1].length,
  }
}