pax_global_header00006660000000000000000000000064126775413250014526gustar00rootroot0000000000000052 comment=06ce46bda29a19064a968bd5d2d5596440be05ca regex-cache-0.4.3/000077500000000000000000000000001267754132500137055ustar00rootroot00000000000000regex-cache-0.4.3/.editorconfig000066400000000000000000000006461267754132500163700ustar00rootroot00000000000000# http://editorconfig.org root = true [*] indent_style = space end_of_line = lf charset = utf-8 indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false insert_final_newline = false [**/{actual,fixtures,expected}/**] trim_trailing_whitespace = false insert_final_newline = false [**/templates/**] trim_trailing_whitespace = false insert_final_newline = false regex-cache-0.4.3/.eslintrc.json000066400000000000000000000111251267754132500165010ustar00rootroot00000000000000{ "ecmaFeatures": { "modules": true, "experimentalObjectRestSpread": true }, "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.3/.gitattributes000066400000000000000000000000171267754132500165760ustar00rootroot00000000000000*.* text eol=lfregex-cache-0.4.3/.gitignore000066400000000000000000000001671267754132500157010ustar00rootroot00000000000000*.DS_store *.sublime-* _gh_pages bower_components node_modules npm-debug.log actual test/actual temp tmp TODO.md vendorregex-cache-0.4.3/.travis.yml000066400000000000000000000002341267754132500160150ustar00rootroot00000000000000sudo: false language: node_js node_js: - stable - '5' - '4' - '0.12' - '0.10' matrix: fast_finish: true allow_failures: - node_js: '0.10' regex-cache-0.4.3/.verb.md000066400000000000000000000073041267754132500152470ustar00rootroot00000000000000- 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.3/LICENSE000066400000000000000000000021001267754132500147030ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2016, 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.3/README.md000066400000000000000000000123021267754132500151620ustar00rootroot00000000000000# regex-cache [![NPM version](https://img.shields.io/npm/v/regex-cache.svg?style=flat)](https://www.npmjs.com/package/regex-cache) [![NPM downloads](https://img.shields.io/npm/dm/regex-cache.svg?style=flat)](https://npmjs.org/package/regex-cache) [![Build Status](https://img.shields.io/travis/jonschlinkert/regex-cache.svg?style=flat)](https://travis-ci.org/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 suprising performance improvements. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install regex-cache --save ``` * 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**. ## Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/regex-cache/issues/new). ## Building docs Generate readme and API documentation with [verb](https://github.com/verbose/verb): ```sh $ npm install verb && npm run docs ``` Or, if [verb](https://github.com/verbose/verb) is installed globally: ```sh $ verb ``` ## Running tests Install dev dependencies: ```sh $ npm install -d && npm test ``` ## Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) ## License Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT license](https://github.com/jonschlinkert/regex-cache/blob/master/LICENSE). *** _This file was generated by [verb](https://github.com/verbose/verb), v, on April 01, 2016._regex-cache-0.4.3/benchmark/000077500000000000000000000000001267754132500156375ustar00rootroot00000000000000regex-cache-0.4.3/benchmark/check.js000066400000000000000000000012701267754132500172520ustar00rootroot00000000000000'use strict'; var fs = require('fs'); var chalk = require('chalk'); var path = require('path'); var mm = require('micromatch'); /** * 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(chalk.bold(name) + ':', fn.apply(null, require(fixture))); } }); } }); regex-cache-0.4.3/benchmark/code/000077500000000000000000000000001267754132500165515ustar00rootroot00000000000000regex-cache-0.4.3/benchmark/code/with-cache.js000077500000000000000000000003051267754132500211240ustar00rootroot00000000000000'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.3/benchmark/code/without-cache.js000077500000000000000000000002401267754132500216520ustar00rootroot00000000000000'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.3/benchmark/fixtures/000077500000000000000000000000001267754132500175105ustar00rootroot00000000000000regex-cache-0.4.3/benchmark/fixtures/no-args-(defaults).js000066400000000000000000000000251267754132500233370ustar00rootroot00000000000000module.exports = []; regex-cache-0.4.3/benchmark/fixtures/options-nested.js000066400000000000000000000000461267754132500230210ustar00rootroot00000000000000module.exports = [{ d: {e: 'abc'} }]; regex-cache-0.4.3/benchmark/fixtures/options-one.js000066400000000000000000000000401267754132500223120ustar00rootroot00000000000000module.exports = [{ a: true }]; regex-cache-0.4.3/benchmark/fixtures/options-six.js000066400000000000000000000001151267754132500223370ustar00rootroot00000000000000module.exports = [{ a: true, b: true, c: true, x: true, y: true, z: true }]; regex-cache-0.4.3/benchmark/fixtures/options-ten.js000066400000000000000000000001621267754132500223240ustar00rootroot00000000000000module.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.3/benchmark/fixtures/options-twelve.js000066400000000000000000000002661267754132500230510ustar00rootroot00000000000000module.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.3/benchmark/fixtures/options-twenty.js000066400000000000000000000004361267754132500230740ustar00rootroot00000000000000module.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.3/benchmark/fixtures/options-two.js000066400000000000000000000000511267754132500223440ustar00rootroot00000000000000module.exports = [{ a: true, b: true }]; regex-cache-0.4.3/benchmark/fixtures/string-and-options.js000066400000000000000000000001241267754132500236020ustar00rootroot00000000000000module.exports = ['foo', { a: true, b: true, c: true, x: true, y: true, y: true }]; regex-cache-0.4.3/benchmark/fixtures/string.js000066400000000000000000000000321267754132500213470ustar00rootroot00000000000000module.exports = ['foo']; regex-cache-0.4.3/benchmark/index.js000077500000000000000000000003071267754132500173070ustar00rootroot00000000000000'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.3/benchmark/last.md000066400000000000000000000000001267754132500171120ustar00rootroot00000000000000regex-cache-0.4.3/index.js000066400000000000000000000024541267754132500153570ustar00rootroot00000000000000/*! * regex-cache * * Copyright (c) 2015 Jon Schlinkert. * Licensed under the MIT license. */ 'use strict'; var isPrimitive = require('is-primitive'); 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.3/package.json000066400000000000000000000024721267754132500162000ustar00rootroot00000000000000{ "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 suprising performance improvements.", "version": "0.4.3", "homepage": "https://github.com/jonschlinkert/regex-cache", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "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", "is-primitive": "^2.0.0" }, "devDependencies": { "benchmarked": "^0.1.5", "chalk": "^1.1.3", "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.3/support.js000066400000000000000000000021311267754132500157540ustar00rootroot00000000000000'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.3/test.js000066400000000000000000000071351267754132500152300ustar00rootroot00000000000000/*! * 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'); }); }); });