# yanse

> Fast and lightweight terminal color styling library with chalk-like API

Latest version **0.2.2** (published 2026-07-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install yanse
pnpm add yanse
yarn add yanse
bun add yanse
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2026-07-31 |
| First published | 2025-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Constructive <developers@constructive.io> |
| Maintainers | pyramation |

## Links

- npm: https://www.npmjs.com/package/yanse
- Repository: https://github.com/constructive-io/dev-utils
- Issues: https://github.com/constructive-io/dev-utils/issues
- npm.io page: https://npm.io/package/yanse

## Recent versions

- 0.2.2 (latest) — 2026-07-31
- 0.2.1 — 2026-01-29
- 0.2.0 — 2026-01-20
- 0.1.11 — 2025-12-27
- 0.1.10 — 2025-12-27
- 0.1.9 — 2025-12-27
- 0.1.8 — 2025-12-17
- 0.1.7 — 2025-12-14
- 0.1.6 — 2025-11-28
- 0.1.5 — 2025-11-26
- 0.1.4 — 2025-11-24
- 0.1.3 — 2025-11-24
- 0.1.2 — 2025-11-23
- 0.1.1 — 2025-11-23

## README

# yanse

<p align="center">
  <img src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" height="250">
  <br />
    Yanse (颜色) - Fast terminal color styling
  <br />
  <a href="https://github.com/constructive-io/dev-utils/actions/workflows/ci.yml">
    <img height="20" src="https://github.com/constructive-io/dev-utils/actions/workflows/ci.yml/badge.svg" />
  </a>
  <a href="https://github.com/constructive-io/dev-utils/blob/main/LICENSE">
    <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
  </a>
</p>

Fast and lightweight terminal color styling library with a chalk-like API. Yanse (颜色, yánsè) means "color" in Chinese.

Why? We got tired of chalk's ESM-only errors and needed control over our dependencies. This utility is too simple to justify depending on chalk and wrestling with `module: true`.

## Features

- **Fast & Lightweight** - Zero dependencies, optimized for performance
- **Chalk-like API** - Drop-in replacement for chalk with familiar syntax
- **TypeScript Support** - Fully typed with comprehensive type definitions
- **Nested Colors** - Proper handling of nested color styles without bugs
- **Chained Styles** - Chain multiple colors and modifiers
- **Toggle Support** - Easily enable/disable colors
- **Themes & Aliases** - Create custom color themes and aliases

## Install

```sh
npm install yanse
```

## Usage

### Basic Colors

```typescript
import yanse, { red, green, blue, yellow, cyan } from 'yanse';

console.log(red('Error message'));
console.log(green('Success message'));
console.log(blue('Info message'));
console.log(yellow('Warning message'));
console.log(cyan('Debug message'));
```

### Chained Colors

```typescript
import yanse from 'yanse';

console.log(yanse.bold.red('Bold red text'));
console.log(yanse.bold.yellow.italic('Bold yellow italic text'));
console.log(yanse.green.bold.underline('Bold green underlined text'));
```

### Nested Colors

```typescript
import { yellow, red, cyan } from 'yanse';

console.log(yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`));
```

### Logger Example

Perfect for building loggers with colored output:

```typescript
import yanse, { cyan, yellow, red, green, bold } from 'yanse';

type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'success';

const levelColors: Record<LogLevel, typeof cyan> = {
  info: cyan,
  warn: yellow,
  error: red,
  debug: yanse.gray,
  success: green
};

class Logger {
  constructor(private scope: string) {}

  log(level: LogLevel, message: string) {
    const tag = bold(`[${this.scope}]`);
    const color = levelColors[level];
    const prefix = color(`${level.toUpperCase()}:`);

    console.log(`${tag} ${prefix} ${message}`);
  }
}

