Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 0.0.1

  • Loading branch information...
commit 09e890ef8451ea08976cc5dc144312105f476430 0 parents
Irakli Gozalishvili authored
5 .travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.5
+ - 0.6
5 History.md
@@ -0,0 +1,5 @@
+# Changes
+
+## 0.0.1 / 2012-10-18
+
+ - Initial release
18 License.md
@@ -0,0 +1,18 @@
+Copyright 2012 Irakli Gozalishvili. All rights reserved.
+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.
9 Readme.md
@@ -0,0 +1,9 @@
+# pattern-exp
+
+[![Build Status](https://secure.travis-ci.org/Gozala/pattern-exp.png)](http://travis-ci.org/Gozala/pattern-exp)
+
+Utility for regexp creation out of string patterns
+
+## Install
+
+ npm install pattern-exp
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "pattern-exp",
+ "id": "pattern-exp",
+ "version": "0.0.1",
+ "description": "Utility for regexp creation out of string patterns",
+ "keywords": [ "pattern-exp" ],
+ "author": "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)",
+ "homepage": "https://github.com/Gozala/pattern-exp",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/Gozala/pattern-exp.git",
+ "web": "https://github.com/Gozala/pattern-exp"
+ },
+ "bugs": {
+ "url": "http://github.com/Gozala/pattern-exp/issues/"
+ },
+ "devDependencies": {
+ "test": ">=0.4.4",
+ "repl-utils": ">=1.0.0"
+ },
+ "scripts": {
+ "test": "node test/test-all.js",
+ "repl": "node node_modules/repl-utils"
+ },
+ "licenses": [{
+ "type" : "MIT",
+ "url" : "https://github.com/Gozala/pattern-exp/License.md"
+ }]
+}
46 pattern-exp.js
@@ -0,0 +1,46 @@
+/* vim:set ts=2 sw=2 sts=2 expandtab */
+/*jshint asi: true undef: true es5: true node: true browser: true devel: true
+ forin: true latedef: false globalstrict: true*/
+
+"use strict";
+
+var stirgifier = Object.prototype.toString
+var ESCAPE_PATTERN = /[\.\?\*\+\^\$\|\(\)\{\[\]\\]/g
+
+function escape(pattern) {
+ /**
+ Returns the `pattern` with all regexp meta characters in it backslashed.
+ **/
+ return String(pattern).replace(ESCAPE_PATTERN, '\\$&')
+}
+escape.pattern = ESCAPE_PATTERN
+
+function Pattern(pattern, flags) {
+ /**
+ Function takes `pattern` string or regexp & optional flags string,
+ which is just regexp flags and returns instance of `RegExp` by actually
+ calling it. If pattern fails to compile it will escaped given pattern and
+ compile it to regexp after.
+
+ ## examples
+
+ RegExp("[") // => SyntaxError("unterminated character class")
+ RegExp(/:/, "y") // => TypeError("can't supply flags when ...")
+ Pattern("[") // => /\[/
+ Pattern(/:/, "y") // => /:/
+ **/
+ if (!pattern.exec) {
+ try {
+ pattern = RegExp(pattern, flags)
+ } catch (exception) {
+ if (exception instanceof SyntaxError)
+ pattern = RegExp(escape(pattern), flags)
+ else
+ throw exception
+ }
+ }
+ return pattern
+}
+Pattern.escape = escape
+
+module.exports = Pattern
18 test/test-all.js
@@ -0,0 +1,18 @@
+/* vim:set ts=2 sw=2 sts=2 expandtab */
+/*jshint asi: true undef: true es5: true node: true browser: true devel: true
+ forin: true latedef: false globalstrict: true*/
+
+"use strict";
+
+var pattern = require("../pattern-exp")
+
+exports["test pattern"] = function(assert) {
+ assert.deepEqual(pattern("["), /\[/, "pattern escapes brackets")
+ assert.deepEqual(pattern(/:/, "y"), /\:/, "pattern escapes")
+}
+
+exports["test pattern flags"] = function(assert) {
+ assert.deepEqual(pattern("[hello]", "g"), /\[hello\]/g, "flags work")
+}
+
+require("test").run(exports)
Please sign in to comment.
Something went wrong with that request. Please try again.