Skip to content

Commit 77a7378

Browse files
committed
🚀 v1.0.0
0 parents  commit 77a7378

9 files changed

+173
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
indent_style = space
12+
indent_size = 2

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.nyc_output
3+
coverage

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test
2+
.editorconfig
3+
.travis.yml
4+
coverage
5+
.nyc_output
6+
node_modules

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
sudo: false
3+
node_js:
4+
- 6
5+
after_script:
6+
- npm run coveralls

lib/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const padLeft = require('left-pad')
2+
3+
module.exports = (input, line, col = -1) => {
4+
let code, start, error
5+
6+
// split input by linebreaks
7+
code = input.split('\n')
8+
9+
// find line in input (count new lines)
10+
start = code[line - 2] // line before error
11+
error = code[line - 1] // line containing error
12+
13+
// A little ugly, but we can handle line zero this way
14+
let frame = []
15+
if (line - 1 === 0) frame.push(' 0. |')
16+
else frame.push(` ${line - 1}. | ${start}`)
17+
frame.push(` > ${line}. | ${error}`)
18+
if (col !== -1) frame.push(` | ${padLeft('^', col + 1, ' ')}`)
19+
frame.push(` ${line + 1}. |`)
20+
21+
return frame.join('\n')
22+
}

license.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Henry Snopek
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 all
13+
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 THE
21+
SOFTWARE.

package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "code-frame",
3+
"version": "1.0.0",
4+
"description": "Got an error, give me the src/line/col and I'll frame it for you.",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "npm run lint && nyc ava",
8+
"lint": "standard | snazzy",
9+
"coverage": "npm test && nyc report --reporter=html && open ./coverage/index.html",
10+
"coveralls": "nyc report --reporter=text-lcov | coveralls"
11+
},
12+
"ava": {
13+
"verbose": "true"
14+
},
15+
"engines": {
16+
"node": ">= 6.0.0"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "git+ssh://git@github.com/hhsnopek/code-frame.git"
21+
},
22+
"author": "Henry Snopek <hhsnopek@gmail.com> (http://hhsnopek.com/)",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/hhsnopek/code-frame/issues"
26+
},
27+
"homepage": "https://github.com/hhsnopek/code-frame#readme",
28+
"devDependencies": {
29+
"ava": "^0.16.0",
30+
"coveralls": "^2.11.12",
31+
"nyc": "^8.1.0",
32+
"snazzy": "^4.0.1",
33+
"standard": "^7.1.2"
34+
},
35+
"dependencies": {
36+
"left-pad": "^1.1.1"
37+
}
38+
}

readme.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Code Frame
2+
[![Travis]]()
3+
[![Coveralls]]()
4+
[![NPM]]()
5+
[![L]][MIT]
6+
7+
Minimal Code Frame like babel-code-frame, but smaller
8+
9+
## Usage
10+
11+
```javascript
12+
const framer = require('code-frame')
13+
14+
// framer(input, line[, col])
15+
// input - string
16+
// line - int
17+
// col - int (optional)
18+
19+
framer('\nfunction (foo)\n{\tconsole.log(foo\n}', 2, 15)
20+
// 1. |
21+
// > 2. | function (foo)
22+
// | ^
23+
// 3. |
24+
25+
// Without col
26+
framer('\n\n\tconsole.logfoo, bar)', 3)
27+
// 2. |
28+
// > 3. | console.logfoo, bar)
29+
// 4. |
30+
```
31+
32+
## Development
33+
requirements:
34+
- node: 6.0 >=
35+
36+
cmds: `npm test`
37+
38+
[Travis]: //img.shields.io/travis/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
39+
[Coveralls]: //img.shields.io/coveralls/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
40+
[NPM]: //img.shields.io/npm/hhsnopek/code-frame.svg?maxAge=2592000?style=flat-square
41+
[L]: //img.shields.io/npm/l/code-frame.svg?maxAge=2592000
42+
[MIT]: license.md

test/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const test = require('ava')
2+
const framer = require('..')
3+
4+
test('basic output', (t) => {
5+
let error = framer('\nfunction (foo)\n{\tconsole.log(foo\n}', 2, 15)
6+
let expected =
7+
' 1. | \n > 2. | function (foo)\n | ^\n 3. |'
8+
t.is(error, expected)
9+
})
10+
11+
test('error at line 1', (t) => {
12+
let error = framer('function (foo, bar\n{\tconsole.log(foo, bar)\n}', 1, 17)
13+
let expected =
14+
' 0. |\n > 1. | function (foo, bar\n | ^\n 2. |'
15+
t.is(error, expected)
16+
})
17+
18+
test('no col passed', (t) => {
19+
let error = framer('\n\n\tconsole.logfoo, bar)', 3)
20+
let expected =
21+
' 2. | \n > 3. | \tconsole.logfoo, bar)\n 4. |'
22+
t.is(error, expected)
23+
})

0 commit comments

Comments
 (0)