slice-ansi 2.0.0 → 5.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 +78 -31
  2. package/license +1 -0
  3. package/package.json +12 -10
  4. package/readme.md +18 -16
package/index.js CHANGED
@@ -1,58 +1,105 @@
1
- 'use strict';
2
- const isFullwidthCodePoint = require('is-fullwidth-code-point');
3
- const astralRegex = require('astral-regex');
4
- const ansiStyles = require('ansi-styles');
1
+ import isFullwidthCodePoint from 'is-fullwidth-code-point';
2
+ import ansiStyles from 'ansi-styles';
3
+
4
+ const astralRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/;
5
5
 
6
6
  const ESCAPES = [
7
7
  '\u001B',
8
8
  '\u009B'
9
9
  ];
10
10
 
11
- const END_CODE = 39;
12
-
13
11
  const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
14
12
 
15
- module.exports = (str, begin, end) => {
16
- const arr = [...str.normalize()];
13
+ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
14
+ let output = [];
15
+ ansiCodes = [...ansiCodes];
16
+
17
+ for (let ansiCode of ansiCodes) {
18
+ const ansiCodeOrigin = ansiCode;
19
+ if (ansiCode.includes(';')) {
20
+ ansiCode = ansiCode.split(';')[0][0] + '0';
21
+ }
22
+
23
+ const item = ansiStyles.codes.get(Number.parseInt(ansiCode, 10));
24
+ if (item) {
25
+ const indexEscape = ansiCodes.indexOf(item.toString());
26
+ if (indexEscape === -1) {
27
+ output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin));
28
+ } else {
29
+ ansiCodes.splice(indexEscape, 1);
30
+ }
31
+ } else if (isEscapes) {
32
+ output.push(wrapAnsi(0));
33
+ break;
34
+ } else {
35
+ output.push(wrapAnsi(ansiCodeOrigin));
36
+ }
37
+ }
38
+
39
+ if (isEscapes) {
40
+ output = output.filter((element, index) => output.indexOf(element) === index);
41
+
42
+ if (endAnsiCode !== undefined) {
43
+ const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(Number.parseInt(endAnsiCode, 10)));
44
+ // TODO: Remove the use of `.reduce` here.
45
+ // eslint-disable-next-line unicorn/no-array-reduce
46
+ output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
47
+ }
48
+ }
49
+
50
+ return output.join('');
51
+ };
17
52
 
