slice-ansi 3.0.0 → 4.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 (4) hide show
  1. package/index.js +20 -14
  2. package/license +1 -0
  3. package/package.json +6 -5
  4. package/readme.md +0 -6
package/index.js CHANGED
@@ -16,17 +16,17 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
16
16
 
17
17
  for (let ansiCode of ansiCodes) {
18
18
  const ansiCodeOrigin = ansiCode;
19
- if (ansiCode.match(';')) {
19
+ if (ansiCode.includes(';')) {
20
20
  ansiCode = ansiCode.split(';')[0][0] + '0';
21
21
  }
22
22
 
23
- const item = ansiStyles.codes.get(parseInt(ansiCode, 10));
23
+ const item = ansiStyles.codes.get(Number.parseInt(ansiCode, 10));
24
24
  if (item) {
25
25
  const indexEscape = ansiCodes.indexOf(item.toString());
26
- if (indexEscape >= 0) {
27
- ansiCodes.splice(indexEscape, 1);
28
- } else {
26
+ if (indexEscape === -1) {
29
27
  output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin));
28
+ } else {
29
+ ansiCodes.splice(indexEscape, 1);
30
30
  }
31
31
  } else if (isEscapes) {
32
32
  output.push(wrapAnsi(0));
@@ -38,8 +38,9 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
38
38
 
39
39
  if (isEscapes) {
40
40
  output = output.filter((element, index) => output.indexOf(element) === index);
41
+
41
42
  if (endAnsiCode !== undefined) {
42
- const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(parseInt(endAnsiCode, 10)));
43
+ const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(Number.parseInt(endAnsiCode, 10)));
43
44
  output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
44
45
  }
45
46
  }
@@ -48,11 +49,10 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
48
49
  };
49
50
 
50
51
  module.exports = (string, begin, end) => {
51
- const characters = [...string.normalize()];
52
+ const characters = [...string];
52
53
  const ansiCodes = [];
53
54
 
54
- end = typeof end === 'number' ? end : characters.length;
55
-
55
+ let stringEnd = typeof end === 'number' ? end : characters.length;
56
56
  let isInsideEscape = false;
57
57
  let ansiCode;
58
58
  let visible = 0;
@@ -64,8 +64,10 @@ module.exports = (string, begin, end) => {
64
64
  if (ESCAPES.includes(character)) {
65
65
  const code = /\d[^m]*/.exec(string.slice(index, index + 18));
66
66
  ansiCode = code && code.length > 0 ? code[0] : undefined;
67
- if (visible < end) {
67
+
68
+ if (visible < stringEnd) {
68
69
  isInsideEscape = true;
70
+
69
71
  if (ansiCode !== undefined) {
70
72
  ansiCodes.push(ansiCode);
71
73
  }
@@ -76,18 +78,22 @@ module.exports = (string, begin, end) => {
76
78
  }
77
79
 
78
80
  if (!isInsideEscape && !leftEscape) {
79
- ++visible;
81
+ visible++;
80
82
  }
81
83
 
82
84
  if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
83
- ++visible;
85
+ visible++;
86
+
87
+ if (typeof end !== 'number') {
88
+ stringEnd++;
89
+ }
84
90
  }
85
91
 
86
- if (visible > begin && visible <= end) {
92
+ if (visible > begin && visible <= stringEnd) {
87
93
  output += character;
88
94
  } else if (visible === begin && !isInsideEscape && ansiCode !== undefined) {
89
95
  output = checkAnsi(ansiCodes);
90
- } else if (visible >= end) {
96
+ } else if (visible >= stringEnd) {
91
97
  output += checkAnsi(ansiCodes, true, ansiCode);
92
98
  break;
93
99
  }
package/license CHANGED
@@ -1,6 +1,7 @@
1
1
  MIT License
2
2
 
3
3
  Copyright (c) DC <threedeecee@gmail.com>
4
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
5
 
5
6
  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:
6
7
 
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "slice-ansi",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Slice a string with ANSI escape codes",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/slice-ansi",
7
+ "funding": "https://github.com/chalk/slice-ansi?sponsor=1",
7
8
  "engines": {
8
- "node": ">=8"
9
+ "node": ">=10"
9
10
  },
10
11
  "scripts": {
11
12
  "test": "xo && ava"
@@ -43,9 +44,9 @@
43
44
  },
44
45
  "devDependencies": {
45
46
  "ava": "^2.1.0",
46
- "chalk": "^2.4.2",
47
+ "chalk": "^3.0.0",
47
48
  "random-item": "^3.0.0",
48
- "strip-ansi": "^5.0.0",
49
- "xo": "^0.24.0"
49
+ "strip-ansi": "^6.0.0",
50
+ "xo": "^0.26.1"
50
51
  }
51
52
  }
package/readme.md CHANGED
@@ -2,14 +2,12 @@
2
2
 
3
3
  > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
4
4
 
5
-
6
5
  ## Install
7
6
 
8
7
  ```
9
8
  $ npm install slice-ansi
10
9
  ```
11
10
 
12
-
13
11
  ## Usage
14
12
 
15
13
  ```js
@@ -22,7 +20,6 @@ const string = 'The quick brown ' + chalk.red('fox jumped over ') +
22
20
  console.log(sliceAnsi(string, 20, 30));
23
21
  ```
24
22
 
25
-
26
23
  ## API
27
24
 
28
25
  ### sliceAnsi(string, beginSlice, endSlice?)
@@ -45,20 +42,17 @@ Type: `number`
45
42
 
46
43
  Zero-based index at which to end the slice.
47
44
 
48
-
49
45
  ## Related
50
46
 
51
47
  - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
52
48
  - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal
53
49
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
54
50
 
55
-
56
51
  ## Maintainers
57
52
 
58
53
  - [Sindre Sorhus](https://github.com/sindresorhus)
59
54
  - [Josh Junon](https://github.com/qix-)
60
55
 
61
-
62
56
  ---
63
57
 
64
58
  <div align="center">