string-pos

super fast string index-to-line/column and line/column-to-index translation

GPL-3.0 License

Downloads
225
Stars
2
Committers
1

string-pos

Usage

var stringPos = require('coffee-register');
var sample = "abc\n\n123\n \ndef";

stringPos(sample, 0) //-> {line:1, column:0}
stringPos(sample, 2) //-> {line:1, column:2}
stringPos(sample, 4) //-> {line:2, column:0}
stringPos(sample, 7) //-> {line:3, column:2}

stringPos.toIndex(sample, {line:1, column:0}) //-> 0
stringPos.toIndex(sample, {line:1, column:2}) //-> 2
stringPos.toIndex(sample, {line:2, column:0}) //-> 4
stringPos.toIndex(sample, {line:3, column:2}) //-> 7

Benchmarks

Notes

  • Compatible with source-map positions.
  • Line numbers are 1 index based.
  • Column numbers are zero index based.

License

MIT Daniel Kalen

Package Rankings
Top 8.3% on Npmjs.org
Badges
Extracted from project README
Build Status Coverage Code Climate NPM NPM Benchmarks