18
- end = typeof end === 'number' ? end : arr.length;
53
+ export default function sliceAnsi(string, begin, end) {
54
+ const characters = [...string];
55
+ const ansiCodes = [];
19
56
 
20
- let insideEscape = false;
21
- let escapeCode = null;
57
+ let stringEnd = typeof end === 'number' ? end : characters.length;
58
+ let isInsideEscape = false;
59
+ let ansiCode;
22
60
  let visible = 0;
23
61
  let output = '';
24
62
 
25
- for (const [i, x] of arr.entries()) {
63
+ for (const [index, character] of characters.entries()) {
26
64
  let leftEscape = false;
27
65
 
28
- if (ESCAPES.includes(x)) {
29
- insideEscape = true;
30
- const code = /\d[^m]*/.exec(str.slice(i, i + 4));
31
- escapeCode = code === END_CODE ? null : code;
32
- } else if (insideEscape && x === 'm') {
33
- insideEscape = false;
66
+ if (ESCAPES.includes(character)) {
67
+ const code = /\d[^m]*/.exec(string.slice(index, index + 18));
68
+ ansiCode = code && code.length > 0 ? code[0] : undefined;
69
+
70
+ if (visible < stringEnd) {
71
+ isInsideEscape = true;
72
+
73
+ if (ansiCode !== undefined) {
74
+ ansiCodes.push(ansiCode);
75
+ }
76
+ }
77
+ } else if (isInsideEscape && character === 'm') {
78
+ isInsideEscape = false;
34
79
  leftEscape = true;
35
80
  }
36
81
 
37
- if (!insideEscape && !leftEscape) {
38
- ++visible;
82
+ if (!isInsideEscape && !leftEscape) {
83
+ visible++;
39
84
  }
40
85
 
41
- if (!astralRegex({exact: true}).test(x) && isFullwidthCodePoint(x.codePointAt())) {
42
- ++visible;
43
- }
86
+ if (!astralRegex.test(character) && isFullwidthCodePoint(character.codePointAt())) {
87
+ visible++;
44
88
 
45
- if (visible > begin && visible <= end) {
46
- output += x;
47
- } else if (visible === begin && !insideEscape && escapeCode !== null && escapeCode !== END_CODE) {
48
- output += wrapAnsi(escapeCode);
49
- } else if (visible >= end) {
50
- if (escapeCode !== null) {
51
- output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE);
89
+ if (typeof end !== 'number') {
90
+ stringEnd++;
52
91
  }
92
+ }
93
+
94
+ if (visible > begin && visible <= stringEnd) {
95
+ output += character;
96
+ } else if (visible === begin && !isInsideEscape && ansiCode !== undefined) {
97
+ output = checkAnsi(ansiCodes);
98
+ } else if (visible >= stringEnd) {
99
+ output += checkAnsi(ansiCodes, true, ansiCode);
53
100
  break;
54
101
  }
55
102
  }
56
103
 
57
104
  return output;
58
- };
105
+ }
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,14 @@
1
1
  {
2
2
  "name": "slice-ansi",
3
- "version": "2.0.0",
3
+ "version": "5.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",
8
+ "type": "module",
9
+ "exports": "./index.js",
7
10
  "engines": {
8
- "node": ">=6"
11
+ "node": ">=12"
9
12
  },
10
13
  "scripts": {
11
14
  "test": "xo && ava"
@@ -37,15 +40,14 @@
37
40
  "text"
38
41
  ],
39
42
  "dependencies": {
40
- "ansi-styles": "^3.2.0",
41
- "astral-regex": "^1.0.0",
42
- "is-fullwidth-code-point": "^2.0.0"
43
+ "ansi-styles": "^6.0.0",
44
+ "is-fullwidth-code-point": "^4.0.0"
43
45
  },
44
46
  "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"
47
+ "ava": "^3.15.0",
48
+ "chalk": "^4.1.0",
49
+ "random-item": "^4.0.0",
50
+ "strip-ansi": "^7.0.0",
51
+ "xo": "^0.38.2"
50
52
  }
51
53
  }
package/readme.md CHANGED
@@ -1,33 +1,30 @@
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)
1
+ # 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
 
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
16
- const chalk = require('chalk');
17
- const sliceAnsi = require('slice-ansi');
14
+ import chalk from 'chalk';
15
+ import sliceAnsi from 'slice-ansi';
18
16
 
19
- const input = 'The quick brown ' + chalk.red('fox jumped over ') +
17
+ const string = 'The quick brown ' + chalk.red('fox jumped over ') +
20
18
  'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
21
19
 
22
- console.log(sliceAnsi(input, 20, 30));
20
+ console.log(sliceAnsi(string, 20, 30));
23
21
  ```
24
22
 
25
-
26
23
  ## API
27
24
 
28
- ### sliceAnsi(input, beginSlice, [endSlice])
25
+ ### sliceAnsi(string, beginSlice, endSlice?)
29
26
 
30
- #### input
27
+ #### string
31
28
 
32
29
  Type: `string`
33
30
 
@@ -45,20 +42,25 @@ 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
- ## License
63
-
64
- MIT
56
+ ---
57
+
58
+ <div align="center">
59
+ <b>
60
+ <a href="https://tidelift.com/subscription/pkg/npm-slice_ansi?utm_source=npm-slice-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
61
+ </b>
62
+ <br>
63
+ <sub>
64
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
65
+ </sub>
66
+ </div>