pax_global_header00006660000000000000000000000064131522744520014517gustar00rootroot0000000000000052 comment=e5ced08e45d2cc2d286a9e7b5a574963f6577712 regex-cache-0.4.4/000077500000000000000000000000001315227445200136775ustar00rootroot00000000000000regex-cache-0.4.4/.editorconfig000066400000000000000000000004051315227445200163530ustar00rootroot00000000000000root = true [*] indent_style = space end_of_line = lf charset = utf-8 indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true [{**/{actual,fixtures,expected,templates}/**,*.md}] trim_trailing_whitespace = false insert_final_newline = falseregex-cache-0.4.4/.eslintrc.json000066400000000000000000000067751315227445200165120ustar00rootroot00000000000000{ "env": { "browser": false, "es6": true, "node": true, "mocha": true }, "globals": { "document": false, "navigator": false, "window": false }, "rules": { "accessor-pairs": 2, "arrow-spacing": [2, { "before": true, "after": true }], "block-spacing": [2, "always"], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "comma-dangle": [2, "never"], "comma-spacing": [2, { "before": false, "after": true }], "comma-style": [2, "last"], "constructor-super": 2, "curly": [2, "multi-line"], "dot-location": [2, "property"], "eol-last": 2, "eqeqeq": [2, "allow-null"], "generator-star-spacing": [2, { "before": true, "after": true }], "handle-callback-err": [2, "^(err|error)$" ], "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": [2, { "before": true, "after": true }], "new-cap": [2, { "newIsCap": true, "capIsNew": false }], "new-parens": 2, "no-array-constructor": 2, "no-caller": 2, "no-class-assign": 2, "no-cond-assign": 2, "no-const-assign": 2, "no-control-regex": 2, "no-debugger": 2, "no-delete-var": 2, "no-dupe-args": 2, "no-dupe-class-members": 2, "no-dupe-keys": 2, "no-duplicate-case": 2, "no-empty-character-class": 2, "no-eval": 2, "no-ex-assign": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-extra-boolean-cast": 2, "no-extra-parens": [2, "functions"], "no-fallthrough": 2, "no-floating-decimal": 2, "no-func-assign": 2, "no-implied-eval": 2, "no-inner-declarations": [2, "functions"], "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-iterator": 2, "no-label-var": 2, "no-labels": 2, "no-lone-blocks": 2, "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": 2, "no-multi-str": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-native-reassign": 0, "no-negated-in-lhs": 2, "no-new": 2, "no-new-func": 2, "no-new-object": 2, "no-new-require": 2, "no-new-wrappers": 2, "no-obj-calls": 2, "no-octal": 2, "no-octal-escape": 2, "no-proto": 0, "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 2, "no-self-compare": 2, "no-sequences": 2, "no-shadow-restricted-names": 2, "no-spaced-func": 2, "no-sparse-arrays": 2, "no-this-before-super": 2, "no-throw-literal": 2, "no-trailing-spaces": 0, "no-undef": 2, "no-undef-init": 2, "no-unexpected-multiline": 2, "no-unneeded-ternary": [2, { "defaultAssignment": false }], "no-unreachable": 2, "no-unused-vars": [2, { "vars": "all", "args": "none" }], "no-useless-call": 0, "no-with": 2, "one-var": [0, { "initialized": "never" }], "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], "padded-blocks": [0, "never"], "quotes": [2, "single", "avoid-escape"], "radix": 2, "semi": [2, "always"], "semi-spacing": [2, { "before": false, "after": true }], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, "never"], "space-in-parens": [2, "never"], "space-infix-ops": 2, "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], "use-isnan": 2, "valid-typeof": 2, "wrap-iife": [2, "any"], "yoda": [2, "never"] } } regex-cache-0.4.4/.gitattributes000066400000000000000000000000171315227445200165700ustar00rootroot00000000000000*.* text eol=lfregex-cache-0.4.4/.gitignore000066400000000000000000000004401315227445200156650ustar00rootroot00000000000000# always ignore files *.DS_Store *.sublime-* # test related, or directories generated by tests test/actual actual coverage .nyc* # npm node_modules npm-debug.log # yarn yarn.lock yarn-error.log # misc _gh_pages _draft _drafts bower_components vendor temp tmp TODO.md package-lock.jsonregex-cache-0.4.4/.travis.yml000066400000000000000000000002041315227445200160040ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '8' - '7' - '6' - '5' - '4' - '0.12' - '0.10' regex-cache-0.4.4/.verb.md000066400000000000000000000073041315227445200152410ustar00rootroot00000000000000- Read [what this does](#what-this-does). - See [the benchmarks](#benchmarks) ## Usage Wrap a function like this: ```js var cache = require('{%= name %}'); var someRegex = cache(require('some-regex-lib')); ``` **Caching a regex** If you want to cache a regex after calling `new RegExp()`, or you're requiring a module that returns a regex, wrap it with a function first: ```js var cache = require('{%= name %}'); function yourRegex(str, opts) { // do stuff to str and opts return new RegExp(str, opts.flags); } var regex = cache(yourRegex); ``` ## Recommendations ### Use this when... * **No options are passed** to the function that creates the regex. Regardless of how big or small the regex is, when zero options are passed, caching will be faster than not. * **A few options are passed**, and the values are primitives. The limited benchmarks I did show that caching is beneficial when up to 8 or 9 options are passed. ### Do not use this when... * **The values of options are not primitives**. When non-primitives must be compared for equality, the time to compare the options is most likely as long or longer than the time to just create a new regex. ### Example benchmarks Performance results, with and without regex-cache: ```bash # no args passed (defaults) with-cache x 8,699,231 ops/sec ±0.86% (93 runs sampled) without-cache x 2,777,551 ops/sec ±0.63% (95 runs sampled) # string and six options passed with-cache x 1,885,934 ops/sec ±0.80% (93 runs sampled) without-cache x 1,256,893 ops/sec ±0.65% (97 runs sampled) # string only with-cache x 7,723,256 ops/sec ±0.87% (92 runs sampled) without-cache x 2,303,060 ops/sec ±0.47% (99 runs sampled) # one option passed with-cache x 4,179,877 ops/sec ±0.53% (100 runs sampled) without-cache x 2,198,422 ops/sec ±0.47% (95 runs sampled) # two options passed with-cache x 3,256,222 ops/sec ±0.51% (99 runs sampled) without-cache x 2,121,401 ops/sec ±0.79% (97 runs sampled) # six options passed with-cache x 1,816,018 ops/sec ±1.08% (96 runs sampled) without-cache x 1,157,176 ops/sec ±0.53% (100 runs sampled) # # diminishing returns happen about here # # ten options passed with-cache x 1,210,598 ops/sec ±0.56% (92 runs sampled) without-cache x 1,665,588 ops/sec ±1.07% (100 runs sampled) # twelve options passed with-cache x 1,042,096 ops/sec ±0.68% (92 runs sampled) without-cache x 1,389,414 ops/sec ±0.68% (97 runs sampled) # twenty options passed with-cache x 661,125 ops/sec ±0.80% (93 runs sampled) without-cache x 1,208,757 ops/sec ±0.65% (97 runs sampled) # # when non-primitive values are compared # # single value on the options is an object with-cache x 1,398,313 ops/sec ±1.05% (95 runs sampled) without-cache x 2,228,281 ops/sec ±0.56% (99 runs sampled) ``` ## Run benchmarks Install dev dependencies: ```bash npm i -d && npm run benchmarks ``` ## What this does If you're using `new RegExp('foo')` instead of a regex literal, it's probably because you need to dyamically generate a regex based on user options or some other potentially changing factors. When your function creates a string based on user inputs and passes it to the `RegExp` constructor, regex-cache caches the results. The next time the function is called if the key of a cached regex matches the user input (or no input was given), the cached regex is returned, avoiding unnecessary runtime compilation. Using the RegExp constructor offers a lot of flexibility, but the runtime compilation comes at a price - it's slow. Not specifically because of the call to the RegExp constructor, but **because you have to build up the string before `new RegExp()` is even called**. [mentions-regex]: https://github.com/regexps/mentions-regex regex-cache-0.4.4/LICENSE000066400000000000000000000020771315227445200147120ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2017, Jon Schlinkert. 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 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.regex-cache-0.4.4/README.md000066400000000000000000000136631315227445200151670ustar00rootroot00000000000000# regex-cache [![NPM version](https://img.shields.io/npm/v/regex-cache.svg?style=flat)](https://www.npmjs.com/package/regex-cache) [![NPM monthly downloads](https://img.shields.io/npm/dm/regex-cache.svg?style=flat)](https://npmjs.org/package/regex-cache) [![NPM total downloads](https://img.shields.io/npm/dt/regex-cache.svg?style=flat)](https://npmjs.org/package/regex-cache) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/regex-cache.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/regex-cache) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/regex-cache.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/regex-cache) > Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save regex-cache ``` * Read [what this does](#what-this-does). * See [the benchmarks](#benchmarks) ## Usage Wrap a function like this: ```js var cache = require('regex-cache'); var someRegex = cache(require('some-regex-lib')); ``` **Caching a regex** If you want to cache a regex after calling `new RegExp()`, or you're requiring a module that returns a regex, wrap it with a function first: ```js var cache = require('regex-cache'); function yourRegex(str, opts) { // do stuff to str and opts return new RegExp(str, opts.flags); } var regex = cache(yourRegex); ``` ## Recommendations ### Use this when... * **No options are passed** to the function that creates the regex. Regardless of how big or small the regex is, when zero options are passed, caching will be faster than not. * **A few options are passed**, and the values are primitives. The limited benchmarks I did show that caching is beneficial when up to 8 or 9 options are passed. ### Do not use this when... * **The values of options are not primitives**. When non-primitives must be compared for equality, the time to compare the options is most likely as long or longer than the time to just create a new regex. ### Example benchmarks Performance results, with and without regex-cache: ```bash # no args passed (defaults) with-cache x 8,699,231 ops/sec ±0.86% (93 runs sampled) without-cache x 2,777,551 ops/sec ±0.63% (95 runs sampled) # string and six options passed with-cache x 1,885,934 ops/sec ±0.80% (93 runs sampled) without-cache x 1,256,893 ops/sec ±0.65% (97 runs sampled) # string only with-cache x 7,723,256 ops/sec ±0.87% (92 runs sampled) without-cache x 2,303,060 ops/sec ±0.47% (99 runs sampled) # one option passed with-cache x 4,179,877 ops/sec ±0.53% (100 runs sampled) without-cache x 2,198,422 ops/sec ±0.47% (95 runs sampled) # two options passed with-cache x 3,256,222 ops/sec ±0.51% (99 runs sampled) without-cache x 2,121,401 ops/sec ±0.79% (97 runs sampled) # six options passed with-cache x 1,816,018 ops/sec ±1.08% (96 runs sampled) without-cache x 1,157,176 ops/sec ±0.53% (100 runs sampled) # # diminishing returns happen about here # # ten options passed with-cache x 1,210,598 ops/sec ±0.56% (92 runs sampled) without-cache x 1,665,588 ops/sec ±1.07% (100 runs sampled) # twelve options passed with-cache x 1,042,096 ops/sec ±0.68% (92 runs sampled) without-cache x 1,389,414 ops/sec ±0.68% (97 runs sampled) # twenty options passed with-cache x 661,125 ops/sec ±0.80% (93 runs sampled) without-cache x 1,208,757 ops/sec ±0.65% (97 runs sampled) # # when non-primitive values are compared # # single value on the options is an object with-cache x 1,398,313 ops/sec ±1.05% (95 runs sampled) without-cache x 2,228,281 ops/sec ±0.56% (99 runs sampled) ``` ## Run benchmarks Install dev dependencies: ```bash npm i -d && npm run benchmarks ``` ## What this does If you're using `new RegExp('foo')` instead of a regex literal, it's probably because you need to dyamically generate a regex based on user options or some other potentially changing factors. When your function creates a string based on user inputs and passes it to the `RegExp` constructor, regex-cache caches the results. The next time the function is called if the key of a cached regex matches the user input (or no input was given), the cached regex is returned, avoiding unnecessary runtime compilation. Using the RegExp constructor offers a lot of flexibility, but the runtime compilation comes at a price - it's slow. Not specifically because of the call to the RegExp constructor, but **because you have to build up the string before `new RegExp()` is even called**. ## About ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### Contributors | **Commits** | **Contributor** | | --- | --- | | 31 | [jonschlinkert](https://github.com/jonschlinkert) | | 1 | [MartinKolarik](https://github.com/MartinKolarik) | ### Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` ### Running tests Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 01, 2017._regex-cache-0.4.4/appveyor.yml000066400000000000000000000012111315227445200162620ustar00rootroot00000000000000# Test against this version of Node.js environment: matrix: # node.js - nodejs_version: "8.0" - nodejs_version: "7.0" - nodejs_version: "6.0" - nodejs_version: "5.0" - nodejs_version: "4.0" - nodejs_version: "0.12" - nodejs_version: "0.10" # Install scripts. (runs after repo cloning) install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules - npm install # Post-install test scripts. test_script: # Output useful info for debugging. - node --version - npm --version # run tests - npm test # Don't actually build. build: off regex-cache-0.4.4/benchmark/000077500000000000000000000000001315227445200156315ustar00rootroot00000000000000regex-cache-0.4.4/benchmark/check.js000066400000000000000000000012651315227445200172500ustar00rootroot00000000000000'use strict'; var fs = require('fs'); var path = require('path'); var mm = require('micromatch'); var bold = require('ansi-bold'); /** * Sanity check. run to ensure that all fns return a correct * result. */ fs.readdirSync(__dirname + '/code').forEach(function (fp) { if (mm.isMatch(fp, '*.js')) { var fn = require(path.resolve(__dirname, 'code', fp)); var name = path.basename(fp, path.extname(fp)); fs.readdirSync(__dirname + '/fixtures').forEach(function (fixture) { if (mm.isMatch(fixture, '*.js')) { fixture = path.resolve(__dirname, 'fixtures', fixture); console.log(bold(name) + ':', fn.apply(null, require(fixture))); } }); } }); regex-cache-0.4.4/benchmark/code/000077500000000000000000000000001315227445200165435ustar00rootroot00000000000000regex-cache-0.4.4/benchmark/code/with-cache.js000077500000000000000000000003051315227445200211160ustar00rootroot00000000000000'use strict'; var cache = require('../..'); var fn = require('../../support.js'); module.exports = function(str, opts) { return cache(fn.toRegex, str, opts).exec('a abc foo xyz fooabc ab'); }; regex-cache-0.4.4/benchmark/code/without-cache.js000077500000000000000000000002401315227445200216440ustar00rootroot00000000000000'use strict'; var fn = require('../../support.js'); module.exports = function(str, opts) { return fn.toRegex(str, opts).exec('a abc foo xyz fooabc ab'); }; regex-cache-0.4.4/benchmark/fixtures/000077500000000000000000000000001315227445200175025ustar00rootroot00000000000000regex-cache-0.4.4/benchmark/fixtures/no-args-(defaults).js000066400000000000000000000000251315227445200233310ustar00rootroot00000000000000module.exports = []; regex-cache-0.4.4/benchmark/fixtures/options-nested.js000066400000000000000000000000461315227445200230130ustar00rootroot00000000000000module.exports = [{ d: {e: 'abc'} }]; regex-cache-0.4.4/benchmark/fixtures/options-one.js000066400000000000000000000000401315227445200223040ustar00rootroot00000000000000module.exports = [{ a: true }]; regex-cache-0.4.4/benchmark/fixtures/options-six.js000066400000000000000000000001151315227445200223310ustar00rootroot00000000000000module.exports = [{ a: true, b: true, c: true, x: true, y: true, z: true }]; regex-cache-0.4.4/benchmark/fixtures/options-ten.js000066400000000000000000000001621315227445200223160ustar00rootroot00000000000000module.exports = [{ a: true, b: true, c: true, d: true, f: true, g: true, h: true, i: true, j: true, k: true } ]; regex-cache-0.4.4/benchmark/fixtures/options-twelve.js000066400000000000000000000002661315227445200230430ustar00rootroot00000000000000module.exports = [ { a: true, b: true, c: true, e: true, f: true, g: true, h: true, i: true, j: true, k: true, l: true, m: true, } ]; regex-cache-0.4.4/benchmark/fixtures/options-twenty.js000066400000000000000000000004361315227445200230660ustar00rootroot00000000000000module.exports = [ { a: true, b: true, c: true, e: true, f: true, g: true, h: true, i: true, j: true, k: true, l: true, m: true, n: true, o: true, p: true, q: true, r: true, s: true, t: true, u: true, } ]; regex-cache-0.4.4/benchmark/fixtures/options-two.js000066400000000000000000000000511315227445200223360ustar00rootroot00000000000000module.exports = [{ a: true, b: true }]; regex-cache-0.4.4/benchmark/fixtures/string-and-options.js000066400000000000000000000001241315227445200235740ustar00rootroot00000000000000module.exports = ['foo', { a: true, b: true, c: true, x: true, y: true, y: true }]; regex-cache-0.4.4/benchmark/fixtures/string.js000066400000000000000000000000321315227445200213410ustar00rootroot00000000000000module.exports = ['foo']; regex-cache-0.4.4/benchmark/index.js000077500000000000000000000003071315227445200173010ustar00rootroot00000000000000'use strict'; var Suite = require('benchmarked'); var suite = new Suite({ result: false, fixtures: 'fixtures/*{six,ten,twelve,twenty}.js', add: 'code/*.js', cwd: __dirname }); suite.run(); regex-cache-0.4.4/benchmark/last.md000066400000000000000000000000001315227445200171040ustar00rootroot00000000000000regex-cache-0.4.4/index.js000066400000000000000000000024071315227445200153470ustar00rootroot00000000000000/*! * regex-cache * * Copyright (c) 2015-2017, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; var equal = require('is-equal-shallow'); var basic = {}; var cache = {}; /** * Expose `regexCache` */ module.exports = regexCache; /** * Memoize the results of a call to the new RegExp constructor. * * @param {Function} fn [description] * @param {String} str [description] * @param {Options} options [description] * @param {Boolean} nocompare [description] * @return {RegExp} */ function regexCache(fn, str, opts) { var key = '_default_', regex, cached; if (!str && !opts) { if (typeof fn !== 'function') { return fn; } return basic[key] || (basic[key] = fn(str)); } var isString = typeof str === 'string'; if (isString) { if (!opts) { return basic[str] || (basic[str] = fn(str)); } key = str; } else { opts = str; } cached = cache[key]; if (cached && equal(cached.opts, opts)) { return cached.regex; } memo(key, opts, (regex = fn(str, opts))); return regex; } function memo(key, opts, regex) { cache[key] = {regex: regex, opts: opts}; } /** * Expose `cache` */ module.exports.cache = cache; module.exports.basic = basic; regex-cache-0.4.4/package.json000066400000000000000000000026361315227445200161740ustar00rootroot00000000000000{ "name": "regex-cache", "description": "Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.", "version": "0.4.4", "homepage": "https://github.com/jonschlinkert/regex-cache", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "contributors": [ "Jon Schlinkert (http://twitter.com/jonschlinkert)", "Martin Kolárik (https://kolarik.sk)" ], "repository": "jonschlinkert/regex-cache", "bugs": { "url": "https://github.com/jonschlinkert/regex-cache/issues" }, "license": "MIT", "files": [ "index.js" ], "main": "index.js", "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha", "benchmarks": "node benchmark" }, "dependencies": { "is-equal-shallow": "^0.1.3" }, "devDependencies": { "ansi-bold": "^0.1.1", "benchmarked": "^0.1.5", "gulp-format-md": "^0.1.7", "micromatch": "^2.3.7", "should": "^8.3.0" }, "keywords": [ "cache", "expression", "regex", "regexp", "regular", "regular expression", "store", "to-regex" ], "verb": { "run": true, "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "reflinks": [ "verb" ], "lint": { "reflinks": true } } } regex-cache-0.4.4/support.js000066400000000000000000000021311315227445200157460ustar00rootroot00000000000000'use strict'; exports.toRegex = function toRegex(str, opts) { if (typeof str !== 'string') { opts = str; str = ''; } opts = opts || {}; var re = str || ''; if (opts.a) re += 'a'; if (opts.b) re += 'b'; if (opts.c) re += 'c'; if (opts.d && opts.d.e) re += opts.d.e; if (opts.e) re += 'e'; if (opts.f) re += 'f'; if (opts.g) re += 'g'; if (opts.h) re += 'h'; if (opts.i) re += 'i'; if (opts.j) re += 'j'; if (opts.k) re += 'k'; if (opts.l) re += 'l'; if (opts.m) re += 'm'; if (opts.n) re += 'n'; if (opts.o) re += 'o'; if (opts.p) re += 'p'; if (opts.q) re += 'q'; if (opts.r) re += 'r'; if (opts.s) re += 's'; if (opts.t) re += 't'; if (opts.x) re += 'x'; if (opts.y) re += 'y'; if (opts.z) re += 'z'; var f = opts.flags || ''; re = re ? '(' + re + ')' : '([^\W]?)'; // filler for some other logic if (re.indexOf('x') !== -1) { re = re.replace(/x/g, ''); } if (re.indexOf('y') !== -1) { re = re.replace(/y/g, ''); } if (re.indexOf('z') !== -1) { re = re.replace(/z/g, ''); } return new RegExp(re, f); }; regex-cache-0.4.4/test.js000066400000000000000000000071351315227445200152220ustar00rootroot00000000000000/*! * regex-cache * * Copyright (c) 2015 Jon Schlinkert. * Licensed under the MIT license. */ 'use strict'; var assert = require('assert'); var should = require('should'); var cache = require('./'); var fn = require('./support'); var str = 'a abc foo xyz fooabc'; describe('regex-cache:', function() { describe('no cache:', function() { it('should use the un-cached regex to match:', function() { var matches = str.match(fn.toRegex()); matches[1].should.equal('a'); }); }); describe('when a regex is passed:', function() { it('should return it directly:', function() { cache(fn.toRegex()).should.eql(/([^W]?)/); }); }); describe('when a function is passed:', function() { it('should call the function when no other arguments are passed:', function() { str.match(cache(fn.toRegex))[1].should.equal('a'); }); it('should cache the results on the `_default_` key:', function() { str.match(cache(fn.toRegex))[1].should.equal('a'); cache.basic.should.have.property('_default_'); }); }); describe('with cache:', function() { it('should use the cached regex to match:', function() { var cached = cache(fn.toRegex); var a = str.match(cached); var b = str.match(cached); var c = str.match(cached); var d = str.match(cached); var e = str.match(cached); e[1].should.equal('a'); cache.basic.should.have.property('_default_'); }); it('should work when a regex is passed:', function() { str.match(cache(fn.toRegex()))[1].should.equal('a'); cache.basic.should.have.property('_default_'); }); it('should work when a function is passed:', function() { str.match(cache(fn.toRegex))[1].should.equal('a'); cache.basic.should.have.property('_default_'); }); it('should work when options are passed:', function() { str.match(cache(fn.toRegex()))[1].should.equal('a'); str.match(cache(fn.toRegex, 'foo'))[1].should.equal('foo'); assert.equal(str.match(cache(fn.toRegex, 'bar')), null); str.match(cache(fn.toRegex, 'foo', { a: true, b: true, c: true }))[1].should.equal('fooabc'); str.match(cache(fn.toRegex, { a: true, b: true, c: true, flags: 'g'}))[0].should.equal('abc'); }); it('should work when an option is an object:', function() { str.match(cache(fn.toRegex, 'foo', { d: {e: 'abc'} }))[0].should.equal('fooabc'); str.match(cache(fn.toRegex, 'foo', { d: {e: 'ab'} }))[0].should.equal('fooab'); str.match(cache(fn.toRegex, 'foo', { d: {f: 'abc'} }))[0].should.equal('foo'); }); it('should not use a cached regex when options have changed:', function() { str.match(cache(fn.toRegex, { a: true, b: true, c: true }))[1].should.equal('abc'); // change `a` to false str.match(cache(fn.toRegex, { a: false, b: true, c: true }))[1].should.equal('bc'); str.match(cache(fn.toRegex, { a: false, b: true, c: true }))[1].should.not.equal('abc'); }); }); describe('cache object:', function() { it('should expose the cache:', function() { cache(fn.toRegex, 'foo'); cache.basic.should.have.property('foo'); cache(fn.toRegex, 'bar', { a: true, b: true, c: true }); cache.cache.should.have.property('bar'); // cache(fn.toRegex, { a: true, b: true, c: true }); // // cache.cache.should.have.property('atruebtruectrue'); // cache(fn.toRegex, { a: true, b: true, c: true, flags: 'g'}); // cache.cache.should.have.property('atruebtruectrueflagsg'); // cache.cache.should.have.property('foo'); }); }); });