code-error-fragment
Advanced tools
+150
| /*! | ||
| * repeat-string <https://github.com/jonschlinkert/repeat-string> | ||
| * | ||
| * Copyright (c) 2014-2015, Jon Schlinkert. | ||
| * Licensed under the MIT License. | ||
| */ | ||
| 'use strict'; | ||
| /** | ||
| * Results cache | ||
| */ | ||
| var res = ''; | ||
| var cache; | ||
| /** | ||
| * Expose `repeat` | ||
| */ | ||
| var repeatString = repeat; | ||
| /** | ||
| * Repeat the given `string` the specified `number` | ||
| * of times. | ||
| * | ||
| * **Example:** | ||
| * | ||
| * ```js | ||
| * var repeat = require('repeat-string'); | ||
| * repeat('A', 5); | ||
| * //=> AAAAA | ||
| * ``` | ||
| * | ||
| * @param {String} `string` The string to repeat | ||
| * @param {Number} `number` The number of times to repeat the string | ||
| * @return {String} Repeated string | ||
| * @api public | ||
| */ | ||
| function repeat(str, num) { | ||
| if (typeof str !== 'string') { | ||
| throw new TypeError('expected a string'); | ||
| } | ||
| // cover common, quick use cases | ||
| if (num === 1) return str; | ||
| if (num === 2) return str + str; | ||
| var max = str.length * num; | ||
| if (cache !== str || typeof cache === 'undefined') { | ||
| cache = str; | ||
| res = ''; | ||
| } else if (res.length >= max) { | ||
| return res.substr(0, max); | ||
| } | ||
| while (max > res.length && num > 1) { | ||
| if (num & 1) { | ||
| res += str; | ||
| } | ||
| num >>= 1; | ||
| str += str; | ||
| } | ||
| res += str; | ||
| res = res.substr(0, max); | ||
| return res; | ||
| } | ||
| 'use strict'; | ||
| var padStart = function (string, maxLength, fillString) { | ||
| if (string == null || maxLength == null) { | ||
| return string; | ||
| } | ||
| var result = String(string); | ||
| var targetLen = typeof maxLength === 'number' | ||
| ? maxLength | ||
| : parseInt(maxLength, 10); | ||
| if (isNaN(targetLen) || !isFinite(targetLen)) { | ||
| return result; | ||
| } | ||
| var length = result.length; | ||
| if (length >= targetLen) { | ||
| return result; | ||
| } | ||
| var fill = fillString == null ? '' : String(fillString); | ||
| if (fill === '') { | ||
| fill = ' '; | ||
| } | ||
| var fillLen = targetLen - length; | ||
| while (fill.length < fillLen) { | ||
| fill += fill; | ||
| } | ||
| var truncated = fill.length > fillLen ? fill.substr(0, fillLen) : fill; | ||
| return truncated + result; | ||
| }; | ||
| function printLine(line, position, maxNumLength, settings) { | ||
| const num = String(position); | ||
| const formattedNum = padStart(num, maxNumLength, ' '); | ||
| const tabReplacement = repeatString(' ', settings.tabSize); | ||
| return formattedNum + ' | ' + line.replace(/\t/g, tabReplacement); | ||
| } | ||
| function printLines(lines, start, end, maxNumLength, settings) { | ||
| return lines | ||
| .slice(start, end) | ||
| .map((line, i) => printLine(line, start + i + 1, maxNumLength, settings)) | ||
| .join('\n'); | ||
| } | ||
| const defaultSettings = { | ||
| extraLines: 2, | ||
| tabSize: 4 | ||
| }; | ||
| var index = (input, linePos, columnPos, settings) => { | ||
| settings = Object.assign({}, defaultSettings, settings); | ||
| const lines = input.split(/\r\n?|\n|\f/); | ||
| const startLinePos = Math.max(1, linePos - settings.extraLines) - 1; | ||
| const endLinePos = Math.min(linePos + settings.extraLines, lines.length); | ||
| const maxNumLength = String(endLinePos).length; | ||
| const prevLines = printLines(lines, startLinePos, linePos, maxNumLength, settings); | ||
| const targetLineBeforeCursor = printLine(lines[linePos - 1].substring(0, columnPos - 1), linePos, maxNumLength, settings); | ||
| const cursorLine = repeatString(' ', targetLineBeforeCursor.length) + '^'; | ||
| const nextLines = printLines(lines, linePos, endLinePos, maxNumLength, settings); | ||
| return [prevLines, cursorLine, nextLines] | ||
| .filter(Boolean) | ||
| .join('\n'); | ||
| }; | ||
| export default index; |
+62
-0
@@ -16,2 +16,64 @@ (function (global, factory) { | ||
| /** | ||
| * Results cache | ||
| */ | ||
| var res = ''; | ||
| var cache; | ||
| /** | ||
| * Expose `repeat` | ||
| */ | ||
| var repeatString = repeat; | ||
| /** | ||
| * Repeat the given `string` the specified `number` | ||
| * of times. | ||
| * | ||
| * **Example:** | ||
| * | ||
| * ```js | ||
| * var repeat = require('repeat-string'); | ||
| * repeat('A', 5); | ||
| * //=> AAAAA | ||
| * ``` | ||
| * | ||
| * @param {String} `string` The string to repeat | ||
| * @param {Number} `number` The number of times to repeat the string | ||
| * @return {String} Repeated string | ||
| * @api public | ||
| */ | ||
| function repeat(str, num) { | ||
| if (typeof str !== 'string') { | ||
| throw new TypeError('expected a string'); | ||
| } | ||
| // cover common, quick use cases | ||
| if (num === 1) return str; | ||
| if (num === 2) return str + str; | ||
| var max = str.length * num; | ||
| if (cache !== str || typeof cache === 'undefined') { | ||
| cache = str; | ||
| res = ''; | ||
| } else if (res.length >= max) { | ||
| return res.substr(0, max); | ||
| } | ||
| while (max > res.length && num > 1) { | ||
| if (num & 1) { | ||
| res += str; | ||
| } | ||
| num >>= 1; | ||
| str += str; | ||
| } | ||
| res += str; | ||
| res = res.substr(0, max); | ||
| return res; | ||
| } | ||
| 'use strict'; | ||
@@ -18,0 +80,0 @@ |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import nprepeatString from 'repeat-string'; | ||
| import repeatString from 'repeat-string'; | ||
| import padStart from 'pad-start'; | ||
@@ -3,0 +3,0 @@ |
+3
-3
| { | ||
| "name": "code-error-fragment", | ||
| "version": "0.0.216", | ||
| "version": "0.0.217", | ||
| "author": "Vlad Trushin", | ||
@@ -16,4 +16,4 @@ "homepage": "https://github.com/vtrushin/code-error-fragment", | ||
| "main": "build.js", | ||
| "module": "index", | ||
| "jsnext:main": "index", | ||
| "module": "module", | ||
| "jsnext:main": "module", | ||
| "dependencies": { | ||
@@ -20,0 +20,0 @@ "pad-start": "^1.0.2", |
+43
-25
@@ -6,27 +6,9 @@ import commonjs from 'rollup-plugin-commonjs'; | ||
| export default { | ||
| const plugins = [ | ||
| commonjs(), | ||
| resolve() | ||
| ]; | ||
| const settings = { | ||
| input: 'index.js', | ||
| name: 'codeErrorFragment', | ||
| output: { | ||
| file: 'build.js', | ||
| format: 'umd' | ||
| }, | ||
| plugins: [ | ||
| resolve(), | ||
| commonjs(), | ||
| babel({ | ||
| exclude: 'node_modules/**', | ||
| presets: [ | ||
| ['es2015', { | ||
| modules: false | ||
| }], | ||
| 'stage-3' | ||
| ], | ||
| plugins: [ | ||
| 'external-helpers' | ||
| ], | ||
| // fixing temporary rollup's regression, remove when https://github.com/rollup/rollup/issues/1595 gets solved | ||
| externalHelpersWhitelist: babelHelpersList.filter(helperName => helperName !== 'asyncGenerator'), | ||
| }) | ||
| ], | ||
| watch: { | ||
@@ -36,2 +18,38 @@ include: '*.js', | ||
| } | ||
| } | ||
| }; | ||
| export default [ | ||
| // ES5 build | ||
| Object.assign({}, settings, { | ||
| name: 'codeErrorFragment', | ||
| output: { | ||
| file: 'build.js', | ||
| format: 'umd' | ||
| }, | ||
| plugins: plugins.concat( | ||
| babel({ | ||
| exclude: 'node_modules/**', | ||
| presets: [ | ||
| ['es2015', { | ||
| modules: false | ||
| }], | ||
| 'stage-3' | ||
| ], | ||
| plugins: [ | ||
| 'external-helpers' | ||
| ], | ||
| // fixing temporary rollup's regression, remove when https://github.com/rollup/rollup/issues/1595 gets solved | ||
| externalHelpersWhitelist: babelHelpersList.filter(helperName => helperName !== 'asyncGenerator'), | ||
| }) | ||
| ) | ||
| }), | ||
| // ES6 module | ||
| Object.assign({}, settings, { | ||
| output: { | ||
| file: 'module.js', | ||
| format: 'es' | ||
| }, | ||
| plugins | ||
| }) | ||
| ] |
11686
64.78%8
14.29%314
137.88%