slice-ansi
Advanced tools
Comparing version 0.0.4 to 0.1.0
87
index.js
'use strict'; | ||
var ESCAPES = [ | ||
'\u001b', | ||
'\u009b' | ||
const ESCAPES = [ | ||
'\u001B', | ||
'\u009B' | ||
]; | ||
var END_CODE = 39; | ||
const END_CODE = 39; | ||
var ESCAPE_CODES = { | ||
0: 0, | ||
1: 22, | ||
2: 22, | ||
3: 23, | ||
4: 24, | ||
7: 27, | ||
8: 28, | ||
9: 29, | ||
30: 39, | ||
31: 39, | ||
32: 39, | ||
33: 39, | ||
34: 39, | ||
35: 39, | ||
36: 39, | ||
37: 39, | ||
90: 39, | ||
40: 49, | ||
41: 49, | ||
42: 49, | ||
43: 49, | ||
44: 49, | ||
45: 49, | ||
46: 49, | ||
47: 49 | ||
}; | ||
const ESCAPE_CODES = new Map([ | ||
[0, 0], | ||
[1, 22], | ||
[2, 22], | ||
[3, 23], | ||
[4, 24], | ||
[7, 27], | ||
[8, 28], | ||
[9, 29], | ||
[30, 39], | ||
[31, 39], | ||
[32, 39], | ||
[33, 39], | ||
[34, 39], | ||
[35, 39], | ||
[36, 39], | ||
[37, 39], | ||
[90, 39], | ||
[40, 49], | ||
[41, 49], | ||
[42, 49], | ||
[43, 49], | ||
[44, 49], | ||
[45, 49], | ||
[46, 49], | ||
[47, 49] | ||
]); | ||
function wrapAnsi(code) { | ||
return ESCAPES[0] + '[' + code + 'm'; | ||
} | ||
const wrapAnsi = code => `${ESCAPES[0]}[${code}m`; | ||
module.exports = function (str, begin, end) { | ||
module.exports = (str, begin, end) => { | ||
end = end || str.length; | ||
var insideEscape = false; | ||
var escapeCode; | ||
var visible = 0; | ||
var output = ''; | ||
let insideEscape = false; | ||
let escapeCode; | ||
let visible = 0; | ||
let output = ''; | ||
for (var i = 0; i < str.length; i++) { | ||
var leftEscape = false; | ||
var x = str[i]; | ||
for (let i = 0; i < str.length; i++) { | ||
let leftEscape = false; | ||
const x = str[i]; | ||
if (ESCAPES.indexOf(x) !== -1) { | ||
insideEscape = true; | ||
var code = /[0-9][^m]*/.exec(str.slice(i, i + 4)); | ||
const code = /\d[^m]*/.exec(str.slice(i, i + 4)); | ||
escapeCode = code === END_CODE ? null : code; | ||
@@ -72,3 +70,3 @@ } else if (insideEscape && x === 'm') { | ||
if (escapeCode !== undefined) { | ||
output += wrapAnsi(ESCAPE_CODES[escapeCode] || END_CODE); | ||
output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE); | ||
} | ||
@@ -81,2 +79,1 @@ break; | ||
}; | ||
{ | ||
"name": "slice-ansi", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Slice a string with ANSI escape codes", | ||
@@ -11,13 +11,7 @@ "license": "MIT", | ||
}, | ||
"maintainers": [ | ||
"David Caccavella <threedeecee@gmail.com> (github.com/dthree)", | ||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)", | ||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)", | ||
"JD Ballard <i.am.qix@gmail.com> (github.com/qix-)" | ||
], | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "node test.js && xo" | ||
"test": "xo && ava" | ||
}, | ||
@@ -50,9 +44,9 @@ "files": [ | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"ava": "^0.2.0", | ||
"ava": "*", | ||
"chalk": "^1.1.1", | ||
"strip-ansi": "^3.0.0", | ||
"random-item": "^1.0.0", | ||
"strip-ansi": "^4.0.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -1,7 +0,5 @@ | ||
# slice-ansi | ||
# slice-ansi [![Build Status](https://travis-ci.org/chalk/slice-ansi.svg?branch=master)](https://travis-ci.org/chalk/slice-ansi) [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/sindresorhus/xo) | ||
[![Build Status](https://travis-ci.org/vorpaljs/slice-ansi.svg?branch=master)](https://travis-ci.org/vorpaljs/slice-ansi) | ||
[![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/sindresorhus/xo) | ||
> Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) | ||
> Slice a string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) | ||
@@ -11,12 +9,13 @@ ## Install | ||
``` | ||
$ npm install --save slice-ansi | ||
$ npm install slice-ansi | ||
``` | ||
## Usage | ||
```js | ||
var chalk = require('chalk'); | ||
var sliceAnsi = require('slice-ansi'); | ||
const chalk = require('chalk'); | ||
const sliceAnsi = require('slice-ansi'); | ||
var input = 'The quick brown ' + chalk.red('fox jumped over ') + | ||
const input = 'The quick brown ' + chalk.red('fox jumped over ') + | ||
'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); | ||
@@ -27,5 +26,6 @@ | ||
## API | ||
### sliceAnsi(input, beginSlice[, endSlice]) | ||
### sliceAnsi(input, beginSlice, [endSlice]) | ||
@@ -42,3 +42,3 @@ #### input | ||
The zero-based index at which to begin the slice. | ||
Zero-based index at which to begin the slice. | ||
@@ -49,3 +49,3 @@ #### endSlice | ||
Optional. The zero-based index at which to end the slice. | ||
Zero-based index at which to end the slice. | ||
@@ -55,7 +55,15 @@ | ||
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes | ||
- [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal | ||
- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right | ||
## Maintainers | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Josh Junon](https://github.com/qix-) | ||
## License | ||
MIT © [David Caccavella](https://githbu.com/dthree) | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65
4662
5
67
1