Socket
Socket
Sign inDemoInstall

get-line-from-pos

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    get-line-from-pos

Get the line number of a string using index or position


Version published
Weekly downloads
624
decreased by-12.24%
Maintainers
1
Install size
4.07 kB
Created
Weekly downloads
 

Readme

Source

get-line-from-pos

Get the line number of a string using index or position

NPM Version NPM Downloads Build Status

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
$ npm install --save get-line-from-pos

Usage

CommonJS

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

define(['getLineFromPos'], function(getLineFromPos){
    var pos = getLineFromPos(multipleLineStr, index);
    console.log(pos);
    //-> line number that index is on
});

Browser

Load the script:

<script src="../get-line-from-pos/index.js"></script>

Then when you need to use it:

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

Copyright © 2014 Gilad Peleg. Licensed under the MIT license.

Keywords

FAQs

Last updated on 27 Oct 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc