Skip to content

Commit

Permalink
🚀 v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hhsnopek committed Aug 19, 2016
0 parents commit 77a7378
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output
coverage
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test
.editorconfig
.travis.yml
coverage
.nyc_output
node_modules
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
sudo: false
node_js:
- 6
after_script:
- npm run coveralls
22 changes: 22 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const padLeft = require('left-pad')

module.exports = (input, line, col = -1) => {
let code, start, error

// split input by linebreaks
code = input.split('\n')

// find line in input (count new lines)
start = code[line - 2] // line before error
error = code[line - 1] // line containing error

// A little ugly, but we can handle line zero this way
let frame = []
if (line - 1 === 0) frame.push(' 0. |')
else frame.push(` ${line - 1}. | ${start}`)
frame.push(` > ${line}. | ${error}`)
if (col !== -1) frame.push(` | ${padLeft('^', col + 1, ' ')}`)
frame.push(` ${line + 1}. |`)

return frame.join('\n')
}
21 changes: 21 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Henry Snopek

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.
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "code-frame",
"version": "1.0.0",
"description": "Got an error, give me the src/line/col and I'll frame it for you.",
"main": "lib/index.js",
"scripts": {
"test": "npm run lint && nyc ava",
"lint": "standard | snazzy",
"coverage": "npm test && nyc report --reporter=html && open ./coverage/index.html",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"ava": {
"verbose": "true"
},
"engines": {
"node": ">= 6.0.0"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/hhsnopek/code-frame.git"
},
"author": "Henry Snopek <[email protected]> (http://hhsnopek.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/hhsnopek/code-frame/issues"
},
"homepage": "https://github.com/hhsnopek/code-frame#readme",
"devDependencies": {
"ava": "^0.16.0",
"coveralls": "^2.11.12",
"nyc": "^8.1.0",
"snazzy": "^4.0.1",
"standard": "^7.1.2"
},
"dependencies": {
"left-pad": "^1.1.1"
}
}
42 changes: 42 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Code Frame
[![Travis]]()
[![Coveralls]]()
[![NPM]]()
[![L]][MIT]

Minimal Code Frame like babel-code-frame, but smaller

## Usage

```javascript
const framer = require('code-frame')

// framer(input, line[, col])
// input - string
// line - int
// col - int (optional)

framer('\nfunction (foo)\n{\tconsole.log(foo\n}', 2, 15)
// 1. |
// > 2. | function (foo)
// | ^
// 3. |

// Without col
framer('\n\n\tconsole.logfoo, bar)', 3)
// 2. |
// > 3. | console.logfoo, bar)
// 4. |
```

## Development
requirements:
- node: 6.0 >=

cmds: `npm test`

[Travis]: //img.shields.io/travis/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
[Coveralls]: //img.shields.io/coveralls/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
[NPM]: //img.shields.io/npm/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
[L]: //img.shields.io/npm/l/code-frame.svg?maxAge=2592000
[MIT]: license.md
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const test = require('ava')
const framer = require('..')

test('basic output', (t) => {
let error = framer('\nfunction (foo)\n{\tconsole.log(foo\n}', 2, 15)
let expected =
' 1. | \n > 2. | function (foo)\n | ^\n 3. |'
t.is(error, expected)
})

test('error at line 1', (t) => {
let error = framer('function (foo, bar\n{\tconsole.log(foo, bar)\n}', 1, 17)
let expected =
' 0. |\n > 1. | function (foo, bar\n | ^\n 2. |'
t.is(error, expected)
})

test('no col passed', (t) => {
let error = framer('\n\n\tconsole.logfoo, bar)', 3)
let expected =
' 2. | \n > 3. | \tconsole.logfoo, bar)\n 4. |'
t.is(error, expected)
})

0 comments on commit 77a7378

Please sign in to comment.