@visulima/string 1.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.
- package/CHANGELOG.md +5 -0
- package/LICENSE.md +77 -0
- package/README.md +880 -0
- package/dist/case/camel-case.cjs +59 -0
- package/dist/case/camel-case.d.cts +7 -0
- package/dist/case/camel-case.d.mts +7 -0
- package/dist/case/camel-case.d.ts +7 -0
- package/dist/case/camel-case.mjs +57 -0
- package/dist/case/capital-case.cjs +51 -0
- package/dist/case/capital-case.d.cts +7 -0
- package/dist/case/capital-case.d.mts +7 -0
- package/dist/case/capital-case.d.ts +7 -0
- package/dist/case/capital-case.mjs +49 -0
- package/dist/case/constant-case.cjs +11 -0
- package/dist/case/constant-case.d.cts +7 -0
- package/dist/case/constant-case.d.mts +7 -0
- package/dist/case/constant-case.d.ts +7 -0
- package/dist/case/constant-case.mjs +9 -0
- package/dist/case/dot-case.cjs +11 -0
- package/dist/case/dot-case.d.cts +7 -0
- package/dist/case/dot-case.d.mts +7 -0
- package/dist/case/dot-case.d.ts +7 -0
- package/dist/case/dot-case.mjs +9 -0
- package/dist/case/flat-case.cjs +11 -0
- package/dist/case/flat-case.d.cts +7 -0
- package/dist/case/flat-case.d.mts +7 -0
- package/dist/case/flat-case.d.ts +7 -0
- package/dist/case/flat-case.mjs +9 -0
- package/dist/case/flip-case.cjs +61 -0
- package/dist/case/flip-case.d.cts +8 -0
- package/dist/case/flip-case.d.mts +8 -0
- package/dist/case/flip-case.d.ts +8 -0
- package/dist/case/flip-case.mjs +57 -0
- package/dist/case/identify-case.cjs +30 -0
- package/dist/case/identify-case.d.cts +7 -0
- package/dist/case/identify-case.d.mts +7 -0
- package/dist/case/identify-case.d.ts +7 -0
- package/dist/case/identify-case.mjs +28 -0
- package/dist/case/index.cjs +47 -0
- package/dist/case/index.d.cts +26 -0
- package/dist/case/index.d.mts +26 -0
- package/dist/case/index.d.ts +26 -0
- package/dist/case/index.mjs +20 -0
- package/dist/case/kebab-case.cjs +61 -0
- package/dist/case/kebab-case.d.cts +11 -0
- package/dist/case/kebab-case.d.mts +11 -0
- package/dist/case/kebab-case.d.ts +11 -0
- package/dist/case/kebab-case.mjs +57 -0
- package/dist/case/lower-first.cjs +13 -0
- package/dist/case/lower-first.d.cts +7 -0
- package/dist/case/lower-first.d.mts +7 -0
- package/dist/case/lower-first.d.ts +7 -0
- package/dist/case/lower-first.mjs +11 -0
- package/dist/case/no-case.cjs +11 -0
- package/dist/case/no-case.d.cts +7 -0
- package/dist/case/no-case.d.mts +7 -0
- package/dist/case/no-case.d.ts +7 -0
- package/dist/case/no-case.mjs +9 -0
- package/dist/case/pascal-case.cjs +52 -0
- package/dist/case/pascal-case.d.cts +7 -0
- package/dist/case/pascal-case.d.mts +7 -0
- package/dist/case/pascal-case.d.ts +7 -0
- package/dist/case/pascal-case.mjs +50 -0
- package/dist/case/pascal-snake-case.cjs +32 -0
- package/dist/case/pascal-snake-case.d.cts +7 -0
- package/dist/case/pascal-snake-case.d.mts +7 -0
- package/dist/case/pascal-snake-case.d.ts +7 -0
- package/dist/case/pascal-snake-case.mjs +30 -0
- package/dist/case/path-case.cjs +11 -0
- package/dist/case/path-case.d.cts +7 -0
- package/dist/case/path-case.d.mts +7 -0
- package/dist/case/path-case.d.ts +7 -0
- package/dist/case/path-case.mjs +9 -0
- package/dist/case/sentence-case.cjs +58 -0
- package/dist/case/sentence-case.d.cts +7 -0
- package/dist/case/sentence-case.d.mts +7 -0
- package/dist/case/sentence-case.d.ts +7 -0
- package/dist/case/sentence-case.mjs +56 -0
- package/dist/case/snake-case.cjs +11 -0
- package/dist/case/snake-case.d.cts +7 -0
- package/dist/case/snake-case.d.mts +7 -0
- package/dist/case/snake-case.d.ts +7 -0
- package/dist/case/snake-case.mjs +9 -0
- package/dist/case/split-by-case.cjs +638 -0
- package/dist/case/split-by-case.d.cts +16 -0
- package/dist/case/split-by-case.d.mts +16 -0
- package/dist/case/split-by-case.d.ts +16 -0
- package/dist/case/split-by-case.mjs +634 -0
- package/dist/case/title-case.cjs +47 -0
- package/dist/case/title-case.d.cts +7 -0
- package/dist/case/title-case.d.mts +7 -0
- package/dist/case/title-case.d.ts +7 -0
- package/dist/case/title-case.mjs +45 -0
- package/dist/case/train-case.cjs +44 -0
- package/dist/case/train-case.d.cts +7 -0
- package/dist/case/train-case.d.mts +7 -0
- package/dist/case/train-case.d.ts +7 -0
- package/dist/case/train-case.mjs +42 -0
- package/dist/case/upper-first.cjs +13 -0
- package/dist/case/upper-first.d.cts +7 -0
- package/dist/case/upper-first.d.mts +7 -0
- package/dist/case/upper-first.d.ts +7 -0
- package/dist/case/upper-first.mjs +11 -0
- package/dist/get-string-truncated-width.cjs +386 -0
- package/dist/get-string-truncated-width.d.cts +25 -0
- package/dist/get-string-truncated-width.d.mts +25 -0
- package/dist/get-string-truncated-width.d.ts +25 -0
- package/dist/get-string-truncated-width.mjs +382 -0
- package/dist/get-string-width.cjs +11 -0
- package/dist/get-string-width.d.cts +6 -0
- package/dist/get-string-width.d.mts +6 -0
- package/dist/get-string-width.d.ts +6 -0
- package/dist/get-string-width.mjs +7 -0
- package/dist/index.cjs +60 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.mts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.mjs +26 -0
- package/dist/native-string-types.d.cts +128 -0
- package/dist/native-string-types.d.mts +128 -0
- package/dist/native-string-types.d.ts +128 -0
- package/dist/outdent.cjs +122 -0
- package/dist/outdent.d.cts +15 -0
- package/dist/outdent.d.mts +15 -0
- package/dist/outdent.d.ts +15 -0
- package/dist/outdent.mjs +118 -0
- package/dist/packem_shared/LRUCache-Crb-m7cw.cjs +53 -0
- package/dist/packem_shared/LRUCache-udNErhWw.mjs +51 -0
- package/dist/packem_shared/WrapMode-BgOOthJV.mjs +545 -0
- package/dist/packem_shared/WrapMode-ZqTeXK8X.cjs +550 -0
- package/dist/packem_shared/constants-BnVGLrDr.mjs +99 -0
- package/dist/packem_shared/constants-HevSYUjU.cjs +149 -0
- package/dist/packem_shared/generate-cache-key-D8f1auDB.cjs +10 -0
- package/dist/packem_shared/generate-cache-key-YSju5pj2.mjs +8 -0
- package/dist/packem_shared/joinSegments-BH1CT4dK.cjs +48 -0
- package/dist/packem_shared/joinSegments-BHY8PPnk.mjs +46 -0
- package/dist/packem_shared/normalize-german-eszett-DJK0S2qj.cjs +11 -0
- package/dist/packem_shared/normalize-german-eszett-fpJ4lZL6.mjs +9 -0
- package/dist/packem_shared/types-BAd3WDHo.d.mts +59 -0
- package/dist/packem_shared/types-BNbQJg02.d.cts +41 -0
- package/dist/packem_shared/types-BNbQJg02.d.mts +41 -0
- package/dist/packem_shared/types-BNbQJg02.d.ts +41 -0
- package/dist/packem_shared/types-BttWSSIk.d.ts +59 -0
- package/dist/packem_shared/types-D2-6p7t1.d.cts +59 -0
- package/dist/slice.cjs +413 -0
- package/dist/slice.d.cts +10 -0
- package/dist/slice.d.mts +10 -0
- package/dist/slice.d.ts +10 -0
- package/dist/slice.mjs +409 -0
- package/dist/test/utils.cjs +77 -0
- package/dist/test/utils.d.cts +30 -0
- package/dist/test/utils.d.mts +30 -0
- package/dist/test/utils.d.ts +30 -0
- package/dist/test/utils.mjs +71 -0
- package/dist/test/vitest.cjs +11 -0
- package/dist/test/vitest.d.cts +8 -0
- package/dist/test/vitest.d.mts +8 -0
- package/dist/test/vitest.d.ts +8 -0
- package/dist/test/vitest.mjs +7 -0
- package/dist/truncate.cjs +90 -0
- package/dist/truncate.d.cts +12 -0
- package/dist/truncate.d.mts +12 -0
- package/dist/truncate.d.ts +12 -0
- package/dist/truncate.mjs +86 -0
- package/dist/utils.cjs +9 -0
- package/dist/utils.d.cts +14 -0
- package/dist/utils.d.mts +14 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.mjs +1 -0
- package/package.json +468 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { N as NodeLocale, m as IsStringLiteral } from './types-BNbQJg02.cjs';
|
|
2
|
+
import { LRUCache } from '../utils.cjs';
|
|
3
|
+
|
|
4
|
+
type SplitterCharacter = " " | "_" | "-" | "." | "/";
|
|
5
|
+
type LastOfArray<T extends any[]> = T extends [...any, infer R] ? R : never;
|
|
6
|
+
type RemoveLastOfArray<T extends any[]> = T extends [...infer F, any] ? F : never;
|
|
7
|
+
type IsUpperCase<S extends string> = S extends Uppercase<S> ? true : false;
|
|
8
|
+
type IsLowerCase<S extends string> = S extends Lowercase<S> ? true : false;
|
|
9
|
+
type SameLetterCase<X extends string, Y extends string> = IsUpperCase<X> extends IsUpperCase<Y> ? true : IsLowerCase<X> extends IsLowerCase<Y> ? true : false;
|
|
10
|
+
type FirstOfString<S extends string> = S extends `${infer F}${string}` ? F : never;
|
|
11
|
+
type RemoveFirstOfString<S extends string> = S extends `${string}${infer R}` ? R : never;
|
|
12
|
+
interface LocaleOptions {
|
|
13
|
+
locale?: NodeLocale;
|
|
14
|
+
}
|
|
15
|
+
interface CaseOptions extends LocaleOptions {
|
|
16
|
+
cache?: boolean;
|
|
17
|
+
cacheStore?: LRUCache<string, string>;
|
|
18
|
+
handleAnsi?: boolean;
|
|
19
|
+
handleEmoji?: boolean;
|
|
20
|
+
knownAcronyms?: ReadonlyArray<string>;
|
|
21
|
+
normalize?: boolean;
|
|
22
|
+
stripAnsi?: boolean;
|
|
23
|
+
stripEmoji?: boolean;
|
|
24
|
+
}
|
|
25
|
+
type CamelCase<T extends string> = IsStringLiteral<T> extends true ? T extends `${infer F}_${infer R}` ? `${Lowercase<F>}${Capitalize<CamelCase<R>>}` : T extends `${infer F}-${infer R}` ? `${Lowercase<F>}${Capitalize<CamelCase<R>>}` : T extends `${infer F} ${infer R}` ? `${Lowercase<F>}${Capitalize<CamelCase<R>>}` : Lowercase<T> : string;
|
|
26
|
+
type PascalCase<T extends string> = IsStringLiteral<T> extends true ? Capitalize<CamelCase<T>> : string;
|
|
27
|
+
type SnakeCase<T extends string> = IsStringLiteral<T> extends true ? T extends `${infer C}${infer Rest}` ? C extends Uppercase<C> ? `_${Lowercase<C>}${SnakeCase<Rest>}` : `${C}${SnakeCase<Rest>}` : T : string;
|
|
28
|
+
type KebabCase<T extends string> = IsStringLiteral<T> extends true ? T extends `${infer C}${infer Rest}` ? C extends Uppercase<C> ? `-${Lowercase<C>}${KebabCase<Rest>}` : `${C}${KebabCase<Rest>}` : T : string;
|
|
29
|
+
type FlatCase<T extends string> = IsStringLiteral<T> extends true ? Lowercase<T extends `${infer F}${infer R}` ? `${F}${FlatCase<R>}` : T> : string;
|
|
30
|
+
type TrainCase<T extends string, N extends boolean = false> = IsStringLiteral<T> extends true ? T extends `${infer F}${infer R}` ? F extends Uppercase<F> ? R extends `${infer Next}${infer Rest}` ? Next extends Uppercase<Next> ? N extends true ? `${Capitalize<F>}-${Lowercase<Next>}${TrainCase<Rest, N>}` : `${F}${Next}${TrainCase<Rest, N>}` : `${Capitalize<F>}${TrainCase<R, N>}` : `${Capitalize<F>}${Lowercase<R>}` : `${Lowercase<F>}${TrainCase<R, N>}` : T : string;
|
|
31
|
+
type TitleCase<T extends string> = IsStringLiteral<T> extends true ? (T extends `${infer F}${infer R}` ? `${Capitalize<F>}${Lowercase<R>}` : T) : string;
|
|
32
|
+
type UpperFirst<T extends string> = IsStringLiteral<T> extends true ? (T extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : T) : string;
|
|
33
|
+
type LowerFirst<T extends string> = IsStringLiteral<T> extends true ? (T extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : T) : string;
|
|
34
|
+
type FlipCase<T extends string> = IsStringLiteral<T> extends true ? T extends `${infer C}${infer Rest}` ? `${C extends Uppercase<C> ? Lowercase<C> : Uppercase<C>}${FlipCase<Rest>}` : T : string;
|
|
35
|
+
type DotCase<T extends string> = T & {
|
|
36
|
+
__dot_case__: never;
|
|
37
|
+
};
|
|
38
|
+
type PathCase<T extends string> = T & {
|
|
39
|
+
__path_case__: never;
|
|
40
|
+
};
|
|
41
|
+
type NoCase<T extends string> = T & {
|
|
42
|
+
__no_case__: never;
|
|
43
|
+
};
|
|
44
|
+
type ConstantCase<T extends string> = T & {
|
|
45
|
+
__constant_case__: never;
|
|
46
|
+
};
|
|
47
|
+
type CapitalCase<T extends string> = T & {
|
|
48
|
+
__capital_case__: never;
|
|
49
|
+
};
|
|
50
|
+
type SentenceCase<T extends string> = T & {
|
|
51
|
+
__sentence_case__: never;
|
|
52
|
+
};
|
|
53
|
+
type PascalSnakeCase<T extends string> = T & {
|
|
54
|
+
__pascal_snake_case__: never;
|
|
55
|
+
};
|
|
56
|
+
type IdentifyCase<T extends string> = IsStringLiteral<T> extends true ? T extends Lowercase<T> ? T extends `${string}_${string}` ? "snake" : T extends `${string}-${string}` ? "kebab" : T extends `${string}${string}` ? "flat" : "lower" : T extends Uppercase<T> ? "upper" : T extends Capitalize<string> ? T extends `${Uppercase<string>}${string}` ? T extends `${string}-${string}` ? "train" : "pascal" : T extends `${Lowercase<string>}${Capitalize<string>}` ? "camel" : T extends `${Capitalize<string>} ${Lowercase<string>}` ? "title" : "mixed" : "mixed" : string;
|
|
57
|
+
type SplitByCase<T, Separator extends string = SplitterCharacter, Accumulator extends unknown[] = []> = string extends Separator ? string[] : T extends `${infer F}${infer R}` ? [LastOfArray<Accumulator>] extends [never] ? SplitByCase<R, Separator, [F]> : LastOfArray<Accumulator> extends string ? R extends "" ? SplitByCase<R, Separator, [...RemoveLastOfArray<Accumulator>, `${LastOfArray<Accumulator>}${F}`]> : SameLetterCase<F, FirstOfString<R>> extends true ? F extends Separator ? FirstOfString<R> extends Separator ? SplitByCase<R, Separator, [...Accumulator, ""]> : IsUpperCase<FirstOfString<R>> extends true ? SplitByCase<RemoveFirstOfString<R>, Separator, [...Accumulator, FirstOfString<R>]> : SplitByCase<R, Separator, [...Accumulator, ""]> : SplitByCase<R, Separator, [...RemoveLastOfArray<Accumulator>, `${LastOfArray<Accumulator>}${F}`]> : IsLowerCase<F> extends true ? SplitByCase<RemoveFirstOfString<R>, Separator, [...RemoveLastOfArray<Accumulator>, `${LastOfArray<Accumulator>}${F}`, FirstOfString<R>]> : SplitByCase<R, Separator, [...Accumulator, F]> : never : Accumulator extends [] ? T extends "" ? [] : string[] : Accumulator;
|
|
58
|
+
|
|
59
|
+
export type { CamelCase as C, DotCase as D, FlatCase as F, IdentifyCase as I, KebabCase as K, LocaleOptions as L, NoCase as N, PascalCase as P, SentenceCase as S, TitleCase as T, UpperFirst as U, CapitalCase as a, CaseOptions as b, ConstantCase as c, FlipCase as d, LowerFirst as e, PascalSnakeCase as f, PathCase as g, SnakeCase as h, SplitByCase as i, TrainCase as j };
|
package/dist/slice.cjs
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
+
|
|
5
|
+
const constants = require('./packem_shared/constants-HevSYUjU.cjs');
|
|
6
|
+
const getStringWidth = require('./get-string-width.cjs');
|
|
7
|
+
const LRUCache = require('./packem_shared/LRUCache-Crb-m7cw.cjs');
|
|
8
|
+
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
+
const segmentCache = new LRUCache(100);
|
|
12
|
+
const defaultSegmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
|
|
13
|
+
const ANSI_RESET_CODES = /* @__PURE__ */ new Set([
|
|
14
|
+
"0",
|
|
15
|
+
// Full reset
|
|
16
|
+
"22",
|
|
17
|
+
// Bold reset
|
|
18
|
+
"23",
|
|
19
|
+
// Italic reset
|
|
20
|
+
"24",
|
|
21
|
+
// Underline reset
|
|
22
|
+
"27",
|
|
23
|
+
// Inverse reset
|
|
24
|
+
"28",
|
|
25
|
+
// Hidden reset
|
|
26
|
+
"29",
|
|
27
|
+
// Strikethrough reset
|
|
28
|
+
"39",
|
|
29
|
+
// Foreground color reset
|
|
30
|
+
"49"
|
|
31
|
+
// Background color reset
|
|
32
|
+
]);
|
|
33
|
+
const STYLE_CODES = /* @__PURE__ */ new Set([
|
|
34
|
+
"1",
|
|
35
|
+
// Bold
|
|
36
|
+
"3",
|
|
37
|
+
// Italic
|
|
38
|
+
"4",
|
|
39
|
+
// Underline
|
|
40
|
+
"7",
|
|
41
|
+
// Inverse
|
|
42
|
+
"8",
|
|
43
|
+
// Hidden
|
|
44
|
+
"9"
|
|
45
|
+
// Strikethrough
|
|
46
|
+
]);
|
|
47
|
+
const FORMAT_RESET_CODES = /* @__PURE__ */ new Map([
|
|
48
|
+
["22", "1"],
|
|
49
|
+
// Bold reset
|
|
50
|
+
["23", "3"],
|
|
51
|
+
// Italic reset
|
|
52
|
+
["24", "4"],
|
|
53
|
+
// Underline reset
|
|
54
|
+
["27", "7"],
|
|
55
|
+
// Inverse reset
|
|
56
|
+
["28", "8"],
|
|
57
|
+
// Hidden reset
|
|
58
|
+
["29", "9"]
|
|
59
|
+
// Strikethrough reset
|
|
60
|
+
]);
|
|
61
|
+
const isForegroundColor = /* @__PURE__ */ __name((code) => code >= "30" && code <= "37" || code >= "90" && code <= "97" || code.startsWith("38;"), "isForegroundColor");
|
|
62
|
+
const isBackgroundColor = /* @__PURE__ */ __name((code) => code >= "40" && code <= "47" || code >= "100" && code <= "107" || code.startsWith("48;"), "isBackgroundColor");
|
|
63
|
+
const FORMAT_STYLES = [
|
|
64
|
+
{ close: "\x1B[22m", open: "\x1B[1m" },
|
|
65
|
+
// Bold
|
|
66
|
+
{ close: "\x1B[23m", open: "\x1B[3m" },
|
|
67
|
+
// Italic
|
|
68
|
+
{ close: "\x1B[24m", open: "\x1B[4m" },
|
|
69
|
+
// Underline
|
|
70
|
+
{ close: "\x1B[27m", open: "\x1B[7m" },
|
|
71
|
+
// Inverse
|
|
72
|
+
{ close: "\x1B[28m", open: "\x1B[8m" },
|
|
73
|
+
// Hidden
|
|
74
|
+
{ close: "\x1B[29m", open: "\x1B[9m" }
|
|
75
|
+
// Strikethrough
|
|
76
|
+
];
|
|
77
|
+
const findFirstPositionOfAny = /* @__PURE__ */ __name((input, substrings) => {
|
|
78
|
+
let firstPos = -1;
|
|
79
|
+
for (const substring of substrings) {
|
|
80
|
+
const pos = input.indexOf(substring);
|
|
81
|
+
if (pos >= 0 && (firstPos === -1 || pos < firstPos)) {
|
|
82
|
+
firstPos = pos;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return firstPos;
|
|
86
|
+
}, "findFirstPositionOfAny");
|
|
87
|
+
const processIntoStyledSegments = /* @__PURE__ */ __name((input, options) => {
|
|
88
|
+
if (!input.includes("\x1B")) {
|
|
89
|
+
return [
|
|
90
|
+
{
|
|
91
|
+
after: "",
|
|
92
|
+
before: "",
|
|
93
|
+
content: input,
|
|
94
|
+
visibleLength: getStringWidth.getStringWidth(input, options.width)
|
|
95
|
+
}
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
const cachedResult = segmentCache.get(input);
|
|
99
|
+
if (cachedResult) {
|
|
100
|
+
return cachedResult;
|
|
101
|
+
}
|
|
102
|
+
const parts = [];
|
|
103
|
+
const matches = [];
|
|
104
|
+
let lastIndex = 0;
|
|
105
|
+
let match;
|
|
106
|
+
constants.RE_ANSI.lastIndex = 0;
|
|
107
|
+
while ((match = constants.RE_ANSI.exec(input)) !== null) {
|
|
108
|
+
const matchedText = match[0];
|
|
109
|
+
parts.push(input.slice(lastIndex, match.index));
|
|
110
|
+
matches.push(matchedText);
|
|
111
|
+
lastIndex = match.index + matchedText.length;
|
|
112
|
+
}
|
|
113
|
+
if (lastIndex < input.length) {
|
|
114
|
+
parts.push(input.slice(lastIndex));
|
|
115
|
+
}
|
|
116
|
+
const closingSequences = [];
|
|
117
|
+
for (const ansi of matches) {
|
|
118
|
+
if (ansi === "\x1B[0m" || ansi === "\x1B]8;;\x07") {
|
|
119
|
+
closingSequences.push(ansi);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (ansi.startsWith("\x1B[") && ansi.endsWith("m")) {
|
|
123
|
+
const code = ansi.slice(2, -1);
|
|
124
|
+
if (ANSI_RESET_CODES.has(code)) {
|
|
125
|
+
closingSequences.push(ansi);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const activeStylesSet = /* @__PURE__ */ new Set();
|
|
131
|
+
const segments = [];
|
|
132
|
+
for (const [index, text] of parts.entries()) {
|
|
133
|
+
if (index > 0 && matches[index - 1]) {
|
|
134
|
+
const ansi = matches[index - 1];
|
|
135
|
+
if (ansi === "\x1B[0m") {
|
|
136
|
+
activeStylesSet.clear();
|
|
137
|
+
} else if (ansi === "\x1B]8;;\x07") {
|
|
138
|
+
for (const style of activeStylesSet) {
|
|
139
|
+
if (style.startsWith("\x1B]8;;") && !style.endsWith("\x1B]8;;\x07")) {
|
|
140
|
+
activeStylesSet.delete(style);
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} else if (ansi.startsWith("\x1B[") && ansi.endsWith("m")) {
|
|
145
|
+
const code = ansi.slice(2, -1);
|
|
146
|
+
if (code === "39") {
|
|
147
|
+
for (const style of activeStylesSet) {
|
|
148
|
+
if (style.startsWith("\x1B[") && style.endsWith("m")) {
|
|
149
|
+
const styleCode = style.slice(2, -1);
|
|
150
|
+
if (isForegroundColor(styleCode)) {
|
|
151
|
+
activeStylesSet.delete(style);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} else if (code === "49") {
|
|
157
|
+
for (const style of activeStylesSet) {
|
|
158
|
+
if (style.startsWith("\x1B[") && style.endsWith("m")) {
|
|
159
|
+
const styleCode = style.slice(2, -1);
|
|
160
|
+
if (isBackgroundColor(styleCode)) {
|
|
161
|
+
activeStylesSet.delete(style);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} else if (FORMAT_RESET_CODES.has(code)) {
|
|
167
|
+
const targetCode = FORMAT_RESET_CODES.get(code);
|
|
168
|
+
for (const style of activeStylesSet) {
|
|
169
|
+
if (style === `\x1B[${targetCode}m`) {
|
|
170
|
+
activeStylesSet.delete(style);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
if (isForegroundColor(code)) {
|
|
176
|
+
for (const style of activeStylesSet) {
|
|
177
|
+
if (style.startsWith("\x1B[") && style.endsWith("m")) {
|
|
178
|
+
const styleCode = style.slice(2, -1);
|
|
179
|
+
if (isForegroundColor(styleCode)) {
|
|
180
|
+
activeStylesSet.delete(style);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} else if (isBackgroundColor(code)) {
|
|
186
|
+
for (const style of activeStylesSet) {
|
|
187
|
+
if (style.startsWith("\x1B[") && style.endsWith("m")) {
|
|
188
|
+
const styleCode = style.slice(2, -1);
|
|
189
|
+
if (isBackgroundColor(styleCode)) {
|
|
190
|
+
activeStylesSet.delete(style);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
} else if (STYLE_CODES.has(code)) {
|
|
196
|
+
const targetStyle = `\x1B[${code}m`;
|
|
197
|
+
if (activeStylesSet.has(targetStyle)) {
|
|
198
|
+
activeStylesSet.delete(targetStyle);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
activeStylesSet.add(ansi);
|
|
202
|
+
}
|
|
203
|
+
} else if (ansi.startsWith("\x1B]8;;") && !ansi.endsWith("\x1B]8;;\x07")) {
|
|
204
|
+
for (const style of activeStylesSet) {
|
|
205
|
+
if (style.startsWith("\x1B]8;;") && !style.endsWith("\x1B]8;;\x07")) {
|
|
206
|
+
activeStylesSet.delete(style);
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
activeStylesSet.add(ansi);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (text === "") {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
let closingSequence = "";
|
|
217
|
+
if (activeStylesSet.size > 0) {
|
|
218
|
+
if (closingSequences.includes("\x1B[0m")) {
|
|
219
|
+
closingSequence = "\x1B[0m";
|
|
220
|
+
} else {
|
|
221
|
+
const closingParts = [];
|
|
222
|
+
let needsHyperlinkClose = false;
|
|
223
|
+
for (const style of activeStylesSet) {
|
|
224
|
+
if (style.startsWith("\x1B]8;;") && !style.endsWith("\x1B]8;;\x07")) {
|
|
225
|
+
needsHyperlinkClose = true;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (needsHyperlinkClose && closingSequences.includes("\x1B]8;;\x07")) {
|
|
230
|
+
closingParts.push("\x1B]8;;\x07");
|
|
231
|
+
}
|
|
232
|
+
let needsForegroundClose = false;
|
|
233
|
+
let needsBackgroundClose = false;
|
|
234
|
+
for (const style of activeStylesSet) {
|
|
235
|
+
if (style.startsWith("\x1B[") && style.endsWith("m")) {
|
|
236
|
+
const styleCode = style.slice(2, -1);
|
|
237
|
+
if (isForegroundColor(styleCode)) {
|
|
238
|
+
needsForegroundClose = true;
|
|
239
|
+
} else if (isBackgroundColor(styleCode)) {
|
|
240
|
+
needsBackgroundClose = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (needsForegroundClose && needsBackgroundClose) {
|
|
245
|
+
const fgOpenPos = findFirstPositionOfAny(input, [
|
|
246
|
+
"\x1B[3",
|
|
247
|
+
// Basic colors 30-37
|
|
248
|
+
"\x1B[9",
|
|
249
|
+
// Bright colors 90-97
|
|
250
|
+
"\x1B[38;"
|
|
251
|
+
// RGB/256 colors
|
|
252
|
+
]);
|
|
253
|
+
const bgOpenPos = findFirstPositionOfAny(input, [
|
|
254
|
+
"\x1B[4",
|
|
255
|
+
// Basic colors 40-47
|
|
256
|
+
"\x1B[10",
|
|
257
|
+
// Bright colors 100-107
|
|
258
|
+
"\x1B[48;"
|
|
259
|
+
// RGB/256 colors
|
|
260
|
+
]);
|
|
261
|
+
if (fgOpenPos >= 0 && bgOpenPos >= 0) {
|
|
262
|
+
if (fgOpenPos > bgOpenPos) {
|
|
263
|
+
closingParts.push("\x1B[39m", "\x1B[49m");
|
|
264
|
+
} else {
|
|
265
|
+
closingParts.push("\x1B[49m", "\x1B[39m");
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
closingParts.push("\x1B[39m", "\x1B[49m");
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
if (needsForegroundClose) {
|
|
272
|
+
closingParts.push("\x1B[39m");
|
|
273
|
+
}
|
|
274
|
+
if (needsBackgroundClose) {
|
|
275
|
+
closingParts.push("\x1B[49m");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (needsForegroundClose && needsBackgroundClose && input.includes("\x1B[39m") && input.includes("\x1B[49m")) {
|
|
279
|
+
if (input.indexOf("\x1B[39m") < input.indexOf("\x1B[49m")) {
|
|
280
|
+
closingParts.length -= needsForegroundClose && needsBackgroundClose ? 2 : needsForegroundClose || needsBackgroundClose ? 1 : 0;
|
|
281
|
+
closingParts.push("\x1B[39m", "\x1B[49m");
|
|
282
|
+
} else if (input.indexOf("\x1B[49m") < input.indexOf("\x1B[39m")) {
|
|
283
|
+
closingParts.length -= needsForegroundClose && needsBackgroundClose ? 2 : needsForegroundClose || needsBackgroundClose ? 1 : 0;
|
|
284
|
+
closingParts.push("\x1B[49m", "\x1B[39m");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const activeFormatStyles = FORMAT_STYLES.filter((style) => activeStylesSet.has(style.open)).map((style) => {
|
|
288
|
+
return {
|
|
289
|
+
position: input.indexOf(style.open),
|
|
290
|
+
style
|
|
291
|
+
};
|
|
292
|
+
}).filter((item) => item.position >= 0).sort((a, b) => b.position - a.position);
|
|
293
|
+
for (const item of activeFormatStyles) {
|
|
294
|
+
if (closingSequences.includes(item.style.close)) {
|
|
295
|
+
closingParts.push(item.style.close);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
closingSequence = closingParts.join("");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const activeStylesString = [...activeStylesSet].join("");
|
|
302
|
+
segments.push({
|
|
303
|
+
after: closingSequence,
|
|
304
|
+
before: activeStylesString,
|
|
305
|
+
content: text,
|
|
306
|
+
visibleLength: getStringWidth.getStringWidth(text, {
|
|
307
|
+
fullWidth: 1,
|
|
308
|
+
wideWidth: 1
|
|
309
|
+
})
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
segmentCache.set(input, segments);
|
|
313
|
+
return segments;
|
|
314
|
+
}, "processIntoStyledSegments");
|
|
315
|
+
const fastSlice = /* @__PURE__ */ __name((inputString, startIndex, endIndex, options) => {
|
|
316
|
+
const graphemes = [...options.segmenter.segment(inputString)];
|
|
317
|
+
const result = [];
|
|
318
|
+
let currentWidth = 0;
|
|
319
|
+
let startFound = false;
|
|
320
|
+
for (const grapheme of graphemes) {
|
|
321
|
+
const graphemeWidth = getStringWidth.getStringWidth(grapheme.segment, options.width);
|
|
322
|
+
const positionBefore = currentWidth;
|
|
323
|
+
const positionAfter = currentWidth + graphemeWidth;
|
|
324
|
+
if (!startFound) {
|
|
325
|
+
if (positionBefore < startIndex) {
|
|
326
|
+
currentWidth = positionAfter;
|
|
327
|
+
continue;
|
|
328
|
+
} else {
|
|
329
|
+
startFound = true;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (positionBefore >= endIndex) {
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
if (positionAfter > endIndex) {
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
result.push(grapheme.segment);
|
|
339
|
+
currentWidth = positionAfter;
|
|
340
|
+
}
|
|
341
|
+
return result.join("");
|
|
342
|
+
}, "fastSlice");
|
|
343
|
+
const slice = /* @__PURE__ */ __name((inputString, startIndex = 0, endIndex = inputString.length, options = {}) => {
|
|
344
|
+
const config = {
|
|
345
|
+
segmenter: defaultSegmenter,
|
|
346
|
+
...options
|
|
347
|
+
};
|
|
348
|
+
if (startIndex >= endIndex || inputString === "") {
|
|
349
|
+
return "";
|
|
350
|
+
}
|
|
351
|
+
if (startIndex === 0 && endIndex >= inputString.length) {
|
|
352
|
+
return inputString;
|
|
353
|
+
}
|
|
354
|
+
if (startIndex < 0 || endIndex < 0) {
|
|
355
|
+
throw new RangeError("Negative indices aren't supported");
|
|
356
|
+
}
|
|
357
|
+
if (!inputString.includes("\x1B")) {
|
|
358
|
+
if (/^[\u0000-\u007F]*$/.test(inputString)) {
|
|
359
|
+
return inputString.slice(startIndex, endIndex);
|
|
360
|
+
}
|
|
361
|
+
return fastSlice(inputString, startIndex, endIndex, config);
|
|
362
|
+
}
|
|
363
|
+
const segments = processIntoStyledSegments(inputString, config);
|
|
364
|
+
if (segments.length === 0) {
|
|
365
|
+
return "";
|
|
366
|
+
}
|
|
367
|
+
let currentPos = 0;
|
|
368
|
+
const visibleSegments = [];
|
|
369
|
+
for (const [index, segment] of segments.entries()) {
|
|
370
|
+
const segmentStart = currentPos;
|
|
371
|
+
const segmentEnd = currentPos + segment.visibleLength;
|
|
372
|
+
if (segmentEnd > startIndex && segmentStart < endIndex) {
|
|
373
|
+
const visibleStart = Math.max(0, startIndex - segmentStart);
|
|
374
|
+
const visibleEnd = Math.min(segment.visibleLength, endIndex - segmentStart);
|
|
375
|
+
visibleSegments.push({
|
|
376
|
+
end: visibleEnd,
|
|
377
|
+
index,
|
|
378
|
+
segment,
|
|
379
|
+
start: visibleStart
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
currentPos = segmentEnd;
|
|
383
|
+
}
|
|
384
|
+
if (visibleSegments.length === 0) {
|
|
385
|
+
return "";
|
|
386
|
+
}
|
|
387
|
+
const resultParts = [];
|
|
388
|
+
const firstSegment = visibleSegments[0].segment;
|
|
389
|
+
resultParts.push(firstSegment.before);
|
|
390
|
+
for (let index = 0; index < visibleSegments.length; index++) {
|
|
391
|
+
const { end, segment, start } = visibleSegments[index];
|
|
392
|
+
if (start === 0 && end === segment.visibleLength) {
|
|
393
|
+
resultParts.push(segment.content);
|
|
394
|
+
} else {
|
|
395
|
+
const graphemes = [...config.segmenter.segment(segment.content)];
|
|
396
|
+
resultParts.push(
|
|
397
|
+
graphemes.slice(start, end).map((entry) => entry.segment).join("")
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (index < visibleSegments.length - 1) {
|
|
401
|
+
const nextSegment = visibleSegments[index + 1].segment;
|
|
402
|
+
const segmentAfter = segment.after;
|
|
403
|
+
const nextBefore = nextSegment.before;
|
|
404
|
+
if (segmentAfter !== nextBefore && (segmentAfter || nextBefore)) {
|
|
405
|
+
resultParts.push(segmentAfter, nextBefore);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
resultParts.push(visibleSegments.at(-1).segment.after);
|
|
410
|
+
return resultParts.join("");
|
|
411
|
+
}, "slice");
|
|
412
|
+
|
|
413
|
+
exports.slice = slice;
|
package/dist/slice.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StringWidthOptions } from './get-string-width.cjs';
|
|
2
|
+
import './get-string-truncated-width.cjs';
|
|
3
|
+
|
|
4
|
+
type SliceOptions = {
|
|
5
|
+
segmenter?: Intl.Segmenter;
|
|
6
|
+
width?: StringWidthOptions;
|
|
7
|
+
};
|
|
8
|
+
declare const slice: (inputString: string, startIndex?: number, endIndex?: number, options?: SliceOptions) => string;
|
|
9
|
+
|
|
10
|
+
export { type SliceOptions, slice };
|
package/dist/slice.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StringWidthOptions } from './get-string-width.mjs';
|
|
2
|
+
import './get-string-truncated-width.mjs';
|
|
3
|
+
|
|
4
|
+
type SliceOptions = {
|
|
5
|
+
segmenter?: Intl.Segmenter;
|
|
6
|
+
width?: StringWidthOptions;
|
|
7
|
+
};
|
|
8
|
+
declare const slice: (inputString: string, startIndex?: number, endIndex?: number, options?: SliceOptions) => string;
|
|
9
|
+
|
|
10
|
+
export { type SliceOptions, slice };
|
package/dist/slice.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StringWidthOptions } from './get-string-width.js';
|
|
2
|
+
import './get-string-truncated-width.js';
|
|
3
|
+
|
|
4
|
+
type SliceOptions = {
|
|
5
|
+
segmenter?: Intl.Segmenter;
|
|
6
|
+
width?: StringWidthOptions;
|
|
7
|
+
};
|
|
8
|
+
declare const slice: (inputString: string, startIndex?: number, endIndex?: number, options?: SliceOptions) => string;
|
|
9
|
+
|
|
10
|
+
export { type SliceOptions, slice };
|