Permalink
Browse files

Initial commit

  • Loading branch information...
lydell committed Dec 20, 2014
0 parents commit 7c9c1484867fa2c8e8874bff98f62576464aa9a2
Showing with 233 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 .travis.yml
  3. +21 −0 LICENSE
  4. +3 −0 changelog.md
  5. +28 −0 index.js
  6. +28 −0 package.json
  7. +56 −0 readme.md
  8. +92 −0 test/index.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - "0.11"
+ - "0.10"
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Simon Lydell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,3 @@
+### Version 0.1.0 (Unreleased) ###
+
+- Initial release.
@@ -0,0 +1,28 @@
+// Copyright 2014 Simon Lydell
+// X11 (“MIT”) Licensed. (See LICENSE.)
+
+var leftPad = require("left-pad")
+
+function identity(arg) { return arg }
+
+function get(options, key, defaultValue) {
+ return (key in options ? options[key] : defaultValue)
+}
+
+function lineNumbers(string, options) {
+ var getOption = get.bind(null, options || {})
+ var transform = getOption("transform", identity)
+ var padding = getOption("padding", " ")
+ var before = getOption("before", " ")
+ var after = getOption("after", " | ")
+ var start = getOption("start", 1)
+ var lines = string.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")
+}
+
+module.exports = lineNumbers
@@ -0,0 +1,28 @@
+{
+ "name": "line-numbers",
+ "version": "0.1.0",
+ "author": "Simon Lydell",
+ "license": "MIT",
+ "description": "Add line numbers to a string.",
+ "keywords": [
+ "line",
+ "number",
+ "numbers",
+ "file",
+ "add",
+ "insert",
+ "gutter",
+ "column",
+ "each"
+ ],
+ "repository": "lydell/line-numbers",
+ "scripts": {
+ "test": "mocha --ui tdd"
+ },
+ "devDependencies": {
+ "mocha": "^2.0.1"
+ },
+ "dependencies": {
+ "left-pad": "0.0.3"
+ }
+}
@@ -0,0 +1,56 @@
+Overview [![Build Status](https://travis-ci.org/lydell/line-numbers.svg?branch=master)](https://travis-ci.org/lydell/line-numbers)
+========
+
+Add line numbers to a string.
+
+```js
+var lineNumbers = require("line-numbers")
+
+var string = [
+ "function sum(a, b) {",
+ " return a + b;",
+ "}"
+].join("\n")
+
+lineNumbers(string)
+// 1 | function sum(a, b) {
+// 2 | return a + b;
+// 3 | }
+```
+
+
+Installation
+============
+
+- `npm install line-numbers`
+
+```js
+var lineNumbers = require("line-numbers")
+```
+
+
+Usage
+=====
+
+### `lineNumbers(string, [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.
+
+`options`:
+
+- start: `Number`. The number to use for the first line. Defaults to `1`.
+- padding: `String`. The character to pad numbers with. Defaults to `" "`.
+- before: `String`. String to put before the line number. Defaults to `" "`.
+- after: `String`. String to put between the line number and the line itself.
+ 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.
+
+
+License
+=======
+
+[The X11 (“MIT”) License](LICENSE).
@@ -0,0 +1,92 @@
+// Copyright 2014 Simon Lydell
+// X11 (“MIT”) Licensed. (See LICENSE.)
+
+var fs = require("fs")
+var assert = require("assert")
+
+var lineNumbers = require("../")
+
+var sumJS = [
+ "/**",
+ " * Sums two numbers.",
+ " *",
+ " * @param a Number",
+ " * @param b Number",
+ " * @returns Number",
+ " */",
+ "",
+ "function sum(a, b) {",
+ " return a + b",
+ "}"
+]
+
+suite("lineNumbers", function() {
+
+ test("is a function", function() {
+ assert.equal(typeof lineNumbers, "function")
+ })
+
+
+ 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"))
+ })
+
+
+ test("options", function() {
+ assert.equal(lineNumbers(sumJS.join("\n"), {
+ start: 5,
+ padding: "0",
+ before: "",
+ after: ": ",
+ transform: function(string) {
+ return string.replace(/13/, "--")
+ }
+ }), [
+ "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: }"
+ ].join("\n"))
+ })
+
+
+ test("Windows-style newlines", function() {
+ assert.equal(lineNumbers("a\r\nb"), " 1 | a\r\n 2 | b")
+ })
+
+
+ test("trailing newline", function() {
+ assert.equal(lineNumbers("single line\n"), " 1 | single line\n 2 | ")
+ })
+
+
+ test("no newline", function() {
+ assert.equal(lineNumbers("single line"), " 1 | single line")
+ })
+
+
+ test("one more digit", function() {
+ assert.equal(lineNumbers("a", {start: 9}), " 9 | a")
+ assert.equal(lineNumbers("a\nb", {start: 9}), " 9 | a\n 10 | b")
+ })
+
+})

0 comments on commit 7c9c148

Please sign in to comment.