Skip to content

Commit

Permalink
refactor: optimize code to reduce size by 120 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Sep 22, 2022
1 parent baf0a80 commit 5c0150a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## 1.5.5 (2022-09-22)
- refactor: optimize code to reduce size by 120 bytes
- test: added test for isSupported() function
- docs: update readme, add example screenshots

## 1.5.4 (2022-09-14)
- fix: visible style with nested template strings

Expand Down
10 changes: 1 addition & 9 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@ import chalk from 'chalk';

import ansis, { Ansis } from '../src/index.js';
import {
white,
red,
green,
blue,
cyan,
cyanBright,
yellow,
yellowBright,
magenta,
grey,
whiteBright,
bold,
italic,
underline,
reset,
inverse,
visible,
hex,
rgb,
fg,
} from '../src/colors.mjs';

import { ansi256Table } from './ansi256.js';
Expand All @@ -32,14 +25,13 @@ import { ansisStylesDemo } from './ansis-styles-demo.js';

const log = console.log;
const style = 'green';
const c = ansis;

/**
* ANSIS Logo.
* Showing in readme.
*/

log(inverse`ANSIS logo:`);
console.log(inverse`ANSIS logo:`);
log();
log(ansisLogo);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "1.5.4",
"version": "1.5.5",
"description": "Formatting text in terminal with ANSI colors & styles.",
"keywords": [
"256",
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "1.5.4",
"version": "1.5.5",
"description": "Formatting text in terminal with ANSI colors & styles.",
"keywords": [
"256",
Expand Down
26 changes: 11 additions & 15 deletions src/ansi-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ export const isSupported = (processMock) => {
return !isForceDisabled && (isForceEnabled || isTerm || platform === 'win32' || 'CI' in env);
};

export const supported = isSupported();

const noColor = { open: '', close: '' };
const esc = supported ? (open, close) => ({ open: `\x1b[${open}m`, close: `\x1b[${close}m` }) : () => noColor;
const esc = isSupported() ? (open, close) => ({ open: `\x1b[${open}m`, close: `\x1b[${close}m` }) : () => noColor;

export const ansi256 = supported ? (code) => ({ open: `\x1B[38;5;${code}m`, close: '\x1B[39m' }) : () => noColor;
export const bgAnsi256 = supported ? (code) => ({ open: `\x1B[48;5;${code}m`, close: '\x1B[49m' }) : () => noColor;
export const rgb = supported ? (r, g, b) => ({ open: `\x1B[38;2;${r};${g};${b}m`, close: '\x1B[39m' }) : () => noColor;
export const bgRgb = supported
? (r, g, b) => ({ open: `\x1B[48;2;${r};${g};${b}m`, close: '\x1B[49m' })
: () => noColor;
export const ansi256 = (code) => esc(`38;5;${code}`, 39);
export const bgAnsi256 = (code) => esc(`48;5;${code}`, 49);
export const rgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, 39);
export const bgRgb = (r, g, b) => esc(`48;2;${r};${g};${b}`, 49);

export const baseStyles = {
// misc
Expand All @@ -54,15 +50,15 @@ export const baseStyles = {
// styles
bold: esc(1, 22),
dim: esc(2, 22),
faint: esc(2, 22), // alias for dim
faint: esc(2, 22), // alias for dim, TODO: remove in next major release
italic: esc(3, 23),
underline: esc(4, 24),
doubleUnderline: esc(21, 24),
doubleUnderline: esc(21, 24), // not widely supported, TODO: remove in next major release
strikethrough: esc(9, 29),
strike: esc(9, 29), // alias for strikethrough
frame: esc(51, 54),
encircle: esc(52, 54),
overline: esc(53, 55),
frame: esc(51, 54), // not widely supported, TODO: remove in next major release
encircle: esc(52, 54), // not widely supported, TODO: remove in next major release
overline: esc(53, 55), // not widely supported, TODO: remove in next major release

// foreground colors
black: esc(30, 39),
Expand All @@ -73,8 +69,8 @@ export const baseStyles = {
magenta: esc(35, 39),
cyan: esc(36, 39),
white: esc(37, 39),
gray: esc(90, 39), // US spelling alias for blackBright
grey: esc(90, 39), // UK spelling alias for blackBright
gray: esc(90, 39), // US spelling alias for blackBright
blackBright: esc(90, 39),
redBright: esc(91, 39),
greenBright: esc(92, 39),
Expand Down
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ describe('default tests', () => {
});

describe('isSupported', () => {
test(`process undefined`, (done) => {
// save original `process` object
const processOriginal = process;
process = undefined;

const received = isSupported(undefined);
const expected = false;
expect(received).toEqual(expected);

// restore original `process` object
process = processOriginal;
done();
});

test(`processMock undefined`, (done) => {
const received = isSupported(undefined);
const expected = true;
Expand Down

0 comments on commit 5c0150a

Please sign in to comment.