├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | tmp.js -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | example.js 11 | LICENSE.md 12 | tmp.js -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Jam3 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # add-line-numbers 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | Adds line numbers to a source string, padding left and starting at the given offset. 6 | 7 | ## Example 8 | 9 | #### example.js 10 | 11 | ```js 12 | var addLineNumbers = require('add-line-numbers') 13 | var stdin = require('get-stdin') 14 | 15 | stdin(function (body) { 16 | var src = addLineNumbers(body.toString()) 17 | process.stdout.write(src + '\n') 18 | }) 19 | ``` 20 | 21 | Now run the following in bash: 22 | 23 | ```sh 24 | node example.js < some-file.js 25 | ``` 26 | 27 | Resulting output: 28 | 29 | ```js 30 | 1: var addLineNumbers = require('./') 31 | 2: var test = require('tape') 32 | 3: 33 | 4: test('adds line numbers to a source string', function (t) { 34 | 5: t.equal(addLineNumbers([ 35 | 6: 'one', 36 | 7: 'is second' 37 | 8: ].join('\r\n')), '1: one\n2: is second', 'return carriage') 38 | 9: 39 | 10: t.equal(addLineNumbers([ 40 | 11: 'one', 41 | 12: 'is second' 42 | 13: ].join('\n'), 0), '0: one\n1: is second', 'start offset') 43 | 14: 44 | ... 45 | ``` 46 | 47 | ## Usage 48 | 49 | [![NPM](https://nodei.co/npm/add-line-numbers.png)](https://www.npmjs.com/package/add-line-numbers) 50 | 51 | #### `str = addLineNumbers(str, start, delimiter)` 52 | 53 | Adds a number to the start of each line in the `str` text. 54 | 55 | - `start` (Number) number to start counting at, defaults to 1 56 | - `delimiter` (String) joins the number and line, defaults to `": "` 57 | 58 | Returns the transformed string. 59 | 60 | ## License 61 | 62 | MIT, see [LICENSE.md](http://github.com/Jam3/add-line-numbers/blob/master/LICENSE.md) for details. 63 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var addLineNumbers = require('./') 2 | var stdin = require('get-stdin') 3 | 4 | stdin(function (body) { 5 | var src = addLineNumbers(body.toString()) 6 | process.stdout.write(src + '\n') 7 | }) 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var padLeft = require('pad-left') 2 | 3 | module.exports = addLineNumbers 4 | function addLineNumbers (string, start, delim) { 5 | start = typeof start === 'number' ? start : 1 6 | delim = delim || ': ' 7 | 8 | var lines = string.split(/\r?\n/) 9 | var totalDigits = String(lines.length + start - 1).length 10 | return lines.map(function (line, i) { 11 | var c = i + start 12 | var digits = String(c).length 13 | var prefix = padLeft(c, totalDigits - digits) 14 | return prefix + delim + line 15 | }).join('\n') 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "add-line-numbers", 3 | "version": "1.0.1", 4 | "description": "adds line numbers to a source string", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Matt DesLauriers", 9 | "email": "dave.des@gmail.com", 10 | "url": "https://github.com/mattdesl" 11 | }, 12 | "dependencies": { 13 | "pad-left": "^1.0.2" 14 | }, 15 | "devDependencies": { 16 | "faucet": "0.0.1", 17 | "get-stdin": "^4.0.1", 18 | "tape": "^4.0.0" 19 | }, 20 | "scripts": { 21 | "test": "node test.js | faucet" 22 | }, 23 | "keywords": [ 24 | "add", 25 | "line", 26 | "numbers", 27 | "pad", 28 | "left", 29 | "padding", 30 | "pads", 31 | "space", 32 | "indent", 33 | "num" 34 | ], 35 | "repository": { 36 | "type": "git", 37 | "url": "git://github.com/Jam3/add-line-numbers.git" 38 | }, 39 | "homepage": "https://github.com/Jam3/add-line-numbers", 40 | "bugs": { 41 | "url": "https://github.com/Jam3/add-line-numbers/issues" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var addLineNumbers = require('./') 2 | var test = require('tape') 3 | 4 | test('adds line numbers to a source string', function (t) { 5 | t.equal(addLineNumbers([ 6 | 'one', 7 | 'is second' 8 | ].join('\r\n')), '1: one\n2: is second', 'return carriage') 9 | 10 | t.equal(addLineNumbers([ 11 | 'one', 12 | 'is second' 13 | ].join('\n'), 0), '0: one\n1: is second', 'start offset') 14 | 15 | t.equal(addLineNumbers([ 16 | 'one', 17 | 'is second' 18 | ].join('\n'), 0, '-'), '0-one\n1-is second', 'delim') 19 | 20 | t.equal(addLineNumbers([ 21 | 'one', 22 | 'is second' 23 | ].join('\n'), 9, '-'), ' 9-one\n10-is second', 'padding') 24 | t.end() 25 | }) 26 | --------------------------------------------------------------------------------