line-numbers
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -0,3 +1,11 @@ | ||
### Version 0.2.0 (2015-02-21) ### | ||
- Improved: You may now pass an already split string as an array. | ||
- Changed: `options.transform` is now passed an object with all the elements of | ||
the current line, allowing you to modify any part of it. | ||
(Backwards-incompatible change.) | ||
### Version 0.1.0 (2014-12-20) ### | ||
- Initial release. |
24
index.js
@@ -1,2 +0,2 @@ | ||
// Copyright 2014 Simon Lydell | ||
// Copyright 2014, 2015 Simon Lydell | ||
// X11 (“MIT”) Licensed. (See LICENSE.) | ||
@@ -6,4 +6,2 @@ | ||
function identity(arg) { return arg } | ||
function get(options, key, defaultValue) { | ||
@@ -13,5 +11,5 @@ return (key in options ? options[key] : defaultValue) | ||
function lineNumbers(string, options) { | ||
function lineNumbers(code, options) { | ||
var getOption = get.bind(null, options || {}) | ||
var transform = getOption("transform", identity) | ||
var transform = getOption("transform", Function.prototype) | ||
var padding = getOption("padding", " ") | ||
@@ -21,11 +19,17 @@ var before = getOption("before", " ") | ||
var start = getOption("start", 1) | ||
var lines = string.split("\n") | ||
var isArray = Array.isArray(code) | ||
var lines = (isArray ? code : code.split("\n")) | ||
var end = start + lines.length - 1 | ||
var width = String(end).length | ||
return lines.map(function(line, index) { | ||
var number = start + index | ||
return transform(before + leftPad(number, width, padding) + after) + line | ||
}).join("\n") | ||
var numbered = lines.map(function(line, index) { | ||
var number = start + index | ||
var params = {before: before, number: number, width: width, after: after, | ||
line: line} | ||
transform(params) | ||
return params.before + leftPad(params.number, width, padding) + | ||
params.after + params.line | ||
}) | ||
return (isArray ? numbered : numbered.join("\n")) | ||
} | ||
module.exports = lineNumbers |
{ | ||
"name": "line-numbers", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Simon Lydell", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -35,7 +35,10 @@ Overview [![Build Status](https://travis-ci.org/lydell/line-numbers.svg?branch=master)](https://travis-ci.org/lydell/line-numbers) | ||
### `lineNumbers(string, [options])` ### | ||
### `lineNumbers(code, [options])` ### | ||
Inserts a line number at the beginning of each line in `string`. All the line | ||
numbers are of the same width; shorter numbers are padded on the left side. | ||
Inserts a line number at the beginning of each line in `code`, which is either a | ||
string or an array of strings—one for each line. All the line numbers are of the | ||
same width; shorter numbers are padded on the left side. | ||
The return value is of the same type as `code`. | ||
`options`: | ||
@@ -48,8 +51,16 @@ | ||
Defaults to `" | "`. | ||
- transform: `Function`. A function that receives the entire string that will be | ||
inserted at each line and returns a transformation of it. May be used if | ||
`before` and `after` aren’t enough, or if you want to colorize the line | ||
numbers, or whatever. | ||
- transform: `Function`. It is called for each line and passed an object with | ||
the following properties: | ||
- before: `options.before` | ||
- number: `Number`. The current line number. | ||
- width: `Number`. The padded width of the line numbers. | ||
- after: `options.after` | ||
- line: `String`. The current line. | ||
You may modify the above properties to alter the line numbering for the | ||
current line. This is useful if `before` and `after` aren’t enough, if you | ||
want to colorize the line numbers, or highlight the current line. | ||
License | ||
@@ -56,0 +67,0 @@ ======= |
@@ -1,2 +0,2 @@ | ||
// Copyright 2014 Simon Lydell | ||
// Copyright 2014, 2015 Simon Lydell | ||
// X11 (“MIT”) Licensed. (See LICENSE.) | ||
@@ -23,2 +23,16 @@ | ||
var defaultOutput = [ | ||
" 1 | /**", | ||
" 2 | * Sums two numbers.", | ||
" 3 | *", | ||
" 4 | * @param a Number", | ||
" 5 | * @param b Number", | ||
" 6 | * @returns Number", | ||
" 7 | */", | ||
" 8 | ", | ||
" 9 | function sum(a, b) {", | ||
" 10 | return a + b", | ||
" 11 | }" | ||
] | ||
suite("lineNumbers", function() { | ||
@@ -32,18 +46,11 @@ | ||
test("defaults", function() { | ||
assert.equal(lineNumbers(sumJS.join("\n")), [ | ||
" 1 | /**", | ||
" 2 | * Sums two numbers.", | ||
" 3 | *", | ||
" 4 | * @param a Number", | ||
" 5 | * @param b Number", | ||
" 6 | * @returns Number", | ||
" 7 | */", | ||
" 8 | ", | ||
" 9 | function sum(a, b) {", | ||
" 10 | return a + b", | ||
" 11 | }" | ||
].join("\n")) | ||
assert.equal(lineNumbers(sumJS.join("\n")), defaultOutput.join("\n")) | ||
}) | ||
test("array of lines", function() { | ||
assert.deepEqual(lineNumbers(sumJS), defaultOutput) | ||
}) | ||
test("options", function() { | ||
@@ -53,19 +60,25 @@ assert.equal(lineNumbers(sumJS.join("\n"), { | ||
padding: "0", | ||
before: "", | ||
before: " ", | ||
after: ": ", | ||
transform: function(string) { | ||
return string.replace(/13/, "--") | ||
transform: function(params) { | ||
if (params.number === 13) { | ||
params.line = params.line + "\n" + params.before + | ||
Array(params.width + 1).join(" ") + params.after + | ||
Array(params.line.indexOf("(") + 1).join(" ") + "^" | ||
params.before = params.before.replace(/^./, ">") | ||
} | ||
} | ||
}), [ | ||
"05: /**", | ||
"06: * Sums two numbers.", | ||
"07: *", | ||
"08: * @param a Number", | ||
"09: * @param b Number", | ||
"10: * @returns Number", | ||
"11: */", | ||
"12: ", | ||
"--: function sum(a, b) {", | ||
"14: return a + b", | ||
"15: }" | ||
" 05: /**", | ||
" 06: * Sums two numbers.", | ||
" 07: *", | ||
" 08: * @param a Number", | ||
" 09: * @param b Number", | ||
" 10: * @returns Number", | ||
" 11: */", | ||
" 12: ", | ||
"> 13: function sum(a, b) {", | ||
" : ^", | ||
" 14: return a + b", | ||
" 15: }" | ||
].join("\n")) | ||
@@ -72,0 +85,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7205
9504
112
68