Skip to content

Commit

Permalink
CommonJS version
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 29, 2024
1 parent 1313322 commit ba8bfa7
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 146 deletions.
47 changes: 0 additions & 47 deletions base.d.ts

This file was deleted.

88 changes: 0 additions & 88 deletions base.js

This file was deleted.

139 changes: 137 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,137 @@
export * from './base.js';
export * as default from './base.js';
type Format = (string: string) => string;

declare const reset: Format;
declare const bold: Format;
declare const dim: Format;
declare const italic: Format;
declare const underline: Format;
declare const overline: Format;
declare const inverse: Format;
declare const hidden: Format;
declare const strikethrough: Format;

declare const black: Format;
declare const red: Format;
declare const green: Format;
declare const yellow: Format;
declare const blue: Format;
declare const magenta: Format;
declare const cyan: Format;
declare const white: Format;
declare const gray: Format;

declare const bgBlack: Format;
declare const bgRed: Format;
declare const bgGreen: Format;
declare const bgYellow: Format;
declare const bgBlue: Format;
declare const bgMagenta: Format;
declare const bgCyan: Format;
declare const bgWhite: Format;
declare const bgGray: Format;

declare const redBright: Format;
declare const greenBright: Format;
declare const yellowBright: Format;
declare const blueBright: Format;
declare const magentaBright: Format;
declare const cyanBright: Format;
declare const whiteBright: Format;

declare const bgRedBright: Format;
declare const bgGreenBright: Format;
declare const bgYellowBright: Format;
declare const bgBlueBright: Format;
declare const bgMagentaBright: Format;
declare const bgCyanBright: Format;
declare const bgWhiteBright: Format;

declare const formats: {
reset: Format;
bold: Format;
dim: Format;
italic: Format;
underline: Format;
overline: Format;
inverse: Format;
hidden: Format;
strikethrough: Format;
black: Format;
red: Format;
green: Format;
yellow: Format;
blue: Format;
magenta: Format;
cyan: Format;
white: Format;
gray: Format;
bgBlack: Format;
bgRed: Format;
bgGreen: Format;
bgYellow: Format;
bgBlue: Format;
bgMagenta: Format;
bgCyan: Format;
bgWhite: Format;
bgGray: Format;
redBright: Format;
greenBright: Format;
yellowBright: Format;
blueBright: Format;
magentaBright: Format;
cyanBright: Format;
whiteBright: Format;
bgRedBright: Format;
bgGreenBright: Format;
bgYellowBright: Format;
bgBlueBright: Format;
bgMagentaBright: Format;
bgCyanBright: Format;
bgWhiteBright: Format;
};

export = formats;

export {
reset,
bold,
dim,
italic,
underline,
overline,
inverse,
hidden,
strikethrough,
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
bgGray,
redBright,
greenBright,
yellowBright,
blueBright,
magentaBright,
cyanBright,
whiteBright,
bgRedBright,
bgGreenBright,
bgYellowBright,
bgBlueBright,
bgMagentaBright,
bgCyanBright,
bgWhiteBright

Check failure on line 136 in index.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 22

Missing trailing comma.

Check failure on line 136 in index.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Missing trailing comma.
};
94 changes: 92 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,92 @@
export * from './base.js';
export * as default from './base.js';
const tty = require('node:tty');

Check failure on line 1 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 22

Do not use "require".

Check failure on line 1 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Do not use "require".

// eslint-disable-next-line no-warning-comments
// TODO: Use a better method when it's added to Node.js (https://github.com/nodejs/node/pull/40240)
const hasColors = tty.WriteStream.prototype.hasColors();

const format = (open, close) => {
if (!hasColors) {
return input => input;
}

const openCode = `\u001B[${open}m`;
const closeCode = `\u001B[${close}m`;

return input => {
const string = input + ''; // eslint-disable-line no-implicit-coercion -- This is faster.
let index = string.indexOf(closeCode);

if (index === -1) {
// Note: Intentionally not using string interpolation for performance reasons.
return openCode + string + closeCode;
}

// Handle nested colors.

// We could have done this, but it's too slow (as of Node.js 22).
// return openCode + string.replaceAll(closeCode, openCode) + closeCode;

let result = openCode;
let lastIndex = 0;

while (index !== -1) {
result += string.slice(lastIndex, index) + openCode;
lastIndex = index + closeCode.length;
index = string.indexOf(closeCode, lastIndex);
}

result += string.slice(lastIndex) + closeCode;

return result;
};
};

const colors = {};

colors.reset = format(0, 0);
colors.bold = format(1, 22);
colors.dim = format(2, 22);
colors.italic = format(3, 23);
colors.underline = format(4, 24);
colors.overline = format(53, 55);
colors.inverse = format(7, 27);
colors.hidden = format(8, 28);
colors.strikethrough = format(9, 29);

colors.black = format(30, 39);
colors.red = format(31, 39);
colors.green = format(32, 39);
colors.yellow = format(33, 39);
colors.blue = format(34, 39);
colors.magenta = format(35, 39);
colors.cyan = format(36, 39);
colors.white = format(37, 39);
colors.gray = format(90, 39);

colors.bgBlack = format(40, 49);
colors.bgRed = format(41, 49);
colors.bgGreen = format(42, 49);
colors.bgYellow = format(43, 49);
colors.bgBlue = format(44, 49);
colors.bgMagenta = format(45, 49);
colors.bgCyan = format(46, 49);
colors.bgWhite = format(47, 49);
colors.bgGray = format(100, 49);

colors.redBright = format(91, 39);
colors.greenBright = format(92, 39);
colors.yellowBright = format(93, 39);
colors.blueBright = format(94, 39);
colors.magentaBright = format(95, 39);
colors.cyanBright = format(96, 39);
colors.whiteBright = format(97, 39);

colors.bgRedBright = format(101, 49);
colors.bgGreenBright = format(102, 49);
colors.bgYellowBright = format(103, 49);
colors.bgBlueBright = format(104, 49);
colors.bgMagentaBright = format(105, 49);
colors.bgCyanBright = format(106, 49);
colors.bgWhiteBright = format(107, 49);

module.exports = colors;

Check failure on line 92 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 22

Do not use "module".

Check failure on line 92 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Do not use "module".
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yoctocolors",
"name": "yoctocolors-cjs",
"version": "2.1.0",
"description": "The smallest and fastest command-line coloring package on the internet",
"description": "CommonJS version - The smallest and fastest command-line coloring package on the internet",
"license": "MIT",
"repository": "sindresorhus/yoctocolors",
"funding": "https://github.com/sponsors/sindresorhus",
Expand All @@ -10,11 +10,9 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"type": "commonjs",
"main": "./index.js",
"types": "./index.d.ts",
"sideEffects": false,
"engines": {
"node": ">=18"
Expand Down

0 comments on commit ba8bfa7

Please sign in to comment.