[Pkg-javascript-commits] [node-regex-cache] 01/04: Import Upstream version 0.4.3

Sruthi Chandran srud-guest at moszumanska.debian.org
Wed Nov 2 10:55:07 UTC 2016


This is an automated email from the git hooks/post-receive script.

srud-guest pushed a commit to branch master
in repository node-regex-cache.

commit 03c073fef4b4d01f4eb9e0b847bebc1e22cca468
Author: Sruthi <srud at disroot.org>
Date:   Wed Nov 2 15:13:26 2016 +0530

    Import Upstream version 0.4.3
---
 .editorconfig                            |  22 +++
 .eslintrc.json                           | 275 +++++++++++++++++++++++++++++++
 .gitattributes                           |   1 +
 .gitignore                               |  12 ++
 .travis.yml                              |  12 ++
 .verb.md                                 | 112 +++++++++++++
 LICENSE                                  |  21 +++
 README.md                                | 160 ++++++++++++++++++
 benchmark/check.js                       |  25 +++
 benchmark/code/with-cache.js             |   8 +
 benchmark/code/without-cache.js          |   7 +
 benchmark/fixtures/no-args-(defaults).js |   1 +
 benchmark/fixtures/options-nested.js     |   1 +
 benchmark/fixtures/options-one.js        |   1 +
 benchmark/fixtures/options-six.js        |   1 +
 benchmark/fixtures/options-ten.js        |   1 +
 benchmark/fixtures/options-twelve.js     |  15 ++
 benchmark/fixtures/options-twenty.js     |  23 +++
 benchmark/fixtures/options-two.js        |   1 +
 benchmark/fixtures/string-and-options.js |   1 +
 benchmark/fixtures/string.js             |   1 +
 benchmark/index.js                       |  11 ++
 benchmark/last.md                        |   0
 index.js                                 |  69 ++++++++
 package.json                             |  61 +++++++
 support.js                               |  54 ++++++
 test.js                                  | 102 ++++++++++++
 27 files changed, 998 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..408d870
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,22 @@
+# 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
diff --git a/.eslintrc.json b/.eslintrc.json
new file mode 100644
index 0000000..5b78706
--- /dev/null
+++ b/.eslintrc.json
@@ -0,0 +1,275 @@
+{
+  "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"
+    ]
+  }
+}
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..a52bd18
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.* text eol=lf
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5c8a697
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,12 @@
+*.DS_store
+*.sublime-*
+_gh_pages
+bower_components
+node_modules
+npm-debug.log
+actual
+test/actual
+temp
+tmp
+TODO.md
+vendor
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..56d5697
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,12 @@
+sudo: false
+language: node_js
+node_js:
+  - stable
+  - '5'
+  - '4'
+  - '0.12'
+  - '0.10'
+matrix:
+  fast_finish: true
+  allow_failures:
+    - node_js: '0.10'
diff --git a/.verb.md b/.verb.md
new file mode 100644
index 0000000..72e1e20
--- /dev/null
+++ b/.verb.md
@@ -0,0 +1,112 @@
+- 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
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..1e49edf
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ab19174
--- /dev/null
+++ b/README.md
@@ -0,0 +1,160 @@
+# 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._
\ No newline at end of file
diff --git a/benchmark/check.js b/benchmark/check.js
new file mode 100644
index 0000000..d6fb859
--- /dev/null
+++ b/benchmark/check.js
@@ -0,0 +1,25 @@
+'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)));
+      }
+    });
+  }
+});
diff --git a/benchmark/code/with-cache.js b/benchmark/code/with-cache.js
new file mode 100755
index 0000000..7d65405
--- /dev/null
+++ b/benchmark/code/with-cache.js
@@ -0,0 +1,8 @@
+'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');
+};
diff --git a/benchmark/code/without-cache.js b/benchmark/code/without-cache.js
new file mode 100755
index 0000000..ea47913
--- /dev/null
+++ b/benchmark/code/without-cache.js
@@ -0,0 +1,7 @@
+'use strict';
+
+var fn = require('../../support.js');
+
+module.exports = function(str, opts) {
+  return fn.toRegex(str, opts).exec('a abc foo xyz fooabc ab');
+};
diff --git a/benchmark/fixtures/no-args-(defaults).js b/benchmark/fixtures/no-args-(defaults).js
new file mode 100644
index 0000000..e0a30c5
--- /dev/null
+++ b/benchmark/fixtures/no-args-(defaults).js
@@ -0,0 +1 @@
+module.exports = [];
diff --git a/benchmark/fixtures/options-nested.js b/benchmark/fixtures/options-nested.js
new file mode 100644
index 0000000..e3cc551
--- /dev/null
+++ b/benchmark/fixtures/options-nested.js
@@ -0,0 +1 @@
+module.exports = [{ d: {e: 'abc'} }];
diff --git a/benchmark/fixtures/options-one.js b/benchmark/fixtures/options-one.js
new file mode 100644
index 0000000..3a607af
--- /dev/null
+++ b/benchmark/fixtures/options-one.js
@@ -0,0 +1 @@
+module.exports = [{ a: true }];
diff --git a/benchmark/fixtures/options-six.js b/benchmark/fixtures/options-six.js
new file mode 100644
index 0000000..81b142a
--- /dev/null
+++ b/benchmark/fixtures/options-six.js
@@ -0,0 +1 @@
+module.exports = [{ a: true, b: true, c: true, x: true, y: true, z: true }];
diff --git a/benchmark/fixtures/options-ten.js b/benchmark/fixtures/options-ten.js
new file mode 100644
index 0000000..528a5ff
--- /dev/null
+++ b/benchmark/fixtures/options-ten.js
@@ -0,0 +1 @@
+module.exports = [{ a: true, b: true, c: true, d: true, f: true, g: true, h: true, i: true, j: true, k: true } ];
diff --git a/benchmark/fixtures/options-twelve.js b/benchmark/fixtures/options-twelve.js
new file mode 100644
index 0000000..68dc9fc
--- /dev/null
+++ b/benchmark/fixtures/options-twelve.js
@@ -0,0 +1,15 @@
+module.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,
+  }
+];
diff --git a/benchmark/fixtures/options-twenty.js b/benchmark/fixtures/options-twenty.js
new file mode 100644
index 0000000..ffb0131
--- /dev/null
+++ b/benchmark/fixtures/options-twenty.js
@@ -0,0 +1,23 @@
+module.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,
+  }
+];
diff --git a/benchmark/fixtures/options-two.js b/benchmark/fixtures/options-two.js
new file mode 100644
index 0000000..264979a
--- /dev/null
+++ b/benchmark/fixtures/options-two.js
@@ -0,0 +1 @@
+module.exports = [{ a: true, b: true }];
diff --git a/benchmark/fixtures/string-and-options.js b/benchmark/fixtures/string-and-options.js
new file mode 100644
index 0000000..daed4f1
--- /dev/null
+++ b/benchmark/fixtures/string-and-options.js
@@ -0,0 +1 @@
+module.exports = ['foo', { a: true, b: true, c: true, x: true, y: true, y: true }];
diff --git a/benchmark/fixtures/string.js b/benchmark/fixtures/string.js
new file mode 100644
index 0000000..40a844e
--- /dev/null
+++ b/benchmark/fixtures/string.js
@@ -0,0 +1 @@
+module.exports = ['foo'];
diff --git a/benchmark/index.js b/benchmark/index.js
new file mode 100755
index 0000000..a6214f9
--- /dev/null
+++ b/benchmark/index.js
@@ -0,0 +1,11 @@
+'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();
diff --git a/benchmark/last.md b/benchmark/last.md
new file mode 100644
index 0000000..e69de29
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..13d2022
--- /dev/null
+++ b/index.js
@@ -0,0 +1,69 @@
+/*!
+ * regex-cache <https://github.com/jonschlinkert/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;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..9507d58
--- /dev/null
+++ b/package.json
@@ -0,0 +1,61 @@
+{
+  "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
+    }
+  }
+}
diff --git a/support.js b/support.js
new file mode 100644
index 0000000..aa00d32
--- /dev/null
+++ b/support.js
@@ -0,0 +1,54 @@
+'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);
+};
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..ae5b599
--- /dev/null
+++ b/test.js
@@ -0,0 +1,102 @@
+/*!
+ * regex-cache <https://github.com/jonschlinkert/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');
+    });
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-regex-cache.git



More information about the Pkg-javascript-commits mailing list