From 9a562ff953767b1699c2bdfac6eb61815df2b8be Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Tue, 16 Apr 2019 17:21:42 +0000 Subject: [PATCH] Add TypeScript definition (#5) --- .gitattributes | 3 +- .travis.yml | 1 + index.d.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 10 +++---- index.test-d.ts | 14 +++++++++ package.json | 10 ++++--- readme.md | 4 +-- test.js | 24 +++++++-------- 8 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index e0cc348..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..9005e81 --- /dev/null +++ b/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; diff --git a/index.js b/index.js index e7c548d..24edae6 100644 --- a/index.js +++ b/index.js @@ -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; }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..77dee4f --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,14 @@ +import {expectType} from 'tsd'; +import cloneRegexp = require('.'); + +const regex = /[a-z]/gi; + +expectType(cloneRegexp(regex)); +expectType(cloneRegexp(regex, {source: 'unicorn'})); +expectType(cloneRegexp(regex, {global: false})); +expectType(cloneRegexp(regex, {ignoreCase: true})); +expectType(cloneRegexp(regex, {multiline: true})); +expectType(cloneRegexp(regex, {dotAll: true})); +expectType(cloneRegexp(regex, {sticky: true})); +expectType(cloneRegexp(regex, {unicode: true})); +expectType(cloneRegexp(regex, {lastIndex: 1})); diff --git a/package.json b/package.json index 8665f43..ef333cb 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "regexp", @@ -33,7 +34,8 @@ "is-regexp": "^2.0.0" }, "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 7215147..f749947 100644 --- a/readme.md +++ b/readme.md @@ -48,9 +48,9 @@ Regex to clone. #### options Type: `Object`
-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 diff --git a/test.js b/test.js index 98a403f..8c20961 100644 --- a/test.js +++ b/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, @@ -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); });