code-excerpt

Extract code excerpts

MIT 9 个版本
安装
npm install code-excerpt
yarn add code-excerpt
pnpm add code-excerpt
bun add code-excerpt
README

code-excerpt test

Extract code excerpts

Install

$ npm install --save code-excerpt

Usage

import codeExcerpt from '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])

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.

版本列表
4.0.0 2022-02-06
3.0.0 2020-06-16
2.1.1 2018-01-20
2.1.0 2017-02-01
2.0.0 2017-01-05
1.1.0 2017-01-05
1.0.2 2016-11-19
1.0.1 2016-11-13
1.0.0 2016-11-13