verbal-expressions
Advanced tools
Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "verbal-expressions", | ||
"version": "0.1.2", | ||
"description": "JavaScript Regular expressions made easy", | ||
@@ -5,0 +4,0 @@ "keywords": [ "regular expressions", "regex" ], |
@@ -1,11 +0,66 @@ | ||
module.exports = function(grunt) { | ||
module.exports = function gruntConfig(grunt) { | ||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
eslint: { | ||
options: { | ||
configFile: '.eslintrc', | ||
}, | ||
target: ['VerbalExpressions.js', 'test/tests.js'], | ||
}, | ||
qunit: { | ||
files: ['test/index.html'] | ||
} | ||
files: ['test/index.html'], | ||
}, | ||
copy: { | ||
build: { | ||
src: '<%= pkg.name %>.js', | ||
dest: 'dist/verbalexpressions.js', | ||
}, | ||
}, | ||
uglify: { | ||
options: { | ||
banner: '/*!\n' + | ||
'* <%= pkg.name %> JavaScript Library v<%= pkg.version %>\n' + | ||
'* <%= pkg.homepage %>\n' + | ||
'*\n' + | ||
'*\n' + | ||
'* Released under the <%= pkg.license.type %> license\n' + | ||
'* <%= pkg.license.url %>\n' + | ||
'*\n' + | ||
'* Date: <%= grunt.template.today("yyyy-mm-dd") %>\n' + | ||
'*\n' + | ||
'*/\n', | ||
sourceMap: true, | ||
}, | ||
dist: { | ||
files: { | ||
'dist/verbalexpressions.min.js': ['<%= pkg.name %>.js'], | ||
}, | ||
}, | ||
}, | ||
sourcemap_localize: { | ||
options: { | ||
localize_to: '..', | ||
}, | ||
build: { | ||
files: { | ||
src: ['dist/*.min.js.map'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
grunt.loadNpmTasks("grunt-contrib-qunit"); | ||
grunt.registerTask('test', 'qunit'); | ||
grunt.loadNpmTasks('grunt-contrib-copy'); | ||
grunt.loadNpmTasks('grunt-contrib-qunit'); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-eslint'); | ||
grunt.loadNpmTasks('grunt-sourcemap-localize'); | ||
grunt.registerTask('test', ['eslint', 'qunit']); | ||
grunt.registerTask('default', ['qunit']); | ||
grunt.registerTask('build', ['qunit', 'copy', 'uglify', 'sourcemap_localize']); | ||
}; |
{ | ||
"name": "verbal-expressions", | ||
"description": "JavaScript Regular expressions made easy", | ||
"version": "0.1.2", | ||
"keywords": [ "regular expressions", "regex" ], | ||
"devDependencies": { | ||
"grunt": "~0.4.2", | ||
"grunt-contrib-qunit": "~0.2.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/VerbalExpressions/JSVerbalExpressions.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/VerbalExpressions/JSVerbalExpressions/issues" | ||
}, | ||
"main": "VerbalExpressions.js", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
} | ||
"name": "verbal-expressions", | ||
"description": "JavaScript Regular expressions made easy", | ||
"version": "0.2.0", | ||
"keywords": [ | ||
"regular expressions", | ||
"regex" | ||
], | ||
"homepage": "https://github.com/VerbalExpressions/JSVerbalExpressions", | ||
"devDependencies": { | ||
"eslint": "^1.10.3", | ||
"eslint-config-airbnb": "^3.1.0", | ||
"grunt": "^0.4.2", | ||
"grunt-contrib-copy": "^0.8.2", | ||
"grunt-contrib-qunit": "^0.7.0", | ||
"grunt-contrib-uglify": "^0.11.0", | ||
"grunt-eslint": "^17.3.1", | ||
"grunt-sourcemap-localize": "^0.1.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/VerbalExpressions/JSVerbalExpressions.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/VerbalExpressions/JSVerbalExpressions/issues" | ||
}, | ||
"main": "VerbalExpressions.js", | ||
"license": { | ||
"type": "MIT", | ||
"url": "http://opensource.org/licenses/MIT" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
} | ||
} |
@@ -1,4 +0,6 @@ | ||
VerbalExpressions v0.1.2 | ||
VerbalExpressions v0.2.0 | ||
===================== | ||
[![Build Status](https://travis-ci.org/VerbalExpressions/JSVerbalExpressions.svg)](https://travis-ci.org/VerbalExpressions/JSVerbalExpressions) | ||
## JavaScript Regular Expressions made easy | ||
@@ -8,3 +10,3 @@ VerbalExpressions is a JavaScript library that helps to construct difficult regular expressions. | ||
## Other Implementations | ||
You can see an up to date list of all ports on [VerbalExpressions.github.io](http://VerbalExpressions.github.io). | ||
You can see an up to date list of all ports on [VerbalExpressions.github.io](http://VerbalExpressions.github.io). | ||
- [Ruby](https://github.com/ryan-endacott/verbal_expressions) | ||
@@ -17,4 +19,7 @@ - [C#](https://github.com/VerbalExpressions/CSharpVerbalExpressions) | ||
- [Haskell](https://github.com/VerbalExpressions/HaskellVerbalExpressions) | ||
- [Haxe](https://github.com/VerbalExpressions/HaxeVerbalExpressions) | ||
- [C++](https://github.com/VerbalExpressions/CppVerbalExpressions) | ||
- [Objective-C](https://github.com/VerbalExpressions/ObjectiveCVerbalExpressions) | ||
- [Perl](https://github.com/VerbalExpressions/PerlVerbalExpressions) | ||
- [Swift](https://github.com/VerbalExpressions/SwiftVerbalExpressions) | ||
@@ -26,4 +31,5 @@ If you would like to contribute another port (which would be awesome!), please open an issue specifying the language. A repo in the [VerbalExpressions organization](https://github.com/VerbalExpressions) will be created for it. Please don't open PRs for other languages against this repo. | ||
```HTML | ||
<script type="text/javascript" src="VerbalExpressions.js"></script> | ||
<script src="VerbalExpressions.js"></script> | ||
``` | ||
Or use the [jsDelivr CDN](http://www.jsdelivr.com/projects/jsverbalexpressions). | ||
### On the server (node.js) | ||
@@ -36,3 +42,3 @@ Install: | ||
```javascript | ||
var VerEx = require("verbal-expressions"); | ||
var VerEx = require('verbal-expressions'); | ||
``` | ||
@@ -42,6 +48,14 @@ | ||
$ grunt | ||
$ grunt | ||
(or) | ||
$ grunt test | ||
## Creating a minified version | ||
This will generate a minified version of VerbalExpressions.js (aptly named VerbalExpressions.min.js) in a _dist_ folder. | ||
$ grunt build | ||
A source map will also be created in the same folder, so you can use the original unminified source file (copied to _dist_ as well) for debugging purposes. | ||
## Examples | ||
@@ -56,18 +70,21 @@ | ||
var tester = VerEx() | ||
.startOfLine() | ||
.then( "http" ) | ||
.maybe( "s" ) | ||
.then( "://" ) | ||
.maybe( "www." ) | ||
.anythingBut( " " ) | ||
.endOfLine(); | ||
.startOfLine() | ||
.then('http') | ||
.maybe('s') | ||
.then('://') | ||
.maybe('www.') | ||
.anythingBut(' ') | ||
.endOfLine(); | ||
// Create an example URL | ||
var testMe = "https://www.google.com"; | ||
var testMe = 'https://www.google.com'; | ||
// Use RegExp object's native test() function | ||
if( tester.test( testMe ) ) alert( "We have a correct URL "); // This output will fire | ||
else alert( "The URL is incorrect" ); | ||
if (tester.test(testMe)) { | ||
alert('We have a correct URL '); // This output will fire} | ||
} else { | ||
alert('The URL is incorrect'); | ||
} | ||
console.log( tester ); // Ouputs the actual expression used: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/ | ||
console.log(tester); // Outputs the actual expression used: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/ | ||
``` | ||
@@ -79,11 +96,12 @@ | ||
// Create a test string | ||
var replaceMe = "Replace bird with a duck"; | ||
var replaceMe = 'Replace bird with a duck'; | ||
// Create an expression that seeks for word "bird" | ||
var expression = VerEx().find( "bird" ); | ||
var expression = VerEx().find('bird'); | ||
// Execute the expression like a normal RegExp object | ||
var result = expression.replace( replaceMe, "duck" ); | ||
var result = expression.replace(replaceMe, 'duck'); | ||
alert( result ); // Outputs "Replace duck with a duck" | ||
// Outputs "Replace duck with a duck" | ||
alert(result); | ||
``` | ||
@@ -94,4 +112,6 @@ | ||
```javascript | ||
var result = VerEx().find( "red" ).replace( "We have a red house", "blue" ); | ||
alert( result ); // Outputs "We have a blue house" | ||
var result = VerEx().find('red').replace('We have a red house', 'blue'); | ||
// Outputs "We have a blue house" | ||
alert(result); | ||
``` | ||
@@ -101,3 +121,3 @@ | ||
You can find the API documentation at the [wiki pages](https://github.com/jehna/VerbalExpressions/wiki). | ||
You can find the API documentation at the [wiki pages](https://github.com/VerbalExpressions/JSVerbalExpressions/wiki). | ||
@@ -111,9 +131,13 @@ ## A little word for a big help | ||
### Style guide | ||
The [Airbnb](https://github.com/airbnb/javascript) style guide is loosely used as a basis for creating clean and readable JavaScript code. | ||
Pull requests are warmly welcome! | ||
Check out these slide decks for handy Github & git tips: | ||
[Git and Github Secrets](http://zachholman.com/talk/git-github-secrets/) | ||
[More Git and Github Secrets](http://zachholman.com/talk/more-git-and-github-secrets/) | ||
- [Git and Github Secrets](http://zachholman.com/talk/git-github-secrets/) | ||
- [More Git and Github Secrets](http://zachholman.com/talk/more-git-and-github-secrets/) | ||
[ben-nadel]:http://www.bennadel.com/ | ||
[extending]:http://www.bennadel.com/blog/2292-Extending-JavaScript-Arrays-While-Keeping-Native-Bracket-Notation-Functionality.htm | ||
[extending]:http://www.bennadel.com/blog/2292-extending-javascript-arrays-while-keeping-native-bracket-notation-functionality.htm |
@@ -1,210 +0,219 @@ | ||
test( "something", function() { | ||
/* eslint-disable new-cap */ | ||
test('something', function somethingTest() { | ||
var testRegex = VerEx().something(); | ||
var testString; | ||
var testString = ''; | ||
testString = ""; | ||
ok( ! testRegex.test( testString ), "empty string doesn't have something" ); | ||
ok(!testRegex.test(testString), 'Empty string doesn\'t have something'); | ||
testRegex.lastIndex = 0; | ||
testString = "a"; | ||
ok( testRegex.test( testString ), "a is something" ); | ||
} ); | ||
testString = 'a'; | ||
ok(testRegex.test(testString), 'a is something'); | ||
}); | ||
test( "anything", function() { | ||
test('anything', function anythingTest() { | ||
var testRegex = VerEx().startOfLine().anything(); | ||
var testString = "what"; | ||
ok( testRegex.test( testString ), "Passes!" ); | ||
} ); | ||
var testString = 'what'; | ||
test( "anythingBut", function() { | ||
var testRegex = VerEx().startOfLine().anythingBut( "w" ); | ||
var testString = "what"; | ||
ok( testRegex.test( testString ), "starts with w" ); | ||
} ); | ||
ok(testRegex.test(testString), 'Contains anything'); | ||
}); | ||
test( "somethingBut", function() { | ||
var testRegex = VerEx().somethingBut("a"); | ||
var testString; | ||
test('anythingBut', function anythingButTest() { | ||
var testRegex = VerEx().startOfLine().anythingBut('w'); | ||
var testString = 'what'; | ||
testString = ""; | ||
ok( ! testRegex.test( testString ), "empty string doesn't have something" ); | ||
ok(testRegex.test(testString), 'Starts with a w'); | ||
}); | ||
test('somethingBut', function somethingButTest() { | ||
var testRegex = VerEx().somethingBut('a'); | ||
var testString = ''; | ||
ok(!testRegex.test(testString), 'Empty string doesn\'t have something'); | ||
testRegex.lastIndex = 0; | ||
testString = "b"; | ||
ok( testRegex.test( testString ), "doesn't start with a" ); | ||
testString = 'b'; | ||
ok(testRegex.test(testString), 'Doesn\'t start with an a'); | ||
testRegex.lastIndex = 0; | ||
testString = "a"; | ||
ok( ! testRegex.test( testString ), "starts with a" ); | ||
} ); | ||
testString = 'a'; | ||
ok(!testRegex.test(testString), 'Starts with an a'); | ||
}); | ||
test( "startOfLine", function() { | ||
var testRegex = VerEx().startOfLine().then( "a" ); | ||
var testString; | ||
test('startOfLine', function startOfLineTest() { | ||
var testRegex = VerEx().startOfLine().then('a'); | ||
var testString = 'a'; | ||
testString = "a"; | ||
ok( testRegex.test( testString ), "Starts with a" ); | ||
ok(testRegex.test(testString), 'Starts with an a'); | ||
testRegex.lastIndex = 0; | ||
testString = "ba"; | ||
ok( ! testRegex.test( testString ), "Doesn't start with a" ); | ||
} ); | ||
testString = 'ba'; | ||
ok(!testRegex.test(testString), 'Doesn\'t start with an a'); | ||
}); | ||
test( "endOfLine", function() { | ||
var testRegex = VerEx().find( "a" ).endOfLine(); | ||
var testString; | ||
test('endOfLine', function endOfLineTest() { | ||
var testRegex = VerEx().find('a').endOfLine(); | ||
var testString = 'a'; | ||
testString = "a"; | ||
ok( testRegex.test( testString ), "Ends with a" ); | ||
ok(testRegex.test(testString), 'Ends with an a'); | ||
testRegex.lastIndex = 0; | ||
testString = "ab"; | ||
ok( ! testRegex.test( testString ), "Doesn't end with a" ); | ||
} ); | ||
testString = 'ab'; | ||
ok(!testRegex.test(testString), 'Doesn\'t end with an a'); | ||
}); | ||
test( "maybe", function() { | ||
var testRegex = VerEx().startOfLine().then( "a" ).maybe( "b" ); | ||
var testString; | ||
test('maybe', function maybeTest() { | ||
var testRegex = VerEx().startOfLine().then('a').maybe('b'); | ||
var testString = 'acb'; | ||
testString = "acb"; | ||
ok( testRegex.test( testString ), "Maybe has a b after an a" ); | ||
ok(testRegex.test(testString), 'Maybe has a b after an a'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc"; | ||
ok( testRegex.test( testString ), "Maybe has a b after an a" ); | ||
} ); | ||
testString = 'abc'; | ||
ok(testRegex.test(testString), 'Maybe has a b after an a'); | ||
}); | ||
test( "anyOf", function() { | ||
var testRegex = VerEx().startOfLine().then( "a" ).anyOf( "xyz" ); | ||
var testString; | ||
test('anyOf', function anyOfTest() { | ||
var testRegex = VerEx().startOfLine().then('a').anyOf('xyz'); | ||
var testString = 'ay'; | ||
testString = "ay"; | ||
ok( testRegex.test( testString ), "Has an x, y, or z after a" ); | ||
ok(testRegex.test(testString), 'Has an x, y, or z after an a'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc"; | ||
ok( ! testRegex.test( testString ), "Doesn't have an x, y, or z after a" ); | ||
} ); | ||
testString = 'abc'; | ||
ok(!testRegex.test(testString), 'Doesn\'t have an x, y, or z after an a'); | ||
}); | ||
test( "or", function() { | ||
var testRegex = VerEx().startOfLine().then( "abc" ).or( "def" ); | ||
var testString; | ||
test('or', function orTest() { | ||
var testRegex = VerEx().startOfLine().then('abc').or('def'); | ||
var testString = 'defzzz'; | ||
testString = "defzzz"; | ||
ok( testRegex.test( testString ), "Starts with abc or def" ); | ||
ok(testRegex.test(testString), 'Starts with an abc or a def'); | ||
testRegex.lastIndex = 0; | ||
testString = "xyzabc"; | ||
ok( ! testRegex.test( testString ), "Doesn't start with abc or def" ); | ||
} ); | ||
testString = 'xyzabc'; | ||
ok(!testRegex.test(testString), 'Doesn\'t start with an abc or a def'); | ||
}); | ||
test( "lineBreak", function() { | ||
var testRegex; | ||
var testString; | ||
test('lineBreak', function lineBreakTest() { | ||
var testRegex = VerEx().startOfLine().then('abc').lineBreak().then('def'); | ||
var testString = 'abc\r\ndef'; | ||
testRegex = VerEx().startOfLine().then( "abc" ).lineBreak().then( "def" ); | ||
testString = "abc\r\ndef"; | ||
ok( testRegex.test( testString ), "abc then line break then def" ); | ||
ok(testRegex.test(testString), 'abc,then a line break and then def'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc\ndef"; | ||
ok( testRegex.test( testString ), "abc then line break then def" ); | ||
testString = 'abc\ndef'; | ||
ok(testRegex.test(testString), 'abc, then a line break and then def'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc\r\n def"; | ||
ok( ! testRegex.test( testString ), "abc then line break then space then def" ); | ||
} ); | ||
testString = 'abc\r\n def'; | ||
ok(!testRegex.test(testString), 'abc, then a line break, then a space and then def'); | ||
}); | ||
test( "br", function() { | ||
var testRegex; | ||
var testString; | ||
test('br', function brTest() { | ||
var testRegex = VerEx().startOfLine().then('abc').lineBreak().then('def'); | ||
var testString = 'abc\r\ndef'; | ||
testRegex = VerEx().startOfLine().then( "abc" ).lineBreak().then( "def" ); | ||
testString = "abc\r\ndef"; | ||
ok( testRegex.test( testString ), "abc then line break then def" ); | ||
ok(testRegex.test(testString), 'abc, then a line break and then def'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc\ndef"; | ||
ok( testRegex.test( testString ), "abc then line break then def" ); | ||
testString = 'abc\ndef'; | ||
ok(testRegex.test(testString), 'abc, then a line break and then def'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc\r\n def"; | ||
ok( ! testRegex.test( testString ), "abc then line break then space then def" ); | ||
} ); | ||
testString = 'abc\r\n def'; | ||
ok(!testRegex.test(testString), 'abc, then a line break, then a space and then def'); | ||
}); | ||
test( "tab", function() { | ||
var testRegex; | ||
var testString; | ||
test('tab', function tabTest() { | ||
var testRegex = VerEx().startOfLine().tab().then('abc'); | ||
var testString = '\tabc'; | ||
testRegex = VerEx().startOfLine().tab().then( "abc" ); | ||
testString = "\tabc"; | ||
ok( testRegex.test( testString ), "tab then abc" ); | ||
ok(testRegex.test(testString), 'tab then abc'); | ||
testRegex.lastIndex = 0; | ||
testString = "abc"; | ||
ok( ! testRegex.test( testString ), "no tab then abc" ); | ||
} ); | ||
testString = 'abc'; | ||
ok(!testRegex.test(testString), 'No tab then abc'); | ||
}); | ||
test( "withAnyCase", function() { | ||
var testRegex; | ||
var testString; | ||
test('withAnyCase', function withAnyCaseTest() { | ||
var testRegex = VerEx().startOfLine().then('a'); | ||
var testString = 'A'; | ||
testRegex = VerEx().startOfLine().then( "a" ); | ||
testString = "A"; | ||
ok( ! testRegex.test( testString ), "not case insensitive" ); | ||
ok(!testRegex.test(testString), 'Not case-insensitive'); | ||
testRegex = VerEx().startOfLine().then( "a" ).withAnyCase(); | ||
testString = "A"; | ||
ok( testRegex.test( testString ), "case insensitive" ); | ||
testRegex = VerEx().startOfLine().then('a').withAnyCase(); | ||
testString = 'A'; | ||
ok(testRegex.test(testString), 'Case-insensitive'); | ||
testRegex.lastIndex = 0; | ||
testString = "a"; | ||
ok( testRegex.test( testString ), "case insensitive" ); | ||
} ); | ||
testString = 'a'; | ||
ok(testRegex.test(testString), 'Case-insensitive'); | ||
}); | ||
test( "searchOneLine", function() { | ||
var testRegex; | ||
var testString; | ||
test('whitespace', function whitespaceTest() { | ||
var testRegex = VerEx().startOfLine().then('a').whitespace().then('z'); | ||
var testString = 'a z'; | ||
testRegex = VerEx().startOfLine().then( "a" ).br().then( "b" ).endOfLine(); | ||
testString = "a\nb"; | ||
ok( testRegex.test( testString ), "b is on the second line" ); | ||
ok(testRegex.test(testString), 'a, then a space and then z'); | ||
testRegex = VerEx().startOfLine().then( "a" ).br().then( "b" ).endOfLine().searchOneLine(); | ||
testString = "a\nb"; | ||
ok( testRegex.test( testString ), "b is on the second line but we are only searching the first" ); | ||
} ); | ||
testRegex.lastIndex = 0; | ||
testString = 'a_z'; | ||
ok(!testRegex.test(testString), 'a, then no whitespace and then z'); | ||
}); | ||
test( "sanitize", function() { | ||
var testRegex; | ||
var testString; | ||
test('searchOneLine', function searchOneLineTest() { | ||
var testRegex = VerEx().startOfLine().then('a').br().then('b').endOfLine(); | ||
var testString = 'a\nb'; | ||
testRegex = VerEx().startOfLine().then( "$a^b\\c|d(e)f[g]h{i}j.k*l+m?" ).endOfLine(); | ||
testString = "$a^b\\c|d(e)f[g]h{i}j.k*l+m?"; | ||
ok( testRegex.test( testString ), "special characters sanitization" ); | ||
} ); | ||
ok(testRegex.test(testString), 'b is on the second line'); | ||
test( "multiple", function() { | ||
var testRegex, testString; | ||
testRegex = VerEx().startOfLine().then('a').br().then('b').endOfLine().searchOneLine(); | ||
testString = 'a\nb'; | ||
ok(testRegex.test(testString), 'b is on the second line, but we are only searching the first'); | ||
}); | ||
testRegex = VerEx().startOfLine().multiple("foo"); | ||
testString = "foo"; | ||
ok( testRegex.test( testString ), "contains 'foo' atleast once "); | ||
test('sanitize', function sanitizeTest() { | ||
var testRegex = VerEx().startOfLine().then('$a^b\\c|d(e)f[g]h{i}j.k*l+m?').endOfLine(); | ||
var testString = '$a^b\\c|d(e)f[g]h{i}j.k*l+m?'; | ||
testRegex = VerEx().startOfLine().multiple("foo", 2); | ||
testString = "foo"; | ||
ok( ! testRegex.test( testString ), " should contains 'foo' atleast twice" ); | ||
ok(testRegex.test(testString), 'Special character sanitization'); | ||
}); | ||
testRegex = VerEx().startOfLine().multiple("foo", 2); | ||
testString = "foofoo"; | ||
ok( testRegex.test( testString ), " should contains 'foo' atleast twice" ); | ||
test('oneOrMore', function oneOrMoreTest() { | ||
var testRegex = VerEx().startOfLine().then('foo').oneOrMore(); | ||
var testString = 'foo'; | ||
testRegex = VerEx().startOfLine().multiple("foo", 2, 5); | ||
testString = "foofoofoofoo"; | ||
ok( testRegex.test( testString ), " should contains 'foo' repeated two to five times" ); | ||
ok(testRegex.test(testString), 'Contains \'foo\' at least once '); | ||
testRegex = VerEx().startOfLine().multiple("foo", 2, 5); | ||
testString = "foo"; | ||
ok( ! testRegex.test( testString ), " should contains 'foo' repeated two to five times" ); | ||
testRegex.lastIndex = 0; | ||
testString = 'foofoo'; | ||
ok(testRegex.test(testString), 'Contains \'foo\' at least once in \'foofoo\''); | ||
} ); | ||
testRegex.lastIndex = 0; | ||
testString = 'bar'; | ||
ok(!testRegex.test(testString), 'Contains \'foo\' at least once'); | ||
}); | ||
test('multiple', function multipleTest() { | ||
var testRegex = VerEx().startOfLine().multiple('foo'); | ||
var testString = 'foo'; | ||
ok(testRegex.test(testString), 'Contains \'foo\' at least once'); | ||
testRegex = VerEx().startOfLine().multiple('foo', 2); | ||
testString = 'foo'; | ||
ok(!testRegex.test(testString), 'Should contain \'foo\' at least twice'); | ||
testRegex = VerEx().startOfLine().multiple('foo', 2); | ||
testString = 'foofoo'; | ||
ok(testRegex.test(testString), 'Should contain \'foo\' at least twice'); | ||
testRegex = VerEx().startOfLine().multiple('foo', 2, 5); | ||
testString = 'foofoofoofoo'; | ||
ok(testRegex.test(testString), 'Should be \'foo\' repeated two to five times'); | ||
testRegex = VerEx().startOfLine().multiple('foo', 2, 5); | ||
testString = 'foo'; | ||
ok(!testRegex.test(testString), 'Should be \'foo\' repeated two to five times'); | ||
}); |
/*! | ||
* VerbalExpressions JavaScript Library v0.1.2 | ||
* VerbalExpressions JavaScript Library v0.2.0 | ||
* https://github.com/VerbalExpressions/JSVerbalExpressions | ||
@@ -7,3 +7,3 @@ * | ||
* Released under the MIT license | ||
* http://jquery.org/license | ||
* http://opensource.org/licenses/MIT | ||
* | ||
@@ -15,12 +15,12 @@ * Date: 2013-07-19 | ||
// Define the collection class. | ||
(function(){ | ||
(function verbalExpressionIIFE(root) { | ||
// Constants | ||
var MODULE_NAME = 'VerEx'; | ||
var root = this; | ||
// I am the constructor function. | ||
function VerbalExpression(){ | ||
function VerbalExpression() { | ||
var verbalExpression = new RegExp(); | ||
// Add all the class methods | ||
VerbalExpression.injectClassMethods( verbalExpression ); | ||
VerbalExpression.injectClassMethods(verbalExpression); | ||
@@ -31,21 +31,15 @@ // Return the new object. | ||
// Define the static methods. | ||
VerbalExpression.injectClassMethods = function( verbalExpression ){ | ||
VerbalExpression.injectClassMethods = function injectClassMethods(verbalExpression) { | ||
var method; | ||
// Loop over all the prototype methods | ||
for (var method in VerbalExpression.prototype){ | ||
for (method in VerbalExpression.prototype) { | ||
// Make sure this is a local method. | ||
if (VerbalExpression.prototype.hasOwnProperty( method )){ | ||
if (VerbalExpression.prototype.hasOwnProperty(method)) { | ||
// Add the method | ||
verbalExpression[ method ] = VerbalExpression.prototype[ method ]; | ||
verbalExpression[method] = VerbalExpression.prototype[method]; | ||
} | ||
} | ||
return verbalExpression; | ||
}; | ||
@@ -58,14 +52,25 @@ | ||
// expression construction in order | ||
_prefixes : "", | ||
_source : "", | ||
_suffixes : "", | ||
_modifiers : "gm", // default to global multiline matching | ||
_prefixes: '', | ||
_source: '', | ||
_suffixes: '', | ||
_modifiers: 'gm', // default to global multiline matching | ||
// Sanitation function for adding | ||
// anything safely to the expression | ||
sanitize : function( value ) { | ||
if(value.source) return value.source; | ||
if(typeof value === "number") return value; | ||
return value.replace(/[^\w]/g, function(character) { return "\\" + character; }); | ||
sanitize: function sanitize(value) { | ||
var reRegExpEscape; | ||
if (value.source) { | ||
return value.source; | ||
} | ||
if (typeof value === 'number') { | ||
return value; | ||
} | ||
// Regular expression meta characters, URL: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/regexp | ||
reRegExpEscape = /([\].|*?+(){}^$\\:=[])/g; | ||
// Escape RegExp special characters only | ||
// $& => Last match, URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch | ||
return value.replace(reRegExpEscape, '\\$&'); | ||
}, | ||
@@ -77,5 +82,6 @@ | ||
// be used. | ||
add: function( value ) { | ||
this._source += value || ""; | ||
add: function add(value) { | ||
this._source += value || ''; | ||
this.compile(this._prefixes + this._source + this._suffixes, this._modifiers); | ||
return this; | ||
@@ -85,13 +91,15 @@ }, | ||
// Start and end of line functions | ||
startOfLine: function( enable ) { | ||
enable = ( enable !== false ); | ||
this._prefixes = enable ? "^" : ""; | ||
this.add( "" ); | ||
startOfLine: function startOfLine(enable) { | ||
enable = (enable !== false); | ||
this._prefixes = enable ? '^' : ''; | ||
this.add(); | ||
return this; | ||
}, | ||
endOfLine : function( enable ) { | ||
enable = ( enable !== false ); | ||
this._suffixes = enable ? "$" : ""; | ||
this.add( "" ); | ||
endOfLine: function endOfLine(enable) { | ||
enable = (enable !== false); | ||
this._suffixes = enable ? '$' : ''; | ||
this.add(); | ||
return this; | ||
@@ -105,5 +113,6 @@ }, | ||
// naturally. | ||
then : function( value ) { | ||
value = this.sanitize( value ); | ||
this.add( "(?:" + value + ")" ); | ||
then: function then(value) { | ||
value = this.sanitize(value); | ||
this.add('(?:' + value + ')'); | ||
return this; | ||
@@ -116,10 +125,11 @@ }, | ||
// of the chain. | ||
find : function( value ) { | ||
return this.then( value ); | ||
find: function find(value) { | ||
return this.then(value); | ||
}, | ||
// Maybe is used to add values with ? | ||
maybe : function( value ) { | ||
maybe: function maybe(value) { | ||
value = this.sanitize(value); | ||
this.add( "(?:" + value + ")?" ); | ||
this.add('(?:' + value + ')?'); | ||
return this; | ||
@@ -129,4 +139,4 @@ }, | ||
// Any character any number of times | ||
anything : function() { | ||
this.add( "(?:.*)" ); | ||
anything: function anything() { | ||
this.add('(?:.*)'); | ||
return this; | ||
@@ -136,5 +146,6 @@ }, | ||
// Anything but these characters | ||
anythingBut : function( value ) { | ||
value = this.sanitize( value ); | ||
this.add( "(?:[^" + value + "]*)" ); | ||
anythingBut: function anythingBut(value) { | ||
value = this.sanitize(value); | ||
this.add('(?:[^' + value + ']*)'); | ||
return this; | ||
@@ -144,4 +155,4 @@ }, | ||
// Any character at least one time | ||
something : function() { | ||
this.add( "(?:.+)" ); | ||
something: function something() { | ||
this.add('(?:.+)'); | ||
return this; | ||
@@ -151,5 +162,6 @@ }, | ||
// Any character at least one time except for these characters | ||
somethingBut : function( value ) { | ||
value = this.sanitize( value ); | ||
this.add( "(?:[^" + value + "]+)" ); | ||
somethingBut: function somethingBut(value) { | ||
value = this.sanitize(value); | ||
this.add('(?:[^' + value + ']+)'); | ||
return this; | ||
@@ -163,8 +175,7 @@ }, | ||
// replacements on one regexp. | ||
replace : function( source, value ) { | ||
replace: function replace(source, value) { | ||
source = source.toString(); | ||
return source.replace( this, value ); | ||
return source.replace(this, value); | ||
}, | ||
/// Add regular expression special /// | ||
@@ -174,8 +185,9 @@ /// characters /// | ||
// Line break | ||
lineBreak : function() { | ||
this.add( "(?:\\r\\n|\\r|\\n)" ); // Unix + windows CLRF | ||
lineBreak: function lineBreak() { | ||
this.add('(?:\\r\\n|\\r|\\n)'); // Unix + Windows CRLF | ||
return this; | ||
}, | ||
// And a shorthand for html-minded | ||
br : function() { | ||
br: function br() { | ||
return this.lineBreak(); | ||
@@ -185,4 +197,4 @@ }, | ||
// Tab (duh?) | ||
tab : function() { | ||
this.add( "\\t" ); | ||
tab: function tab() { | ||
this.add('\\t'); | ||
return this; | ||
@@ -192,11 +204,17 @@ }, | ||
// Any alphanumeric | ||
word : function() { | ||
this.add( "\\w+" ); | ||
word: function word() { | ||
this.add('\\w+'); | ||
return this; | ||
}, | ||
// Any whitespace | ||
whitespace: function whitespace() { | ||
this.add('\\s'); | ||
return this; | ||
}, | ||
// Any given character | ||
anyOf : function( value ) { | ||
anyOf: function anyOf(value) { | ||
value = this.sanitize(value); | ||
this.add( "["+ value +"]" ); | ||
this.add('[' + value + ']'); | ||
return this; | ||
@@ -206,37 +224,49 @@ }, | ||
// Shorthand | ||
any : function( value ) { | ||
return this.anyOf( value ); | ||
any: function any(value) { | ||
return this.anyOf(value); | ||
}, | ||
// Usage: .range( from, to [, from, to ... ] ) | ||
range : function() { | ||
var value = "["; | ||
range: function range() { | ||
var length = arguments.length; | ||
for(var _to = 1; _to < arguments.length; _to += 2) { | ||
var from = this.sanitize( arguments[_to - 1] ); | ||
var to = this.sanitize( arguments[_to] ); | ||
// Create a string buffer instead of concatenating on iteration | ||
var buffer = new Array(length / 2); | ||
var index = 0; | ||
var i = 0; | ||
var from; | ||
var to; | ||
value += from + "-" + to; | ||
buffer[index++] = '['; | ||
while (i < length) { | ||
from = this.sanitize(arguments[i++]); | ||
to = this.sanitize(arguments[i++]); | ||
buffer[index++] = from + '-' + to; | ||
} | ||
value += "]"; | ||
buffer[index++] = ']'; | ||
this.add( value ); | ||
this.add(buffer.join('')); | ||
return this; | ||
}, | ||
/// Modifiers /// | ||
// Modifier abstraction | ||
addModifier : function( modifier ) { | ||
if( this._modifiers.indexOf( modifier ) == -1 ) { | ||
addModifier: function addModifier(modifier) { | ||
if (this._modifiers.indexOf(modifier) === -1) { | ||
this._modifiers += modifier; | ||
} | ||
this.add(""); | ||
this.add(); | ||
return this; | ||
}, | ||
removeModifier : function( modifier ) { | ||
this._modifiers = this._modifiers.replace( modifier, "" ); | ||
this.add(""); | ||
removeModifier: function removeModifier(modifier) { | ||
this._modifiers = this._modifiers.replace(modifier, ''); | ||
this.add(); | ||
return this; | ||
@@ -246,10 +276,12 @@ }, | ||
// Case-insensitivity modifier | ||
withAnyCase : function( enable ) { | ||
withAnyCase: function withAnyCase(enable) { | ||
if (enable !== false) { | ||
this.addModifier('i'); | ||
} else { | ||
this.removeModifier('i'); | ||
} | ||
if(enable !== false) this.addModifier( "i" ); | ||
else this.removeModifier( "i" ); | ||
this.add(); | ||
this.add( "" ); | ||
return this; | ||
}, | ||
@@ -260,21 +292,25 @@ | ||
// than other modifiers | ||
stopAtFirst : function( enable ) { | ||
stopAtFirst: function stopAtFirst(enable) { | ||
if (enable !== false) { | ||
this.removeModifier('g'); | ||
} else { | ||
this.addModifier('g'); | ||
} | ||
if(enable !== false) this.removeModifier( "g" ); | ||
else this.addModifier( "g" ); | ||
this.add(); | ||
this.add( "" ); | ||
return this; | ||
}, | ||
// Multiline, also reversed | ||
searchOneLine : function( enable ) { | ||
searchOneLine: function searchOneLine(enable) { | ||
if (enable !== false) { | ||
this.removeModifier('m'); | ||
} else { | ||
this.addModifier('m'); | ||
} | ||
if(enable !== false) this.removeModifier( "m" ); | ||
else this.addModifier( "m" ); | ||
this.add(); | ||
this.add( "" ); | ||
return this; | ||
}, | ||
@@ -285,38 +321,47 @@ | ||
// between n and m times. | ||
repeatPrevious: function( ) { | ||
repeatPrevious: function repeatPrevious() { | ||
var value; | ||
if(arguments.length <= 1) { | ||
if(/\d+/.exec(arguments[0]) !== null) { | ||
value = "{" + arguments[0] + "}"; | ||
var reIsInteger = /\d+/; | ||
var length = arguments.length; | ||
var values = new Array(length); | ||
var i = 0; | ||
var j = 0; | ||
for (i = 0; i < length; i++) { | ||
if (reIsInteger.test(arguments[i])) { | ||
values[j++] = arguments[i]; | ||
} | ||
} else { | ||
var values = []; | ||
for(var i=0; i< arguments.length; i++) { | ||
if(/\d+/.exec(arguments[i]) !== null) { | ||
values.push(arguments[i]); | ||
} | ||
} | ||
} | ||
value = "{" + values.join(",") + "}"; | ||
if (j > 0) { | ||
// Set the new length of the array, thus reducing to the elements that have content | ||
values.length = j; | ||
value = '{' + values.join(',') + '}'; | ||
} | ||
this.add( value || "" ); | ||
return ( this ); | ||
this.add(value); | ||
return (this); | ||
}, | ||
// Repeats the previous at least once | ||
oneOrMore: function oneOrMore() { | ||
this.add('+'); | ||
return (this); | ||
}, | ||
/// Loops /// | ||
multiple : function( value ) { | ||
multiple: function multiple(value) { | ||
// Use expression or string | ||
value = value.source ? value.source : this.sanitize(value); | ||
value = value.source || this.sanitize(value); | ||
if (arguments.length === 1) { | ||
this.add("(?:" + value + ")*"); | ||
this.add('(?:' + value + ')*'); | ||
} | ||
if (arguments.length > 1) { | ||
this.add("(?:" + value + ")"); | ||
this.add("{" + arguments[1] + "}"); | ||
this.add('(?:' + value + ')'); | ||
this.add('{' + arguments[1] + '}'); | ||
} | ||
return this; | ||
@@ -326,18 +371,19 @@ }, | ||
// Adds alternative expressions | ||
or : function( value ) { | ||
or: function or(value) { | ||
this._prefixes += '(?:'; | ||
this._suffixes = ')' + this._suffixes; | ||
this._prefixes += "(?:"; | ||
this._suffixes = ")" + this._suffixes; | ||
this.add(')|(?:'); | ||
if (value) { | ||
this.then(value); | ||
} | ||
this.add( ")|(?:" ); | ||
if(value) this.then( value ); | ||
return this; | ||
}, | ||
//starts a capturing group | ||
beginCapture : function() { | ||
//add the end of the capture group to the suffixes for now so compilation continues to work | ||
this._suffixes += ")"; | ||
this.add( "(", false ); | ||
// Starts a capturing group | ||
beginCapture: function beginCapture() { | ||
// Add the end of the capture group to the suffixes for now so compilation continues to work | ||
this._suffixes += ')'; | ||
this.add('('); | ||
@@ -347,7 +393,7 @@ return this; | ||
//ends a capturing group | ||
endCapture : function() { | ||
//remove the last parentheses from the _suffixes and add to the regex itself | ||
this._suffixes = this._suffixes.substring(0, this._suffixes.length - 1 ); | ||
this.add( ")", true ); | ||
// Ends a capturing group | ||
endCapture: function endCapture() { | ||
// Remove the last parentheses from the _suffixes and add to the regex itself | ||
this._suffixes = this._suffixes.substring(0, this._suffixes.length - 1); | ||
this.add(')'); | ||
@@ -357,8 +403,7 @@ return this; | ||
//convert to RegExp object | ||
toRegExp : function() { | ||
var arr = this.toString().match(/\/(.*)\/([a-z]+)?/); | ||
return new RegExp(arr[1],arr[2]); | ||
} | ||
// Convert to RegExp object | ||
toRegExp: function toRegExp() { | ||
var array = this.toString().match(/\/(.*)\/([gimuy]+)?/); | ||
return new RegExp(array[1], array[2]); | ||
}, | ||
}; | ||
@@ -370,13 +415,16 @@ | ||
// support both browser and node.js | ||
if (typeof module !== 'undefined' && module.exports) { | ||
// UMD (Universal Module Definition), URL: https://github.com/umdjs/umd | ||
// Supports AMD, CommonJS and the browser | ||
if (typeof root.module !== 'undefined' && root.module.exports) { | ||
// Node.js Module | ||
module.exports = createVerbalExpression; | ||
} else if (typeof root.define === 'function' && root.define.amd) { | ||
// AMD Module | ||
root.define(MODULE_NAME, [], function define() { | ||
return VerbalExpression; | ||
}); | ||
} else { | ||
// Browser | ||
root[MODULE_NAME] = createVerbalExpression; | ||
} | ||
else if (typeof define === 'function' && define.amd) { | ||
define(function(){ return VerbalExpression; }); | ||
} | ||
else { | ||
root.VerEx = createVerbalExpression; | ||
} | ||
}).call(); | ||
})(this); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
171174
16
4686
1
134
8