const logger = new Logger('MyApp');
logger.log('info', 'Application started');
logger.log('success', 'Connection established');
logger.log('warn', 'Deprecated API used');
logger.log('error', 'Failed to connect');
```

## Available Styles

### Colors

- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
- `gray` / `grey`

### Background Colors

- `bgBlack`
- `bgRed`
- `bgGreen`
- `bgYellow`
- `bgBlue`
- `bgMagenta`
- `bgCyan`
- `bgWhite`

### Bright Colors

- `blackBright`, `redBright`, `greenBright`, `yellowBright`
- `blueBright`, `magentaBright`, `cyanBright`, `whiteBright`

### Bright Background Colors

- `bgBlackBright`, `bgRedBright`, `bgGreenBright`, `bgYellowBright`
- `bgBlueBright`, `bgMagentaBright`, `bgCyanBright`, `bgWhiteBright`

### Style Modifiers

- `bold`
- `dim`
- `italic`
- `underline`
- `inverse`
- `hidden`
- `strikethrough`
- `reset`

## Toggle Color Support

```typescript
import yanse from 'yanse';

// Disable colors
yanse.enabled = false;
console.log(yanse.red('This will not be colored'));

// Re-enable colors
yanse.enabled = true;
console.log(yanse.red('This will be red'));
```

## Strip ANSI Codes

```typescript
import yanse from 'yanse';

const styled = yanse.blue.bold('Hello World');
console.log(yanse.unstyle(styled)); // 'Hello World'
console.log(yanse.stripColor(styled)); // 'Hello World' (alias)
```

## Themes & Aliases

### Create Aliases

```typescript
import yanse from 'yanse';

yanse.alias('primary', yanse.blue);
yanse.alias('secondary', yanse.gray);

console.log(yanse.primary('Primary text'));
console.log(yanse.secondary('Secondary text'));
```

### Create Themes

```typescript
import yanse from 'yanse';

yanse.theme({
  danger: yanse.red,
  success: yanse.green,
  warning: yanse.yellow,
  info: yanse.cyan,
  primary: yanse.blue,
  muted: yanse.dim.gray
});

console.log(yanse.danger('Error occurred!'));
console.log(yanse.success('Operation successful!'));
console.log(yanse.warning('Be careful!'));
```

## Create Custom Instances

```typescript
import { create } from 'yanse';

const customYanse = create();
customYanse.enabled = false; // This instance has colors disabled

console.log(customYanse.red('Not colored'));
```

## API

### Properties

- `enabled: boolean` - Enable/disable color output
- `visible: boolean` - Make output visible/invisible
- `ansiRegex: RegExp` - Regex for matching ANSI codes

### Methods

- `hasColor(str: string): boolean` - Check if string contains ANSI codes
- `hasAnsi(str: string): boolean` - Alias for hasColor
- `unstyle(str: string): string` - Remove ANSI codes from string
- `stripColor(str: string): string` - Alias for unstyle
- `alias(name: string, color: YanseColor): void` - Create color alias
- `theme(colors: Record<string, YanseColor>): void` - Create color theme
- `create(): YanseColors` - Create new yanse instance

## Why Yanse?

- **Zero Dependencies** - No external dependencies, minimal bundle size
- **Fast** - Optimized for performance
- **Correct Nested Colors** - Unlike some libraries, yanse correctly handles nested color styles
- **TypeScript First** - Written in TypeScript with full type support
- **Familiar API** - Drop-in replacement for chalk

## OSS Credit

Inspired by [chalk](https://github.com/chalk/chalk) and [ansi-colors](https://github.com/doowb/ansi-colors).

---

## Development

### Setup

1. Clone the repository:

```bash
git clone https://github.com/constructive-io/dev-utils.git
```

2. Install dependencies:

```bash
cd dev-utils
pnpm install
pnpm build
```

3. Test the package of interest:

```bash
cd packages/<packagename>
pnpm test:watch
```

## Credits

**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**

## Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

---
_Source: https://npm.io/package/yanse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
