slice-location

String slices by line and column indexes

Usage no npm install needed!

<script type="module">
  import sliceLocation from 'https://cdn.skypack.dev/slice-location';
</script>

README

slice-location

String slices by line and column indexes

Coverage Status version node-version downloads

MIT License PRs Welcome Code of Conduct XO code style

Watch on GitHub Star on GitHub

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev slice-location

Usage

Node.js

import sliceLocation from 'slice-location';

const inputString = `Line 1
Line 2

Line 4
Line 5

Line 7
`;

const options = {beginLine: 4, beginColumn: 1, endLine: 5, endColumn: 2, offsetLine: 1, offsetColumn: 1};
console.log(sliceLocation(inputString));
//=> Line 4
//=> Li

API

sliceLocation(input, options)

Slice a string by line and column indexes. Returns a string sliced.

input

Type: string

The string to slice.

options

Type: object

  • beginLine: The line number where the slice begins.
    • Type: number
    • Default: 0
  • beginColumn: The column number where the slice begins.
    • Type: number
    • Default: 0
  • endLine: The line number where the slice ends.
    • Type: number
    • Default: The number of the last line of the string
  • endColumn: The column number where the slice ends.
    • Type: number
    • Default: The number of the last column of the last line of the string
  • offsetLine: by default, the value of the first line is 0. If you need the first line begin at 1 or more, set offsetLine in the option parameter object to the desired value.
    • Type: number
    • Default: 0
  • offsetColumn: by default, the value of the first column is 0. If you need the first line begin at 1 or more, set offsetColumn in the option parameter object to the desired value.
    • Type: number
    • Default: 0

return

Type: string

The string sliced.

Example

index.js:


import sliceLocation from 'slice-location';

const inputString = `Line 1
Line 2

Line 4
Line 5

Line 7
`;

const options = {beginLine: 4, beginColumn: 1, endLine: 5, endColumn: 2, offsetLine: 1, offsetColumn: 1};
console.log(sliceLocation(inputString));
//=> Line 4
//=> Li

LICENSE

MIT