diff --git a/.editorconfig b/.editorconfig index 5740eca..4eb6462 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,6 @@ end_of_line = lf insert_final_newline = true indent_style = tab -[{package.json,yarn.lock}] +[package.json] indent_style = space indent_size = 2 diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..0964bbb --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +/coverage/ +/demo/lib/ +/demo/webpack.config.js +/lib/ diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..3995a40 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,31 @@ +{ + "parser": "@typescript-eslint/parser", + "extends": [ + "airbnb-base", + "plugin:jest/recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "prettier", + "prettier/@typescript-eslint" + ], + "parserOptions": { + "ecmaVersion": 2018 + }, + "rules": { + "import/no-extraneous-dependencies": [ + "error", + { + "devDependencies": ["**/*.spec.{js,ts}"] + } + ], + "import/prefer-default-export": "off" + }, + "settings": { + "import/parsers": { + "@typescript-eslint/parser": [".ts"] + }, + "import/resolver": { + "typescript": {} + } + } +} diff --git a/.gitignore b/.gitignore index 5f7e9aa..cad0a43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +package-lock.json + coverage/ +demo/lib/ lib/ node_modules/ -demo/demo.js 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/.publishrc b/.publishrc new file mode 100644 index 0000000..29754d3 --- /dev/null +++ b/.publishrc @@ -0,0 +1,15 @@ +{ + "validations": { + "vulnerableDependencies": false, + "uncommittedChanges": true, + "untrackedFiles": true, + "sensitiveData": true, + "branch": "master", + "gitTag": true + }, + "confirm": true, + "publishCommand": "npm publish", + "publishTag": "latest", + "prePublishScript": "npm run publish-please-prereqs", + "postPublishScript": false +} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e25d958 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - 'node' + - '8' +script: + - npm run travisci +cache: + directories: + - node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..bbd6892 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# strings-to-regex + +[](https://badge.fury.io/js/strings-to-regex) + + +[](https://travis-ci.org/wimpyprogrammer/strings-to-regex) +[](https://codecov.io/gh/wimpyprogrammer/strings-to-regex) +[](https://snyk.io/test/github/wimpyprogrammer/strings-to-regex) + +Generate a compact Regular Expression that matches a finite set. + +Have you ever seen a dense Regular Expression like this one to match the 50 US state abbreviations? + +```regexp +/(A(L|K|Z|R)|C(A|O|T)|DE|FL|GA|HI|I(D|L|N|A)|K(S|Y)|LA|M(E|D|A|I|N|S|O|T)|N(E|V|H|J|M|Y|C|D)|O(H|K|R)|PA|RI|S(C|D)|T(N|X)|UT|V(T|A)|W(A|V|I|Y))/ +``` + +This library generates patterns like that to match a list of strings you provide. + +_To reverse this process and list which strings a Regular Expression would match, try [`regex-to-strings`](https://www.npmjs.com/package/regex-to-strings)._ + +## Demo + +## API + +### `condense(arrayOfStrings)` + +Generate a Regular Expression to match all strings in `arrayOfStrings`. Respects the casing of the strings. Returns a [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) object. + +```js +import { condense } from 'strings-to-regex'; + +const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz']; +const matcher = condense(stringsToMatch); +console.log(matcher); // /(foo(|bar|BarBaz)|Foo)/ +``` + +--- + +### `condenseIgnoreCase(arrayOfStrings)` + +A variation of `condense()` that ignores the casing of the strings. Returns a [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) object. + +```js +import { condenseIgnoreCase } from 'strings-to-regex'; + +const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz']; +const matcher = condenseIgnoreCase(stringsToMatch); +console.log(matcher); // /foo(|bar(|baz))/i +``` diff --git a/demo/index.html b/demo/index.html index fa16873..8e15c81 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,117 +1,188 @@ - + - -
- - -