Skip to content

Commit

Permalink
Add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 16, 2019
1 parent b1b5d27 commit 9a562ff
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
77 changes: 77 additions & 0 deletions index.d.ts
@@ -0,0 +1,77 @@
declare namespace cloneRegexp {
interface Options {
/**
Modifies the [`source`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source) property of the cloned `RegExp` instance.
*/
source?: string;

/**
Modifies the [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) property of the cloned `RegExp` instance.
*/
global?: boolean;

/**
Modifies the [`ignoreCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase) property of the cloned `RegExp` instance.
*/
ignoreCase?: boolean;

/**
Modifies the [`multiline`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) property of the cloned `RegExp` instance.
*/
multiline?: boolean;

/**
Modifies the [`dotAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll) property of the cloned `RegExp` instance.
*/
dotAll?: boolean;

/**
Modifies the [`sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) property of the cloned `RegExp` instance.
*/
sticky?: boolean;

/**
Modifies the [`unicode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) property of the cloned `RegExp` instance.
*/
unicode?: boolean;

/**
Modifies the [`lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) property of the cloned `RegExp` instance.
*/
lastIndex?: number;
}
}

/**
Clone and modify a [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance.
@param regexp - Regex to clone.
@example
```
import cloneRegexp = require('clone-regexp');
const regex = /[a-z]/gi;
cloneRegexp(regex);
//=> /[a-z]/gi
cloneRegexp(regex) === regex;
//=> false
cloneRegexp(regex, {global: false});
//=> /[a-z]/i
cloneRegexp(regex, {multiline: true});
//=> /[a-z]/gim
cloneRegexp(regex, {source: 'unicorn'});
//=> /unicorn/gi
```
*/
declare function cloneRegexp(
regexp: RegExp,
options?: cloneRegexp.Options
): RegExp;

export = cloneRegexp;
10 changes: 5 additions & 5 deletions index.js
Expand Up @@ -10,20 +10,20 @@ const flagMap = {
unicode: 'u'
};

module.exports = (regex, options = {}) => {
if (!isRegexp(regex)) {
module.exports = (regexp, options = {}) => {
if (!isRegexp(regexp)) {
throw new TypeError('Expected a RegExp instance');
}

const flags = Object.keys(flagMap).map(flag => (
(typeof options[flag] === 'boolean' ? options[flag] : regex[flag]) ? flagMap[flag] : ''
(typeof options[flag] === 'boolean' ? options[flag] : regexp[flag]) ? flagMap[flag] : ''
)).join('');

const clonedRegexp = new RegExp(options.source || regex.source, flags);
const clonedRegexp = new RegExp(options.source || regexp.source, flags);

clonedRegexp.lastIndex = typeof options.lastIndex === 'number' ?
options.lastIndex :
regex.lastIndex;
regexp.lastIndex;

return clonedRegexp;
};
14 changes: 14 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,14 @@
import {expectType} from 'tsd';
import cloneRegexp = require('.');

const regex = /[a-z]/gi;

expectType<RegExp>(cloneRegexp(regex));
expectType<RegExp>(cloneRegexp(regex, {source: 'unicorn'}));
expectType<RegExp>(cloneRegexp(regex, {global: false}));
expectType<RegExp>(cloneRegexp(regex, {ignoreCase: true}));
expectType<RegExp>(cloneRegexp(regex, {multiline: true}));
expectType<RegExp>(cloneRegexp(regex, {dotAll: true}));
expectType<RegExp>(cloneRegexp(regex, {sticky: true}));
expectType<RegExp>(cloneRegexp(regex, {unicode: true}));
expectType<RegExp>(cloneRegexp(regex, {lastIndex: 1}));
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"regexp",
Expand All @@ -33,7 +34,8 @@
"is-regexp": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -48,9 +48,9 @@ Regex to clone.
#### options

Type: `Object`<br>
Properties: [`source`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source) [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) [`ignoreCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase) [`multiline`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) [`sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) [`unicode`](http://norbertlindenberg.com/2012/05/ecmascript-supplementary-characters/#RegExp)
Properties: [`source`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source) [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) [`ignoreCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase) [`multiline`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) [`dotAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll) [`sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) [`unicode`](http://norbertlindenberg.com/2012/05/ecmascript-supplementary-characters/#RegExp) [`lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex)

Optionally modify the cloned RegExp instance.
Optionally modify the cloned `RegExp` instance.


## License
Expand Down
24 changes: 12 additions & 12 deletions test.js
@@ -1,10 +1,10 @@
import test from 'ava';
import m from '.';
import cloneRegexp from '.';

test('clone and modify RegExp', t => {
t.is(m(/a/, {multiline: true}).toString(), '/a/m');
t.is(m(/a/gi, {source: 'b', global: false}).toString(), '/b/i');
t.is(m(/a/, {
t.is(cloneRegexp(/a/, {multiline: true}).toString(), '/a/m');
t.is(cloneRegexp(/a/gi, {source: 'b', global: false}).toString(), '/b/i');
t.is(cloneRegexp(/a/, {
source: 'b',
global: true,
ignoreCase: true,
Expand All @@ -13,23 +13,23 @@ test('clone and modify RegExp', t => {
});

test('lastIndex is copied by default', t => {
const duckRe = /duck/g;
const duckRegex = /duck/g;

// Mutate duckRe by running 'test'
duckRe.test('duck duck goose');
duckRegex.test('duck duck goose');

const clonedDuckRe = m(duckRe);
const clonedDuckRegex = cloneRegexp(duckRegex);

t.is(clonedDuckRe.lastIndex, duckRe.lastIndex);
t.is(clonedDuckRegex.lastIndex, duckRegex.lastIndex);
});

test('lastIndex can be configured via the options object', t => {
const duckRe = /duck/g;
const duckRegex = /duck/g;

// Mutate duckRe by running 'test'
duckRe.test('duck duck goose');
duckRegex.test('duck duck goose');

const clonedDuckRe = m(duckRe, {lastIndex: 0});
const clonedDuckRegex = cloneRegexp(duckRegex, {lastIndex: 0});

t.is(clonedDuckRe.lastIndex, 0);
t.is(clonedDuckRegex.lastIndex, 0);
});

0 comments on commit 9a562ff

Please sign in to comment.