@alcalzone/ansi-tokenize 0.2.1 → 0.2.3
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.
- package/LICENSE +1 -1
- package/README.md +8 -0
- package/build/ansiCodes.d.ts +4 -0
- package/build/ansiCodes.js +18 -3
- package/build/ansiCodes.js.map +1 -1
- package/build/diff.js +10 -1
- package/build/diff.js.map +1 -1
- package/build/reduce.js +3 -4
- package/build/reduce.js.map +1 -1
- package/build/tokenize.js +96 -25
- package/build/tokenize.js.map +1 -1
- package/package.json +4 -12
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -231,6 +231,14 @@ This automatically figures out the least amount of escape codes necessary to ach
|
|
|
231
231
|
Placeholder for next release:
|
|
232
232
|
### __WORK IN PROGRESS__
|
|
233
233
|
-->
|
|
234
|
+
### 0.2.3 (2026-01-02)
|
|
235
|
+
|
|
236
|
+
- Fix: Regression when rendering adjacent `dim` and `bold` styles (#43, #44)
|
|
237
|
+
|
|
238
|
+
### 0.2.2 (2025-10-22)
|
|
239
|
+
|
|
240
|
+
- Fix: Support compound SGR sequences with multiple attributes (#39, #40)
|
|
241
|
+
|
|
234
242
|
### 0.2.1 (2025-10-20)
|
|
235
243
|
|
|
236
244
|
- Fix: Prevent `dim` and `bold` modifiers from canceling each other (#37)
|
package/build/ansiCodes.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { AnsiCode } from "./tokenize.js";
|
|
2
2
|
export declare const ESCAPES: Set<number>;
|
|
3
|
+
export declare const CSI: number;
|
|
4
|
+
export declare const OSC: number;
|
|
3
5
|
export declare const endCodesSet: Set<string>;
|
|
4
6
|
export declare const linkStartCodePrefix = "\u001B]8;;";
|
|
5
7
|
export declare const linkStartCodePrefixCharCodes: number[];
|
|
@@ -9,3 +11,5 @@ export declare const linkEndCode: string;
|
|
|
9
11
|
export declare function getLinkStartCode(url: string): string;
|
|
10
12
|
export declare function getEndCode(code: string): string;
|
|
11
13
|
export declare function ansiCodesToString(codes: AnsiCode[]): string;
|
|
14
|
+
/** Check if a code is an intensity code (bold or dim) - these share endCode 22m but can coexist */
|
|
15
|
+
export declare function isIntensityCode(code: AnsiCode): boolean;
|
package/build/ansiCodes.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import ansiStyles from "ansi-styles";
|
|
2
2
|
export const ESCAPES = new Set([27, 155]); // \x1b and \x9b
|
|
3
|
+
export const CSI = "[".codePointAt(0);
|
|
4
|
+
export const OSC = "]".codePointAt(0);
|
|
3
5
|
export const endCodesSet = new Set();
|
|
4
6
|
const endCodesMap = new Map();
|
|
5
7
|
for (const [start, end] of ansiStyles.codes) {
|
|
@@ -21,12 +23,19 @@ export function getEndCode(code) {
|
|
|
21
23
|
return code;
|
|
22
24
|
if (endCodesMap.has(code))
|
|
23
25
|
return endCodesMap.get(code);
|
|
26
|
+
// We have a few special cases to handle here:
|
|
27
|
+
// Links:
|
|
24
28
|
if (code.startsWith(linkStartCodePrefix))
|
|
25
29
|
return linkEndCode;
|
|
26
30
|
code = code.slice(2);
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
// 8-bit/24-bit colors:
|
|
32
|
+
if (code.startsWith("38")) {
|
|
33
|
+
return ansiStyles.color.close;
|
|
29
34
|
}
|
|
35
|
+
else if (code.startsWith("48")) {
|
|
36
|
+
return ansiStyles.bgColor.close;
|
|
37
|
+
}
|
|
38
|
+
// Otherwise find the reset code in the ansi-styles map
|
|
30
39
|
const ret = ansiStyles.codes.get(parseInt(code, 10));
|
|
31
40
|
if (ret) {
|
|
32
41
|
return ansiStyles.color.ansi(ret);
|
|
@@ -36,6 +45,12 @@ export function getEndCode(code) {
|
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
export function ansiCodesToString(codes) {
|
|
39
|
-
|
|
48
|
+
// Deduplicate ANSI code strings before joining
|
|
49
|
+
const deduplicated = new Set(codes.map((code) => code.code));
|
|
50
|
+
return [...deduplicated].join("");
|
|
51
|
+
}
|
|
52
|
+
/** Check if a code is an intensity code (bold or dim) - these share endCode 22m but can coexist */
|
|
53
|
+
export function isIntensityCode(code) {
|
|
54
|
+
return code.code === ansiStyles.bold.open || code.code === ansiStyles.dim.open;
|
|
40
55
|
}
|
|
41
56
|
//# sourceMappingURL=ansiCodes.js.map
|
package/build/ansiCodes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ansiCodes.js","sourceRoot":"","sources":["../src/ansiCodes.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB;
|
|
1
|
+
{"version":3,"file":"ansiCodes.js","sourceRoot":"","sources":["../src/ansiCodes.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB;AAC3D,MAAM,CAAC,MAAM,GAAG,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE,CAAC;AACvC,MAAM,CAAC,MAAM,GAAG,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE,CAAC;AAEvC,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;AAC7C,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;AAC9C,KAAK,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,UAAU,CAAC,KAAK,EAAE;IAC5C,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EA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AAG,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;IACrD,IAAI,GAAG,EAAE;QACR,OAAO,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KAClC;SAAM;QACN,OAAO,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC;KAC7B;AACF,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAiB;IAClD,+CAA+C;IAC/C,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7D,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,eAAe,CAAC,IAAc;IAC7C,OAAO,IAAI,CAAC,IAAI,KAAK,UAAU,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC;AAChF,CAAC"}
|
package/build/diff.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isIntensityCode } from "./ansiCodes.js";
|
|
1
2
|
import { undoAnsiCodes } from "./undo.js";
|
|
2
3
|
/**
|
|
3
4
|
* Returns the minimum amount of ANSI codes necessary to get from the compound style `from` to `to`.
|
|
@@ -5,11 +6,19 @@ import { undoAnsiCodes } from "./undo.js";
|
|
|
5
6
|
*/
|
|
6
7
|
export function diffAnsiCodes(from, to) {
|
|
7
8
|
const endCodesInTo = new Set(to.map((code) => code.endCode));
|
|
9
|
+
const startCodesInTo = new Set(to.map((code) => code.code));
|
|
8
10
|
const startCodesInFrom = new Set(from.map((code) => code.code));
|
|
9
11
|
return [
|
|
10
12
|
// Ignore all styles in `from` that are not overwritten or removed by `to`
|
|
11
13
|
// Disable all styles in `from` that are removed in `to`
|
|
12
|
-
...undoAnsiCodes(from.filter((code) =>
|
|
14
|
+
...undoAnsiCodes(from.filter((code) => {
|
|
15
|
+
// Special case: Intensity codes (1m, 2m) can coexist (both end with 22m).
|
|
16
|
+
// We have to check the start codes for those, otherwise we might miss a reset.
|
|
17
|
+
if (isIntensityCode(code)) {
|
|
18
|
+
return !startCodesInTo.has(code.code);
|
|
19
|
+
}
|
|
20
|
+
return !endCodesInTo.has(code.endCode);
|
|
21
|
+
})),
|
|
13
22
|
// Add all styles in `to` that don't exist in `from`
|
|
14
23
|
...to.filter((code) => !startCodesInFrom.has(code.code)),
|
|
15
24
|
];
|
package/build/diff.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,IAAgB,EAAE,EAAc;IAC7D,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7D,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5D,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAEhE,OAAO;QACN,0EAA0E;QAC1E,wDAAwD;QACxD,GAAG,aAAa,CACf,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YACpB,0EAA0E;YAC1E,+EAA+E;YAC/E,IAAI,eAAe,CAAC,IAAI,CAAC,EAAE;gBAC1B,OAAO,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACtC;YACD,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxC,CAAC,CAAC,CACF;QACD,oDAAoD;QACpD,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KACxD,CAAC;AACH,CAAC"}
|
package/build/reduce.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ansiStyles from "ansi-styles";
|
|
2
|
-
import { endCodesSet } from "./ansiCodes.js";
|
|
2
|
+
import { endCodesSet, isIntensityCode } from "./ansiCodes.js";
|
|
3
3
|
/** Reduces the given array of ANSI codes to the minimum necessary to render with the same style */
|
|
4
4
|
export function reduceAnsiCodes(codes) {
|
|
5
5
|
return reduceAnsiCodesIncremental([], codes);
|
|
@@ -20,9 +20,8 @@ export function reduceAnsiCodesIncremental(codes, newCodes) {
|
|
|
20
20
|
// This is a start code. Remove codes it "overrides", then add it.
|
|
21
21
|
// If a new code has the same endCode, it "overrides" existing ones.
|
|
22
22
|
// Special case: Intensity codes (1m, 2m) can coexist (both end with 22m).
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (isIntensityCode) {
|
|
23
|
+
// We only add those if the exact same code is not already present.
|
|
24
|
+
if (isIntensityCode(code)) {
|
|
26
25
|
if (!ret.find((retCode) => retCode.code === code.code && retCode.endCode === code.endCode)) {
|
|
27
26
|
ret.push(code);
|
|
28
27
|
}
|
package/build/reduce.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reduce.js","sourceRoot":"","sources":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C,CAAC;iBACf;aACD;iBAAM;gBACN,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,KAAK,IAAI,CAAC,OAAO,CAAC,CAAC;gBAChE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACf;SACD;KACD;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
package/build/tokenize.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import isFullwidthCodePoint from "is-fullwidth-code-point";
|
|
2
|
-
import { ESCAPES, getEndCode, linkStartCodePrefix, linkStartCodePrefixCharCodes, } from "./ansiCodes.js";
|
|
3
|
-
|
|
4
|
-
for (let index = 0; index < str.length; index++) {
|
|
5
|
-
const charCode = str.charCodeAt(index);
|
|
6
|
-
if (charCode >= 48 && charCode <= 57) {
|
|
7
|
-
return index;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return -1;
|
|
11
|
-
}
|
|
2
|
+
import { CSI, ESCAPES, getEndCode, linkStartCodePrefix, linkStartCodePrefixCharCodes, OSC, } from "./ansiCodes.js";
|
|
3
|
+
// HOT PATH: Use only basic string/char code operations for maximum performance
|
|
12
4
|
function parseLinkCode(string, offset) {
|
|
13
5
|
string = string.slice(offset);
|
|
14
6
|
for (let index = 1; index < linkStartCodePrefixCharCodes.length; index++) {
|
|
@@ -22,16 +14,73 @@ function parseLinkCode(string, offset) {
|
|
|
22
14
|
return undefined;
|
|
23
15
|
return string.slice(0, endIndex + 1);
|
|
24
16
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
17
|
+
const CC_0 = "0".charCodeAt(0);
|
|
18
|
+
const CC_9 = "9".charCodeAt(0);
|
|
19
|
+
const CC_SEMI = ";".charCodeAt(0);
|
|
20
|
+
const CC_M = "m".charCodeAt(0);
|
|
21
|
+
/**
|
|
22
|
+
* Scans through the given string and finds the index of the last character of an SGR sequence
|
|
23
|
+
* like `\x1B[38;2;123;123;123m`. This assumes that the string has been checked to start with `\x1B[`.
|
|
24
|
+
* Returns -1 if no valid SGR sequence is found.
|
|
25
|
+
*/
|
|
26
|
+
function findSGRSequenceEndIndex(str) {
|
|
27
|
+
for (let index = 2; index < str.length; index++) {
|
|
28
|
+
const charCode = str.charCodeAt(index);
|
|
29
|
+
// m marks the end of the SGR sequence
|
|
30
|
+
if (charCode === CC_M)
|
|
31
|
+
return index;
|
|
32
|
+
// Digits and semicolons are valid
|
|
33
|
+
if (charCode === CC_SEMI)
|
|
34
|
+
continue;
|
|
35
|
+
if (charCode >= CC_0 && charCode <= CC_9)
|
|
36
|
+
continue;
|
|
37
|
+
// Everything else is invalid
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
return -1;
|
|
41
|
+
}
|
|
42
|
+
// HOT PATH: Use only basic string/char code operations for maximum performance
|
|
43
|
+
function parseSGRSequence(string, offset) {
|
|
44
|
+
string = string.slice(offset);
|
|
45
|
+
const endIndex = findSGRSequenceEndIndex(string);
|
|
46
|
+
if (endIndex === -1)
|
|
47
|
+
return;
|
|
48
|
+
return string.slice(0, endIndex + 1);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Splits compound SGR sequences like `\x1B[1;3;31m` into individual components
|
|
52
|
+
*/
|
|
53
|
+
function splitCompoundSGRSequences(code) {
|
|
54
|
+
if (!code.includes(";")) {
|
|
55
|
+
// Not a compound code
|
|
56
|
+
return [code];
|
|
57
|
+
}
|
|
58
|
+
const codeParts = code
|
|
59
|
+
// Strip off the escape sequences \x1B[ and m
|
|
60
|
+
.slice(2, -1)
|
|
61
|
+
.split(";");
|
|
62
|
+
const ret = [];
|
|
63
|
+
for (let i = 0; i < codeParts.length; i++) {
|
|
64
|
+
const rawCode = codeParts[i];
|
|
65
|
+
// Keep 8-bit and 24-bit color codes (containing multiple ";") together
|
|
66
|
+
if (rawCode === "38" || rawCode === "48") {
|
|
67
|
+
if (i + 2 < codeParts.length && codeParts[i + 1] === "5") {
|
|
68
|
+
// 8-bit color, followed by another number
|
|
69
|
+
ret.push(codeParts.slice(i, i + 3).join(";"));
|
|
70
|
+
i += 2;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
else if (i + 4 < codeParts.length && codeParts[i + 1] === "2") {
|
|
74
|
+
// 24-bit color, followed by three numbers
|
|
75
|
+
ret.push(codeParts.slice(i, i + 5).join(";"));
|
|
76
|
+
i += 4;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
32
79
|
}
|
|
33
|
-
|
|
80
|
+
// Not a (valid) 8/24-bit color code, push as is
|
|
81
|
+
ret.push(rawCode);
|
|
34
82
|
}
|
|
83
|
+
return ret.map((part) => `\x1b[${part}m`);
|
|
35
84
|
}
|
|
36
85
|
export function tokenize(str, endChar = Number.POSITIVE_INFINITY) {
|
|
37
86
|
const ret = [];
|
|
@@ -40,14 +89,36 @@ export function tokenize(str, endChar = Number.POSITIVE_INFINITY) {
|
|
|
40
89
|
while (index < str.length) {
|
|
41
90
|
const codePoint = str.codePointAt(index);
|
|
42
91
|
if (ESCAPES.has(codePoint)) {
|
|
43
|
-
|
|
44
|
-
|
|
92
|
+
let code;
|
|
93
|
+
// Peek the next code point to determine the type of ANSI sequence
|
|
94
|
+
const nextCodePoint = str.codePointAt(index + 1);
|
|
95
|
+
if (nextCodePoint === OSC) {
|
|
96
|
+
// ] = operating system commands, like links
|
|
97
|
+
code = parseLinkCode(str, index);
|
|
98
|
+
if (code) {
|
|
99
|
+
ret.push({
|
|
100
|
+
type: "ansi",
|
|
101
|
+
code: code,
|
|
102
|
+
endCode: getEndCode(code),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (nextCodePoint === CSI) {
|
|
107
|
+
// [ = control sequence introducer, like SGR sequences [...m
|
|
108
|
+
code = parseSGRSequence(str, index);
|
|
109
|
+
if (code) {
|
|
110
|
+
// Split compound codes into individual tokens
|
|
111
|
+
const codes = splitCompoundSGRSequences(code);
|
|
112
|
+
for (const individualCode of codes) {
|
|
113
|
+
ret.push({
|
|
114
|
+
type: "ansi",
|
|
115
|
+
code: individualCode,
|
|
116
|
+
endCode: getEndCode(individualCode),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
45
121
|
if (code) {
|
|
46
|
-
ret.push({
|
|
47
|
-
type: "ansi",
|
|
48
|
-
code,
|
|
49
|
-
endCode: getEndCode(code),
|
|
50
|
-
});
|
|
51
122
|
index += code.length;
|
|
52
123
|
continue;
|
|
53
124
|
}
|
package/build/tokenize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenize.js","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EACN,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,4BAA4B,
|
|
1
|
+
{"version":3,"file":"tokenize.js","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EACN,GAAG,EACH,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,4BAA4B,EAC5B,GAAG,GACH,MAAM,gBAAgB,CAAC;AAgBxB,+EAA+E;AAC/E,SAAS,aAAa,CAAC,MAAc,EAAE,MAAc;IACpD,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,4BAA4B,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACzE,IAAI,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,4BAA4B,CAAC,KAAK,CAAC,EAAE;YACrE,OAAO,SAAS,CAAC;SACjB;KACD;IACD,0EAA0E;IAC1E,MAAM,QAAQ,GAAG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,IAAI,CAAC;4BACR,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,cAAc;4BACpB,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC;yBACnC,CAAC,CAAC;qBACH;iBACD;aACD;YAED,IAAI,IAAI,EAAE;gBACT,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC;gBACrB,SAAS;aACT;SACD;QAED,MAAM,SAAS,GAAG,oBAAoB,CAAC,SAAS,CAAC,CAAC;QAClD,MAAM,SAAS,GAAG,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QAElD,GAAG,CAAC,IAAI,CAAC;YACR,IAAI,EAAE,MAAM;YACZ,KAAK,EAAE,SAAS;YAChB,SAAS;SACT,CAAC,CAAC;QAEH,KAAK,IAAI,SAAS,CAAC,MAAM,CAAC;QAC1B,OAAO,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC;QAE5C,IAAI,OAAO,IAAI,OAAO,EAAE;YACvB,MAAM;SACN;KACD;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alcalzone/ansi-tokenize",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Efficiently modify strings containing ANSI escape codes",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -33,15 +33,12 @@
|
|
|
33
33
|
"@types/node": "^18.19.130",
|
|
34
34
|
"@typescript-eslint/eslint-plugin": "^5.55.0",
|
|
35
35
|
"@typescript-eslint/parser": "^5.55.0",
|
|
36
|
-
"ava": "^4.3.3",
|
|
37
36
|
"eslint": "^8.36.0",
|
|
38
37
|
"eslint-config-prettier": "^8.7.0",
|
|
39
38
|
"eslint-plugin-prettier": "^4.2.1",
|
|
40
39
|
"prettier": "^2.8.4",
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"tsx": "^3.12.8",
|
|
44
|
-
"typescript": "^5.0.2"
|
|
40
|
+
"typescript": "^5.0.2",
|
|
41
|
+
"vitest": "^3.2.4"
|
|
45
42
|
},
|
|
46
43
|
"dependencies": {
|
|
47
44
|
"ansi-styles": "^6.2.1",
|
|
@@ -50,14 +47,9 @@
|
|
|
50
47
|
"scripts": {
|
|
51
48
|
"prepare": "tsc -p tsconfig.build.json",
|
|
52
49
|
"build": "tsc -p tsconfig.build.json",
|
|
53
|
-
"test": "
|
|
50
|
+
"test": "vitest run",
|
|
54
51
|
"lint": "eslint .",
|
|
55
52
|
"release": "release-script"
|
|
56
53
|
},
|
|
57
|
-
"ava": {
|
|
58
|
-
"extensions": {
|
|
59
|
-
"ts": "module"
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
54
|
"packageManager": "yarn@4.10.3"
|
|
63
55
|
}
|