Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pgilad/get-line-from-pos
Get the line number of a string using index or position
https://github.com/pgilad/get-line-from-pos
javascript line lookup position utility
Last synced: 14 days ago
JSON representation
Get the line number of a string using index or position
- Host: GitHub
- URL: https://github.com/pgilad/get-line-from-pos
- Owner: pgilad
- License: mit
- Created: 2014-09-19T20:24:24.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-31T08:25:07.000Z (over 9 years ago)
- Last Synced: 2024-10-04T19:43:25.083Z (about 1 month ago)
- Topics: javascript, line, lookup, position, utility
- Language: JavaScript
- Size: 168 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# get-line-from-pos
> Get the line number of a string using index or position[![NPM Version](http://img.shields.io/npm/v/get-line-from-pos.svg?style=flat)](https://npmjs.org/package/get-line-from-pos)
[![NPM Downloads](http://img.shields.io/npm/dm/get-line-from-pos.svg?style=flat)](https://npmjs.org/package/get-line-from-pos)
[![Build Status](http://img.shields.io/travis/pgilad/get-line-from-pos.svg?style=flat)](https://travis-ci.org/pgilad/get-line-from-pos)Lookup a line number in a string using an index or position.
Useful if you want to get a line number of a position or index from a regex match, but can
also be used in other situations.- Allows using negative positions to lookup line number.
- Supported by CommonJS, AMD and browser.
- Supports Regular line ending style (`\n`) and MacOS line ending style (`\r`).#### Installation
```bash
$ npm install --save get-line-from-pos
```## Usage
### CommonJS
```js
var getLineFromPos = require('get-line-from-pos');var str = 'string with \n multiple \n lines';
console.log(getLineFromPos(str, /multiple/g.exec(str)));
// -> 2
```### AMD
```js
define(['getLineFromPos'], function(getLineFromPos){
var pos = getLineFromPos(multipleLineStr, index);
console.log(pos);
//-> line number that index is on
});
```### Browser
Load the script:
```html```
Then when you need to use it:
```js
console.log(getLineFromPos(someStr, -1));
//-> Will output the number of lines in someStr
```## API
`getLineFromPos(inputStr, index)`
Both parameters are required.
### inputStr
Type: `String`
**Required**
String to do the lookup for line number.
### index
Type: `Number`
**Required**
The index or position to lookup the line number.
## License
MIT ©[Gilad Peleg](http://giladpeleg.com)