Skip to content

Commit

Permalink
feat: support both CJS and ESM syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdUlHamedMaree committed May 5, 2024
1 parent 02fa893 commit 3a02432
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
node_modules
yarn.lock

# bundle
index.cjs*
index.js*
index.d.ts
8 changes: 0 additions & 8 deletions index.js

This file was deleted.

20 changes: 14 additions & 6 deletions index.d.ts → index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export interface Options {
/**
Match only the first ANSI escape.
/**
Match only the first ANSI escape.
@default false
*/
readonly onlyFirst: boolean;
@default false
*/
readonly onlyFirst?: boolean;
}


/**
Regular expression for matching ANSI escape codes.
Expand All @@ -30,4 +31,11 @@ ansiRegex().test('cake');
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
```
*/
export default function ansiRegex(options?: Options): RegExp;
export default function ansiRegex({ onlyFirst = false }: Options = {}) {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))'
].join('|');

return new RegExp(pattern, onlyFirst ? undefined : 'g');
}
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,34 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"source": "./index.ts",
"main": "./index.cjs",
"module": "./index.js",
"types": "./index.d.ts",
"exports": {
".": {
"default": "./index.cjs",
"node": "./index.cjs",
"require": "./index.cjs",
"import": "./index.js",
"types": "./index.d.ts"
},
"./package.json": "./package.json"
},
"engines": {
"node": ">=12"
},
"scripts": {
"build": "lbundle",
"pretest": "lbundle",
"test": "xo && ava && tsd",
"view-supported": "node fixtures/view-codes.js"
},
"files": [
"index.js",
"index.js.map",
"index.cjs",
"index.cjs.map",
"index.d.ts"
],
"keywords": [
Expand Down Expand Up @@ -53,6 +71,7 @@
"devDependencies": {
"ansi-escapes": "^5.0.0",
"ava": "^3.15.0",
"lbundle": "^1.1.2",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
Expand Down

0 comments on commit 3a02432

Please sign in to comment.