Skip to content

Commit

Permalink
refactor(types): put recurring type in a type variable
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Feb 4, 2023
1 parent a81083d commit 044256c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,29 @@ export let options = {
supportLevel,
};

type Kolorist = (str: string | number) => string;

function kolorist(
start: number | string,
end: number | string,
level: SupportLevel = SupportLevel.ansi
) {
): Kolorist {
const open = `\x1b[${start}m`;
const close = `\x1b[${end}m`;
const regex = new RegExp(`\\x1b\\[${end}m`, 'g');

return (str: string | number) => {
return (str) => {
return options.enabled && options.supportLevel >= level
? open + ('' + str).replace(regex, open) + close
: '' + str;
};
}

export function stripColors(str: string | number) {
return ('' + str)
.replace(/\x1b\[[0-9;]+m/g, '')
.replace(/\x1b\]8;;.*?\x07(.*?)\x1b\]8;;\x07/g, (_, group) => group);
}
export const stripColors: Kolorist = (str) => (
('' + str)
.replace(/\x1b\[[0-9;]+m/g, '')
.replace(/\x1b\]8;;.*?\x07(.*?)\x1b\]8;;\x07/g, (_, group) => group)
);

// modifiers
export const reset = kolorist(0, 0);
Expand Down

0 comments on commit 044256c

Please sign in to comment.