JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3215656
  • Score
    100M100P100Q198319F
  • License MIT

Extract code excerpts

Package Exports

  • code-excerpt

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (code-excerpt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

code-excerpt Build Status

Extract code excerpts

Install

$ npm install --save code-excerpt

Usage

const codeExcerpt = require('code-excerpt');

const source = `
'use strict';

function someFunc() {}

module.exports = () => {
    const a = 1;
    const b = 2;
    const c = 3;

    someFunc();
};
`.trim();

const excerpt = codeExcerpt(source, 5);
//=> [
//	{line: 2, value: '                        '},
//	{line: 3, value: 'function someFunc() {}  '},
//	{line: 4, value: '                        '},
//	{line: 5, value: 'module.exports = () => {'},
//	{line: 6, value: '  const a = 1;          '},
//	{line: 7, value: '  const b = 2;          '},
//	{line: 8, value: '  const c = 3;          '}
// ]

API

codeExcerpt(source, line, [options])

All lines in the output have equal width for convenience (customizable via options.equalLength). For example, if a line needs to be highlighted, the highlight will be full-width, just like in editors.

source

Type: string

Source code.

line

Type: number

Line number to extract excerpt for.

options

around

Type: number
Default: 3

Number of surrounding lines to extract.

equalLength

Type: boolean
Default: true

Extend lines to the same length. Useful for highlighting lines.

License

MIT © vdemedes