Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chalk/strip-ansi
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c299056a42b31d7a479d6a89b41318b2a2462cc7
Choose a base ref
...
head repository: chalk/strip-ansi
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 316ff53b074b4b1465e18a35e5d792b27ecc2865
Choose a head ref
  • 20 commits
  • 14 files changed
  • 5 contributors

Commits on Sep 20, 2017

  1. Verified

    This commit was signed with the committer’s verified signature.
    bagder Daniel Stenberg
    Copy the full SHA
    52dcf65 View commit details

Commits on Sep 29, 2018

  1. Copy the full SHA
    e8d149c View commit details
  2. Copy the full SHA
    6a25566 View commit details
  3. 5.0.0

    sindresorhus committed Sep 29, 2018
    Copy the full SHA
    dfab677 View commit details

Commits on Dec 25, 2018

  1. Add security section

    sindresorhus authored Dec 25, 2018
    Copy the full SHA
    841f0c4 View commit details

Commits on Mar 8, 2019

  1. Copy the full SHA
    41b0a8b View commit details
  2. 5.1.0

    sindresorhus committed Mar 8, 2019
    Copy the full SHA
    581fd4e View commit details
  3. Fix readme (#27)

    LitoMore authored and sindresorhus committed Mar 8, 2019
    Copy the full SHA
    5cb7e20 View commit details

Commits on Mar 18, 2019

  1. Copy the full SHA
    89dc7f6 View commit details
  2. Meta tweaks

    sindresorhus committed Mar 18, 2019
    Copy the full SHA
    81cd3cc View commit details
  3. 5.2.0

    sindresorhus committed Mar 18, 2019
    Copy the full SHA
    b9c4929 View commit details

Commits on May 31, 2019

  1. Tidelift tasks

    sindresorhus committed May 31, 2019
    Copy the full SHA
    a022f23 View commit details

Commits on Oct 30, 2019

  1. Tidelift tasks

    sindresorhus committed Oct 30, 2019
    Copy the full SHA
    57878e2 View commit details

Commits on Nov 9, 2019

  1. Require Node.js 8

    Closes #29
    sindresorhus committed Nov 9, 2019
    Copy the full SHA
    976f459 View commit details
  2. 6.0.0

    sindresorhus committed Nov 9, 2019
    Copy the full SHA
    59533da View commit details

Commits on Nov 20, 2020

  1. Copy the full SHA
    0a6e98d View commit details

Commits on Dec 2, 2020

  1. Move to GitHub Actions

    sindresorhus committed Dec 2, 2020
    1
    Copy the full SHA
    601423d View commit details

Commits on Dec 16, 2020

  1. Add @Qix- to funding.yml

    sindresorhus authored Dec 16, 2020
    Copy the full SHA
    d6d1128 View commit details

Commits on Apr 16, 2021

  1. Copy the full SHA
    7cda68d View commit details
  2. 7.0.0

    sindresorhus committed Apr 16, 2021
    Copy the full SHA
    316ff53 View commit details
Showing with 133 additions and 75 deletions.
  1. +1 −2 .gitattributes
  2. +2 −0 .github/funding.yml
  3. +3 −0 .github/security.md
  4. +21 −0 .github/workflows/main.yml
  5. +1 −0 .gitignore
  6. +1 −0 .npmrc
  7. +0 −5 .travis.yml
  8. +15 −0 index.d.ts
  9. +8 −3 index.js
  10. +4 −0 index.test-d.ts
  11. +1 −1 license
  12. +55 −50 package.json
  13. +12 −10 readme.md
  14. +9 −4 test.js
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 2 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [sindresorhus, Qix-]
tidelift: npm/strip-ansi
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string.
@example
```
import stripAnsi from 'strip-ansi';
stripAnsi('\u001B[4mUnicorn\u001B[0m');
//=> 'Unicorn'
stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
//=> 'Click'
```
*/
export default function stripAnsi(string: string): string;
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
'use strict';
const ansiRegex = require('ansi-regex');
import ansiRegex from 'ansi-regex';

module.exports = input => typeof input === 'string' ? input.replace(ansiRegex(), '') : input;
export default function stripAnsi(string) {
if (typeof string !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
}

return string.replace(ansiRegex(), '');
}
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from 'tsd';
import stripAnsi from './index.js';

expectType<string>(stripAnsi('\u001B[4mcake\u001B[0m'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

105 changes: 55 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
{
"name": "strip-ansi",
"version": "4.0.0",
"description": "Strip ANSI escape codes",
"license": "MIT",
"repository": "chalk/strip-ansi",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"strip",
"trim",
"remove",
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"dependencies": {
"ansi-regex": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "strip-ansi",
"version": "7.0.0",
"description": "Strip ANSI escape codes from a string",
"license": "MIT",
"repository": "chalk/strip-ansi",
"funding": "https://github.com/chalk/strip-ansi?sponsor=1",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"strip",
"trim",
"remove",
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"dependencies": {
"ansi-regex": "^6.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
22 changes: 12 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
# strip-ansi [![Build Status](https://travis-ci.org/chalk/strip-ansi.svg?branch=master)](https://travis-ci.org/chalk/strip-ansi)

> Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
# strip-ansi

> Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string
## Install

```
$ npm install strip-ansi
```


## Usage

```js
const stripAnsi = require('strip-ansi');
import stripAnsi from 'strip-ansi';

stripAnsi('\u001B[4mUnicorn\u001B[0m');
//=> 'Unicorn'

stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
//=> 'Click'
```

## strip-ansi for enterprise

Available as part of the Tidelift Subscription.

The maintainers of strip-ansi and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-strip-ansi?utm_source=npm-strip-ansi&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

## Related

- [strip-ansi-cli](https://github.com/chalk/strip-ansi-cli) - CLI for this module
- [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Streaming version of this module
- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right


## Maintainers

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)


## License

MIT
13 changes: 9 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import test from 'ava';
import m from '.';
import stripAnsi from './index.js';

test('strip color from string', t => {
t.is(m('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m'), 'foofoo');
t.is(stripAnsi('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m'), 'foofoo');
});

test('strip color from ls command', t => {
t.is(m('\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m'), 'foo');
t.is(stripAnsi('\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m'), 'foo');
});

test('strip reset;setfg;setbg;italics;strike;underline sequence from string', t => {
t.is(m('\u001B[0;33;49;3;9;4mbar\u001B[0m'), 'bar');
t.is(stripAnsi('\u001B[0;33;49;3;9;4mbar\u001B[0m'), 'bar');
});

test('strip link from terminal link', t => {
t.is(stripAnsi('\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'), 'click');
});