slice-ansi 1.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 +73 -58
  2. package/license +1 -0
  3. package/package.json +50 -51
  4. package/readme.md +16 -14
package/index.js CHANGED
@@ -1,85 +1,100 @@
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',
6
8
  '\u009B'
7
9
  ];
8
10
 
9
- 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
-
40
11
  const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;
41
12
 
42
- module.exports = (str, begin, end) => {
43
- const arr = Array.from(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
+ }
44
22
 
45
- end = typeof end === 'number' ? end : arr.length;
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
+ output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
45
+ }
46
+ }
46
47
 
47
- let insideEscape = false;
48
- let escapeCode;
48
+ return output.join('');
49
+ };
50
+
51
+ module.exports = (string, begin, end) => {
52
+ const characters = [...string];
53
+ const ansiCodes = [];
54
+
55
+ let stringEnd = typeof end === 'number' ? end : characters.length;
56
+ let isInsideEscape = false;
57
+ let ansiCode;
49
58
  let visible = 0;
50
59
  let output = '';
51
60
 
52
- for (const item of arr.entries()) {
53
- const i = item[0];
54
- const x = item[1];
55
-
61
+ for (const [index, character] of characters.entries()) {
56
62
  let leftEscape = false;
57
63
 
58
- if (ESCAPES.indexOf(x) !== -1) {
59
- insideEscape = true;
60
- const code = /\d[^m]*/.exec(str.slice(i, i + 4));
61
- escapeCode = code === END_CODE ? null : code;
62
- } else if (insideEscape && x === 'm') {
63
- insideEscape = false;
64
+ if (ESCAPES.includes(character)) {
65
+ const code = /\d[^m]*/.exec(string.slice(index, index + 18));
66
+ ansiCode = code && code.length > 0 ? code[0] : undefined;
67
+
68
+ if (visible < stringEnd) {
69
+ isInsideEscape = true;
70
+
71
+ if (ansiCode !== undefined) {
72
+ ansiCodes.push(ansiCode);
73
+ }
74
+ }
75
+ } else if (isInsideEscape && character === 'm') {
76
+ isInsideEscape = false;
64
77
  leftEscape = true;
65
78
  }
66
79
 
67
- if (!insideEscape && !leftEscape) {
68
- ++visible;
80
+ if (!isInsideEscape && !leftEscape) {
81
+ visible++;
69
82
  }
70
83
 
71
- if (!ASTRAL_REGEX.test(x) && isFullwidthCodePoint(x.codePointAt())) {
72
- ++visible;
73
- }
84
+ if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
85
+ visible++;
74
86
 
75
- if (visible > begin && visible <= end) {
76
- output += x;
77
- } else if (visible === begin && !insideEscape && escapeCode !== undefined && escapeCode !== END_CODE) {
78
- output += wrapAnsi(escapeCode);
79
- } else if (visible >= end) {
80
- if (escapeCode !== undefined) {
81
- output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE);
87
+ if (typeof end !== 'number') {
88
+ stringEnd++;
82
89
  }
90
+ }
91
+
92
+ if (visible > begin && visible <= stringEnd) {
93
+ output += character;
94
+ } else if (visible === begin && !isInsideEscape && ansiCode !== undefined) {
95
+ output = checkAnsi(ansiCodes);
96
+ } else if (visible >= stringEnd) {
97
+ output += checkAnsi(ansiCodes, true, ansiCode);
83
98
  break;
84
99
  }
85
100
  }
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,53 +1,52 @@
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": "4.0.0",
4
+ "description": "Slice a string with ANSI escape codes",
5
+ "license": "MIT",
6
+ "repository": "chalk/slice-ansi",
7
+ "funding": "https://github.com/chalk/slice-ansi?sponsor=1",
8
+ "engines": {
9
+ "node": ">=10"
10
+ },
11
+ "scripts": {
12
+ "test": "xo && ava"
13
+ },
14
+ "files": [
15
+ "index.js"
16
+ ],
17
+ "keywords": [
18
+ "slice",
19
+ "string",
20
+ "ansi",
21
+ "styles",
22
+ "color",
23
+ "colour",
24
+ "colors",
25
+ "terminal",
26
+ "console",
27
+ "cli",
28
+ "tty",
29
+ "escape",
30
+ "formatting",
31
+ "rgb",
32
+ "256",
33
+ "shell",
34
+ "xterm",
35
+ "log",
36
+ "logging",
37
+ "command-line",
38
+ "text"
39
+ ],
40
+ "dependencies": {
41
+ "ansi-styles": "^4.0.0",
42
+ "astral-regex": "^2.0.0",
43
+ "is-fullwidth-code-point": "^3.0.0"
44
+ },
45
+ "devDependencies": {
46
+ "ava": "^2.1.0",
47
+ "chalk": "^3.0.0",
48
+ "random-item": "^3.0.0",
49
+ "strip-ansi": "^6.0.0",
50
+ "xo": "^0.26.1"
51
+ }
53
52
  }
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/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
 
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
14
  const chalk = require('chalk');
17
15
  const sliceAnsi = require('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>