Skip to content

Commit

Permalink
fix: cast falsy values 0, false, null, undefined, NaN to a …
Browse files Browse the repository at this point in the history
…string
  • Loading branch information
webdiscus committed Jan 7, 2025
1 parent 7e07074 commit 98c44b8
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 83 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change log

## 3.7.0 (2025-01-07)

- fix: cast falsy values `false`, `null`, `undefined`, `NaN` to a string.
In previous versions, the empty string `''` was returned for falsy values.
- fix: functions with argument `0` , e.g. `ansis.red(0)`, returning empty string `''`, now return colored value `'0'`
- test: add tests for function arguments with various types

## 3.6.0 (2025-01-04)

- feat: remove **undocumented** pointless dummy function `ansis(any)`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

A [small](#compare-size) and [fast](#benchmark) Node.js library for applying ANSI colors and styles in terminal output.\
Ansis provides all the [features](#features) you need, you can compare with [similar libraries](#compare).\
Ansis is [faster](#benchmark) than **Chalk** and **Picocolors** (in some use cases), see [benchmarks](#benchmark).
Ansis is as [small](#compare-size) as **Picocolors** but has the [functionality](#compare) of **Chalk**.


### 🚀 [Install and Quick Start](#install) 🔄 [Why switch to Ansis](#why-switch-to-ansis)
Expand Down
4 changes: 2 additions & 2 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ bench('Chained syntax').
// kolorist - (not supported)
run();

// Nested styles, like picocolors recursion
bench('Nested styles').
// Nested calls, like colorette recursion
bench('Nested calls').
add(packages['chalk'], () => chalk.red(chalk.bold(chalk.underline(chalk.bgWhite('foo'))))).
add(packages['ansis'], () => ansis.red(ansis.bold(ansis.underline(ansis.bgWhite('foo'))))).
add(packages['picocolors'], () => picocolors.red(picocolors.bold(picocolors.underline(picocolors.bgWhite('foo'))))).
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": "3.6.0",
"version": "3.7.0",
"description": "A small and fast Node.js library for applying ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
2 changes: 1 addition & 1 deletion package.npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.6.0",
"version": "3.7.0",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ let createStyle = ({ _p: props }, { open, close }) => {
* @return {string}
*/
let styleFn = (strings, ...values) => {
if (!strings) return '';

let props = styleFn._p;
let { _a: openStack, _b: closeStack } = props;

let str = strings.raw
// optional chaining operator `?.` available since node >= 14
let str = strings?.raw
// render template strings
? String.raw(strings, ...values)
// convert the number to the string
// convert the value to a string
: '' + strings;

// -> detect nested styles
Expand Down
2 changes: 1 addition & 1 deletion test/ansi16.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, describe, test } from 'vitest';
import { esc } from './utils/helpers.js';

// import env variables to simulate ANSI 16 color space
import './env/color-space.ansi16.js';
import './env/ansi16-colors.js';

//import { hex, green } from '../src/index.mjs'; // for debugging only
import { hex, green } from 'ansis';
Expand Down
2 changes: 1 addition & 1 deletion test/ansi256.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, describe, test } from 'vitest';
import { esc } from './utils/helpers.js';

// import env variables to simulate ANSI 256 color space
import './env/color-space.ansi256.js';
import './env/ansi256-colors.js';

//import { hex, ansi256 } from '../src/index.mjs'; // for debugging only
import { hex, ansi256 } from 'ansis';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/flags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, describe, test } from 'vitest';
import { esc, execScriptSync } from './utils/helpers.js';

// import env variables to simulate truecolor color space in CLI
import './env/color-space.truecolor.js';
import './env/truecolor.js';

const TEST_PATH = path.resolve('./test/');

Expand Down
Loading

0 comments on commit 98c44b8

Please sign in to comment.