From ea980986c69f1cd8dee3b65597852c6eeaf79430 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Wed, 17 Apr 2019 07:33:59 +0000 Subject: [PATCH 1/3] Require Node.js 8, add TypeScript definition (#13) --- .editorconfig | 5 +--- .gitattributes | 2 +- .gitignore | 1 + .npmrc | 1 + .travis.yml | 6 ++-- index.d.ts | 16 ++++++++++ index.js | 8 ++--- index.test-d.ts | 4 +++ license | 20 +++---------- package.json | 80 +++++++++++++++++++++++++------------------------ readme.md | 8 ++--- test.js | 6 ++-- 12 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 8f9d77e..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,9 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 - -[*.md] -trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes index 176a458..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 52ba159..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ -sudo: false language: node_js node_js: - - 'stable' - - '0.12' - - '0.10' + - '10' + - '8' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..a8ecae3 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,16 @@ +/** +Escape RegExp special characters. + +@example +``` +import escapeStringRegexp = require('escape-string-regexp'); + +const escapedString = escapeStringRegexp('how much $ for a 🦄?'); +//=> 'how much \\$ for a 🦄\\?' + +new RegExp(escapedString); +``` +*/ +declare const escapeStringRegexp: (str: string) => string; + +export = escapeStringRegexp; diff --git a/index.js b/index.js index 7834bf9..08fc586 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ 'use strict'; -var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; +const matchOperatorsRegex = /[|\\{}()[\]^$+*?.]/g; -module.exports = function (str) { - if (typeof str !== 'string') { +module.exports = string => { + if (typeof string !== 'string') { throw new TypeError('Expected a string'); } - return str.replace(matchOperatorsRe, '\\$&'); + return string.replace(matchOperatorsRegex, '\\$&'); }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..2915492 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,4 @@ +import {expectType} from 'tsd'; +import escapeStringRegexp = require('.'); + +expectType(escapeStringRegexp('how much $ for a 🦄?')); diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (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: +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: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index f307df3..8df6ced 100644 --- a/package.json +++ b/package.json @@ -1,41 +1,43 @@ { - "name": "escape-string-regexp", - "version": "1.0.5", - "description": "Escape RegExp special characters", - "license": "MIT", - "repository": "sindresorhus/escape-string-regexp", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "maintainers": [ - "Sindre Sorhus (sindresorhus.com)", - "Joshua Boy Nicolai Appelman (jbna.nl)" - ], - "engines": { - "node": ">=0.8.0" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "escape", - "regex", - "regexp", - "re", - "regular", - "expression", - "string", - "str", - "special", - "characters" - ], - "devDependencies": { - "ava": "*", - "xo": "*" - } + "name": "escape-string-regexp", + "version": "1.0.5", + "description": "Escape RegExp special characters", + "license": "MIT", + "repository": "sindresorhus/escape-string-regexp", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "maintainers": [ + "Sindre Sorhus (sindresorhus.com)", + "Joshua Boy Nicolai Appelman (jbna.nl)" + ], + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "escape", + "regex", + "regexp", + "re", + "regular", + "expression", + "string", + "str", + "special", + "characters" + ], + "devDependencies": { + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index 87ac82d..375585f 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ ## Install ``` -$ npm install --save escape-string-regexp +$ npm install escape-string-regexp ``` @@ -15,8 +15,8 @@ $ npm install --save escape-string-regexp ```js const escapeStringRegexp = require('escape-string-regexp'); -const escapedString = escapeStringRegexp('how much $ for a unicorn?'); -//=> 'how much \$ for a unicorn\?' +const escapedString = escapeStringRegexp('how much $ for a 🦄?'); +//=> 'how much \\$ for a 🦄\\?' new RegExp(escapedString); ``` @@ -24,4 +24,4 @@ new RegExp(escapedString); ## License -MIT © [Sindre Sorhus](http://sindresorhus.com) +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index cda5ea4..2a22573 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,9 @@ import test from 'ava'; -import fn from './'; +import escapeStringRegexp from '.'; -test(t => { +test('main', t => { t.is( - fn('\\ ^ $ * + ? . ( ) | { } [ ]'), + escapeStringRegexp('\\ ^ $ * + ? . ( ) | { } [ ]'), '\\\\ \\^ \\$ \\* \\+ \\? \\. \\( \\) \\| \\{ \\} \\[ \\]' ); }); From e76291d337900dbfba3b2e04d71c6d718ec7199b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 17 Apr 2019 14:46:19 +0700 Subject: [PATCH 2/3] Also escape `-` This enables you to escape a string that is inserted into a regex, for example, into a character class. Fixes #9 --- index.d.ts | 8 +++++--- index.js | 2 +- readme.md | 6 ++++-- test.js | 7 +++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index a8ecae3..7d34edc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,16 +1,18 @@ /** Escape RegExp special characters. +You can also use this to escape a string that is inserted into the middle of a regex, for example, into a character class. + @example ``` import escapeStringRegexp = require('escape-string-regexp'); -const escapedString = escapeStringRegexp('how much $ for a 🦄?'); -//=> 'how much \\$ for a 🦄\\?' +const escapedString = escapeStringRegexp('How much $ for a 🦄?'); +//=> 'How much \\$ for a 🦄\\?' new RegExp(escapedString); ``` */ -declare const escapeStringRegexp: (str: string) => string; +declare const escapeStringRegexp: (string: string) => string; export = escapeStringRegexp; diff --git a/index.js b/index.js index 08fc586..58217a4 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -const matchOperatorsRegex = /[|\\{}()[\]^$+*?.]/g; +const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g; module.exports = string => { if (typeof string !== 'string') { diff --git a/readme.md b/readme.md index 375585f..157472b 100644 --- a/readme.md +++ b/readme.md @@ -15,12 +15,14 @@ $ npm install escape-string-regexp ```js const escapeStringRegexp = require('escape-string-regexp'); -const escapedString = escapeStringRegexp('how much $ for a 🦄?'); -//=> 'how much \\$ for a 🦄\\?' +const escapedString = escapeStringRegexp('How much $ for a 🦄?'); +//=> 'How much \\$ for a 🦄\\?' new RegExp(escapedString); ``` +You can also use this to escape a string that is inserted into the middle of a regex, for example, into a character class. + ## License diff --git a/test.js b/test.js index 2a22573..7fac094 100644 --- a/test.js +++ b/test.js @@ -7,3 +7,10 @@ test('main', t => { '\\\\ \\^ \\$ \\* \\+ \\? \\. \\( \\) \\| \\{ \\} \\[ \\]' ); }); + +test('escapes `-`', t => { + t.is( + escapeStringRegexp('foo - bar'), + 'foo \\- bar' + ); +}); From 21c557c7f14a112eebe196abd98d085f5fcfcf5e Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 17 Apr 2019 14:48:22 +0700 Subject: [PATCH 3/3] 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8df6ced..2e343cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "escape-string-regexp", - "version": "1.0.5", + "version": "2.0.0", "description": "Escape RegExp special characters", "license": "MIT", "repository": "sindresorhus/escape-string-regexp",