Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "xeger", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "More verbose and readable regular expressions", | ||
@@ -5,0 +5,0 @@ "main": "xeger.js", |
@@ -135,2 +135,4 @@ # Xeger | ||
See the "Chaining" section below for a different syntax that makes using `.to()` less clunky. | ||
### x.alphanumeric([options]) | ||
@@ -216,3 +218,3 @@ | ||
### options | ||
### Options | ||
@@ -236,1 +238,36 @@ You can pass in a few options to the above rule functions. | ||
``` | ||
### Chaining, and `this` | ||
Xeger offers a few alternative ways to construct a regex. | ||
#### Chaining | ||
Each rule function returns a copy of Xeger object, allowing you to chain rule calls. | ||
```javascript | ||
xeger(function (x) { | ||
x.any(function () { | ||
x.literal('A').to().literal('Z'); | ||
}); | ||
}); /* Returns /[A-Z]/ */ | ||
``` | ||
If you call `xeger()` without giving it a callback function, it will return a fresh Xeger object, allowing you to chain calls. In this case, call `.regex()` at the end to get the final RegExp object out of it. | ||
```javascript | ||
xeger().start().any(function () { | ||
x.literal('A').to().literal('Z'); | ||
}).regex(); /* Returns /[A-Z]/ */ | ||
``` | ||
#### this and @ | ||
Instead of relying on the `x` parameter given to each rule callback, you can use the `this` object. This is handy if using CoffeeScript (which has the `@` shorthand to represent `this.`): | ||
```coffeescript | ||
xeger -> | ||
@any -> | ||
@literal('a').to().literal('z') | ||
# Returns /[a-z]/ | ||
``` |
13
test.js
@@ -12,5 +12,6 @@ var xeger = require('./xeger'); | ||
r.newline(); | ||
r.whitespace(); | ||
r.end(); | ||
}); | ||
assert.equal(regex.toString(), '/^hello\\w\\d\\n$/'); | ||
assert.equal(regex.toString(), '/^hello\\w\\d\\n\\s$/'); | ||
}); | ||
@@ -111,2 +112,12 @@ | ||
describe('chaining', function () { | ||
var regex = xeger({ global: true }).start().literal('abc').any(function () { | ||
this.literal('A').to().literal('Z'); | ||
}).regex(); | ||
it('makes the expected regex', function () { | ||
assert.equal(regex.toString(), '/^abc[A-Z]/g'); | ||
}); | ||
}); | ||
it('parses a url', function () { | ||
@@ -113,0 +124,0 @@ var regex = xeger(function (r) { |
39
xeger.js
module.exports = function (cb, options) { | ||
if (typeof cb !== 'function') { | ||
options = cb; | ||
} | ||
var r = new Xeger(cb, options); | ||
return r.regex(); | ||
if (typeof cb === 'function') { | ||
return r.regex(); | ||
} else { | ||
return r; | ||
} | ||
}; | ||
@@ -22,3 +29,5 @@ | ||
} | ||
cb.call(this, this); | ||
if (typeof cb === 'function') { | ||
cb.call(this, this); | ||
} | ||
}; | ||
@@ -41,2 +50,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -47,2 +58,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -53,2 +66,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -59,2 +74,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -65,2 +82,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -70,2 +89,4 @@ | ||
this.add('^'); | ||
return this; | ||
}; | ||
@@ -75,2 +96,4 @@ | ||
this.add('$'); | ||
return this; | ||
}; | ||
@@ -80,2 +103,4 @@ | ||
this.add('-'); | ||
return this; | ||
}; | ||
@@ -96,2 +121,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -109,2 +136,4 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
@@ -120,6 +149,6 @@ | ||
this.addOptions(options); | ||
return this; | ||
}; | ||
/* Private */ | ||
Xeger.prototype.regex = function () { | ||
@@ -129,2 +158,4 @@ return new RegExp(this.regexStr, this.flags); | ||
/* Private */ | ||
Xeger.prototype.addOptions = function (options) { | ||
@@ -131,0 +162,0 @@ options = options || {}; |
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
15165
285
271