slice-ansi 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +10 -40
  2. package/package.json +49 -51
  3. package/readme.md +1 -1
package/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
  const isFullwidthCodePoint = require('is-fullwidth-code-point');
3
+ const astralRegex = require('astral-regex');
4
+ const ansiStyles = require('ansi-styles');
3
5
 
4
6
  const ESCAPES = [
5
7
  '\u001B',
@@ -7,55 +9,23 @@ const ESCAPES = [
7
9
  ];
8
10
 
9
11
  const END_CODE = 39;
10
- const ASTRAL_REGEX = /[\uD800-\uDBFF][\uDC00-\uDFFF]/;
11
-
12
- const ESCAPE_CODES = new Map([
13
- [0, 0],
14
- [1, 22],
15
- [2, 22],
16
- [3, 23],
17
- [4, 24],
18
- [7, 27],
19
- [8, 28],
20
- [9, 29],
21
- [30, 39],
22
- [31, 39],
23
- [32, 39],
24
- [33, 39],
25
- [34, 39],
26
- [35, 39],
27
- [36, 39],
28
- [37, 39],
29
- [90, 39],
30
- [40, 49],
31
- [41, 49],
32
- [42, 49],
33
- [43, 49],
34
- [44, 49],
35
- [45, 49],
36
- [46, 49],
37
- [47, 49]
38
- ]);
39
12
 
40
13
  const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
41
14
 
42
15
  module.exports = (str, begin, end) => {
43
- const arr = Array.from(str.normalize());
16
+ const arr = [...str.normalize()];
44
17
 
45
18
  end = typeof end === 'number' ? end : arr.length;
46
19
 
47
20
  let insideEscape = false;
48
- let escapeCode;
21
+ let escapeCode = null;
49
22
  let visible = 0;
50
23
  let output = '';
51
24
 
52
- for (const item of arr.entries()) {
53
- const i = item[0];
54
- const x = item[1];
55
-
25
+ for (const [i, x] of arr.entries()) {
56
26
  let leftEscape = false;
57
27
 
58
- if (ESCAPES.indexOf(x) !== -1) {
28
+ if (ESCAPES.includes(x)) {
59
29
  insideEscape = true;
60
30
  const code = /\d[^m]*/.exec(str.slice(i, i + 4));
61
31
  escapeCode = code === END_CODE ? null : code;
@@ -68,17 +38,17 @@ module.exports = (str, begin, end) => {
68
38
  ++visible;
69
39
  }
70
40
 
71
- if (!ASTRAL_REGEX.test(x) && isFullwidthCodePoint(x.codePointAt())) {
41
+ if (!astralRegex({exact: true}).test(x) && isFullwidthCodePoint(x.codePointAt())) {
72
42
  ++visible;
73
43
  }
74
44
 
75
45
  if (visible > begin && visible <= end) {
76
46
  output += x;
77
- } else if (visible === begin && !insideEscape && escapeCode !== undefined && escapeCode !== END_CODE) {
47
+ } else if (visible === begin && !insideEscape && escapeCode !== null && escapeCode !== END_CODE) {
78
48
  output += wrapAnsi(escapeCode);
79
49
  } else if (visible >= end) {
80
- if (escapeCode !== undefined) {
81
- output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE);
50
+ if (escapeCode !== null) {
51
+ output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE);
82
52
  }
83
53
  break;
84
54
  }
package/package.json CHANGED
@@ -1,53 +1,51 @@
1
1
  {
2
- "name": "slice-ansi",
3
- "version": "1.0.0",
4
- "description": "Slice a string with ANSI escape codes",
5
- "license": "MIT",
6
- "repository": "chalk/slice-ansi",
7
- "author": {
8
- "name": "David Caccavella",
9
- "email": "threedeecee@gmail.com"
10
- },
11
- "engines": {
12
- "node": ">=4"
13
- },
14
- "scripts": {
15
- "test": "xo && ava"
16
- },
17
- "files": [
18
- "index.js"
19
- ],
20
- "keywords": [
21
- "slice",
22
- "string",
23
- "ansi",
24
- "styles",
25
- "color",
26
- "colour",
27
- "colors",
28
- "terminal",
29
- "console",
30
- "cli",
31
- "tty",
32
- "escape",
33
- "formatting",
34
- "rgb",
35
- "256",
36
- "shell",
37
- "xterm",
38
- "log",
39
- "logging",
40
- "command-line",
41
- "text"
42
- ],
43
- "dependencies": {
44
- "is-fullwidth-code-point": "^2.0.0"
45
- },
46
- "devDependencies": {
47
- "ava": "*",
48
- "chalk": "^2.0.1",
49
- "random-item": "^1.0.0",
50
- "strip-ansi": "^4.0.0",
51
- "xo": "*"
52
- }
2
+ "name": "slice-ansi",
3
+ "version": "2.0.0",
4
+ "description": "Slice a string with ANSI escape codes",
5
+ "license": "MIT",
6
+ "repository": "chalk/slice-ansi",
7
+ "engines": {
8
+ "node": ">=6"
9
+ },
10
+ "scripts": {
11
+ "test": "xo && ava"
12
+ },
13
+ "files": [
14
+ "index.js"
15
+ ],
16
+ "keywords": [
17
+ "slice",
18
+ "string",
19
+ "ansi",
20
+ "styles",
21
+ "color",
22
+ "colour",
23
+ "colors",
24
+ "terminal",
25
+ "console",
26
+ "cli",
27
+ "tty",
28
+ "escape",
29
+ "formatting",
30
+ "rgb",
31
+ "256",
32
+ "shell",
33
+ "xterm",
34
+ "log",
35
+ "logging",
36
+ "command-line",
37
+ "text"
38
+ ],
39
+ "dependencies": {
40
+ "ansi-styles": "^3.2.0",
41
+ "astral-regex": "^1.0.0",
42
+ "is-fullwidth-code-point": "^2.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "ava": "^0.25.0",
46
+ "chalk": "^2.0.1",
47
+ "random-item": "^1.0.0",
48
+ "strip-ansi": "^5.0.0",
49
+ "xo": "^0.23.0"
50
+ }
53
51
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # 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)
1
+ # 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/xojs/xo)
2
2
 
3
3
  > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
4
4