-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 09e890e
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changes | ||
|
||
## 0.0.1 / 2012-10-18 | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |