Skip to content

Commit

Permalink
Version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Oct 18, 2012
0 parents commit 09e890e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.4
- 0.5
- 0.6
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes

## 0.0.1 / 2012-10-18

- Initial release
18 changes: 18 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions pattern-exp.js
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions test/test-all.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 09e890e

Please sign in to comment.