├── .gitignore ├── .travis.yml ├── LICENSE ├── changelog.md ├── index.js ├── package.json ├── readme.md └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014, 2015 Simon Lydell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | ### Version 0.2.2 (2016-03-23) ### 2 | 3 | - Deprecated the package. (See the readme.) 4 | 5 | 6 | ### Version 0.2.1 (2016-03-23) ### 7 | 8 | - Updated the left-pad dependecy to ^1.0.1. 9 | 10 | 11 | ### Version 0.2.0 (2015-02-21) ### 12 | 13 | - Improved: You may now pass an already split string as an array. 14 | - Changed: `options.transform` is now passed an object with all the elements of 15 | the current line, allowing you to modify any part of it. 16 | (Backwards-incompatible change.) 17 | 18 | 19 | ### Version 0.1.0 (2014-12-20) ### 20 | 21 | - Initial release. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Copyright 2014, 2015 Simon Lydell 2 | // X11 (“MIT”) Licensed. (See LICENSE.) 3 | 4 | var leftPad = require("left-pad") 5 | 6 | function get(options, key, defaultValue) { 7 | return (key in options ? options[key] : defaultValue) 8 | } 9 | 10 | function lineNumbers(code, options) { 11 | var getOption = get.bind(null, options || {}) 12 | var transform = getOption("transform", Function.prototype) 13 | var padding = getOption("padding", " ") 14 | var before = getOption("before", " ") 15 | var after = getOption("after", " | ") 16 | var start = getOption("start", 1) 17 | var isArray = Array.isArray(code) 18 | var lines = (isArray ? code : code.split("\n")) 19 | var end = start + lines.length - 1 20 | var width = String(end).length 21 | var numbered = lines.map(function(line, index) { 22 | var number = start + index 23 | var params = {before: before, number: number, width: width, after: after, 24 | line: line} 25 | transform(params) 26 | return params.before + leftPad(params.number, width, padding) + 27 | params.after + params.line 28 | }) 29 | return (isArray ? numbered : numbered.join("\n")) 30 | } 31 | 32 | module.exports = lineNumbers 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "line-numbers", 3 | "version": "0.2.2", 4 | "author": "Simon Lydell", 5 | "license": "MIT", 6 | "description": "Add line numbers to a string.", 7 | "keywords": [ 8 | "line", 9 | "number", 10 | "numbers", 11 | "file", 12 | "add", 13 | "insert", 14 | "gutter", 15 | "column", 16 | "each" 17 | ], 18 | "repository": "lydell/line-numbers", 19 | "scripts": { 20 | "test": "mocha --ui tdd" 21 | }, 22 | "devDependencies": { 23 | "mocha": "^2.0.1" 24 | }, 25 | "dependencies": { 26 | "left-pad": "^1.0.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Overview [![Build Status](https://travis-ci.org/lydell/line-numbers.svg?branch=master)](https://travis-ci.org/lydell/line-numbers) 2 | ======== 3 | 4 | **DEPRECATED.** This is a rather silly package that I do not recommend using. 5 | It's easier to copy the ~20 lines of code of this package and customize that 6 | code, rather than downloading and learning how to use this package. 7 | 8 | Add line numbers to a string. 9 | 10 | ```js 11 | var lineNumbers = require("line-numbers") 12 | 13 | var string = [ 14 | "function sum(a, b) {", 15 | " return a + b;", 16 | "}" 17 | ].join("\n") 18 | 19 | lineNumbers(string) 20 | // 1 | function sum(a, b) { 21 | // 2 | return a + b; 22 | // 3 | } 23 | ``` 24 | 25 | 26 | Installation 27 | ============ 28 | 29 | - `npm install line-numbers` 30 | 31 | ```js 32 | var lineNumbers = require("line-numbers") 33 | ``` 34 | 35 | 36 | Usage 37 | ===== 38 | 39 | ### `lineNumbers(code, [options])` ### 40 | 41 | Inserts a line number at the beginning of each line in `code`, which is either a 42 | string or an array of strings—one for each line. All the line numbers are of the 43 | same width; shorter numbers are padded on the left side. 44 | 45 | The return value is of the same type as `code`. 46 | 47 | `options`: 48 | 49 | - start: `Number`. The number to use for the first line. Defaults to `1`. 50 | - padding: `String`. The character to pad numbers with. Defaults to `" "`. 51 | - before: `String`. String to put before the line number. Defaults to `" "`. 52 | - after: `String`. String to put between the line number and the line itself. 53 | Defaults to `" | "`. 54 | - transform: `Function`. It is called for each line and passed an object with 55 | the following properties: 56 | 57 | - before: `options.before` 58 | - number: `Number`. The current line number. 59 | - width: `Number`. The padded width of the line numbers. 60 | - after: `options.after` 61 | - line: `String`. The current line. 62 | 63 | You may modify the above properties to alter the line numbering for the 64 | current line. This is useful if `before` and `after` aren’t enough, if you 65 | want to colorize the line numbers, or highlight the current line. 66 | 67 | 68 | License 69 | ======= 70 | 71 | [The X11 (“MIT”) License](LICENSE). 72 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | // Copyright 2014, 2015 Simon Lydell 2 | // X11 (“MIT”) Licensed. (See LICENSE.) 3 | 4 | var fs = require("fs") 5 | var assert = require("assert") 6 | 7 | var lineNumbers = require("../") 8 | 9 | var sumJS = [ 10 | "/**", 11 | " * Sums two numbers.", 12 | " *", 13 | " * @param a Number", 14 | " * @param b Number", 15 | " * @returns Number", 16 | " */", 17 | "", 18 | "function sum(a, b) {", 19 | " return a + b", 20 | "}" 21 | ] 22 | 23 | var defaultOutput = [ 24 | " 1 | /**", 25 | " 2 | * Sums two numbers.", 26 | " 3 | *", 27 | " 4 | * @param a Number", 28 | " 5 | * @param b Number", 29 | " 6 | * @returns Number", 30 | " 7 | */", 31 | " 8 | ", 32 | " 9 | function sum(a, b) {", 33 | " 10 | return a + b", 34 | " 11 | }" 35 | ] 36 | 37 | suite("lineNumbers", function() { 38 | 39 | test("is a function", function() { 40 | assert.equal(typeof lineNumbers, "function") 41 | }) 42 | 43 | 44 | test("defaults", function() { 45 | assert.equal(lineNumbers(sumJS.join("\n")), defaultOutput.join("\n")) 46 | }) 47 | 48 | 49 | test("array of lines", function() { 50 | assert.deepEqual(lineNumbers(sumJS), defaultOutput) 51 | }) 52 | 53 | 54 | test("options", function() { 55 | assert.equal(lineNumbers(sumJS.join("\n"), { 56 | start: 5, 57 | padding: "0", 58 | before: " ", 59 | after: ": ", 60 | transform: function(params) { 61 | if (params.number === 13) { 62 | params.line = params.line + "\n" + params.before + 63 | Array(params.width + 1).join(" ") + params.after + 64 | Array(params.line.indexOf("(") + 1).join(" ") + "^" 65 | params.before = params.before.replace(/^./, ">") 66 | } 67 | } 68 | }), [ 69 | " 05: /**", 70 | " 06: * Sums two numbers.", 71 | " 07: *", 72 | " 08: * @param a Number", 73 | " 09: * @param b Number", 74 | " 10: * @returns Number", 75 | " 11: */", 76 | " 12: ", 77 | "> 13: function sum(a, b) {", 78 | " : ^", 79 | " 14: return a + b", 80 | " 15: }" 81 | ].join("\n")) 82 | }) 83 | 84 | 85 | test("Windows-style newlines", function() { 86 | assert.equal(lineNumbers("a\r\nb"), " 1 | a\r\n 2 | b") 87 | }) 88 | 89 | 90 | test("trailing newline", function() { 91 | assert.equal(lineNumbers("single line\n"), " 1 | single line\n 2 | ") 92 | }) 93 | 94 | 95 | test("no newline", function() { 96 | assert.equal(lineNumbers("single line"), " 1 | single line") 97 | }) 98 | 99 | 100 | test("one more digit", function() { 101 | assert.equal(lineNumbers("a", {start: 9}), " 9 | a") 102 | assert.equal(lineNumbers("a\nb", {start: 9}), " 9 | a\n 10 | b") 103 | }) 104 | 105 | }) 106 | --------------------------------------------------------------------------------