Comparing version 0.0.3 to 0.1.1
@@ -10,5 +10,3 @@ /* | ||
var _ = require("lodash"), | ||
ReSpec = require('./respec.js')(Re), | ||
EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require("util"); | ||
ReSpec = require('./respec.js')(Re); | ||
Re.registered = { | ||
@@ -44,8 +42,2 @@ hspace: /[ \t\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]/, | ||
*/ | ||
Re.defaultOptions = { | ||
eventEmitter: { | ||
wildcard: true, | ||
maxListeners: 100 | ||
} | ||
}; | ||
function genguid() { | ||
@@ -67,3 +59,2 @@ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
EventEmitter2.call(this, this.opt.eventEmitter); | ||
this._spec = new Re.Spec(namespace, args); | ||
@@ -74,3 +65,2 @@ this._regexn = null; | ||
Re.Spec = ReSpec; | ||
util.inherits(Re, EventEmitter2); | ||
Re.register = function (identifier) { | ||
@@ -138,25 +128,17 @@ if (!_.isString(identifier)) { | ||
startRe = string[this._id] || 0, | ||
matches = [], found = false, | ||
that = this, resolveMatch; | ||
resolveMatch = function (match, i, lastIndex, noMatch) { | ||
that._spec.resolveMatch(match, i).forEach(function (res) { | ||
var hasStrings = _.some(res[1], _.isString), | ||
match = hasStrings ? res[1].concat(lastIndex) : res[1]; | ||
if (!noMatch && res[0] && hasStrings) | ||
that.emit(res[0], res[0], match); | ||
matches.push(match); | ||
}); | ||
}; | ||
_.forEach(regexn, function(re, i) { | ||
if (i < startRe || found) { | ||
resolveMatch([undefined], i, 0, true); | ||
return; | ||
} | ||
found = false, | ||
that = this; | ||
_.forEach(regexn.slice(startRe), function(re, i) { | ||
var match = re.exec(string); | ||
if (match) { | ||
string[that._id] = i; | ||
resolveMatch(match, i, re.lastIndex); | ||
found = true; | ||
} else { | ||
resolveMatch([undefined], i, 0, true); | ||
_.forEach(that._spec.resolveMatch(match, i), function (res) { | ||
if (_.some(res[0], _.isString)) { | ||
found = res.concat(re.lastIndex); | ||
return false; | ||
} | ||
}); | ||
if (found) { | ||
string[that._id] = i; | ||
return false; | ||
} | ||
} | ||
@@ -167,3 +149,3 @@ }); | ||
delete string[this._id]; | ||
return found ? matches : false; | ||
return found; | ||
}; | ||
@@ -170,0 +152,0 @@ Re.prototype.test = function (string) { |
@@ -102,6 +102,6 @@ /* | ||
if (last && ReMix.canJoin(last[0], re)) { | ||
lastMatch.push([namespace, parenCnt]); | ||
lastMatch.push([namespace, parenCnt, idx]); | ||
last.push(re); | ||
} else { | ||
matches.push([[namespace, parenCnt]]); | ||
matches.push([[namespace, parenCnt, idx]]); | ||
regexn.push([re]); | ||
@@ -118,3 +118,2 @@ } | ||
matches.shift(); // full match | ||
var lastSet = _.last(this._matches[index]); | ||
@@ -126,5 +125,6 @@ if (matches.length < lastSet[1] + 1) | ||
cnt = set[1], | ||
idx = set[2], | ||
match = matches.splice(0, cnt + 1); | ||
return [namespace, match];*/ | ||
return [set[0], matches.splice(0, set[1] + 1)]; | ||
return [match, namespace, idx];*/ | ||
return [matches.splice(0, set[1] + 1), set[0], set[2]]; | ||
}); | ||
@@ -131,0 +131,0 @@ }; |
{ | ||
"name": "remix", | ||
"version": "0.0.3", | ||
"version": "0.1.1", | ||
"description": "regular expression alternation for tokenizers", | ||
@@ -47,5 +47,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"eventemitter2": "^0.4.14", | ||
"lodash": "^2.4.1" | ||
} | ||
} |
@@ -28,11 +28,7 @@ remix | ||
var re = new Re('foo', /foo/, /bar/); | ||
re.on('foo', function (name, match) { | ||
/* 1st time ['foo', 3] */ | ||
/* 2nd time ['bar', 6] */ | ||
console.log(match); | ||
}); | ||
var str = 'foobar'; | ||
/* [['foo', 3], [undefined]] */ | ||
/* [['foo'], 'foo', 0, 3] */ | ||
console.log(re.exec(str)); | ||
/* [[undefined], ['bar', 6]] */ | ||
/* [['bar'], 'foo', 1, 6] */ | ||
console.log(re.exec(str)); | ||
@@ -48,20 +44,7 @@ | ||
var re = new Re('foo', {foo: /foo/, bar: /bar/}); | ||
re.on('foo.bar', function (name, match) { | ||
/* ['bar', 6] */ | ||
console.log(match); | ||
}); | ||
re.on('foo.foo', function (name, match) { | ||
/* ['foo', 3] */ | ||
console.log(match); | ||
}); | ||
re.on('foo.*', function (name, match) { | ||
/* 1st time ['foo', 3] */ | ||
/* 2nd time ['bar', 6] */ | ||
console.log(match); | ||
}); | ||
var str = 'foobar'; | ||
/* [['foo', 3], [undefined]] */ | ||
/* [['foo'], 'foo.foo', 0, 3] */ | ||
console.log(re.exec(str)); | ||
/* [[undefined], ['bar', 6]] */ | ||
/* [['bar'], 'foo.bar', 1, 6] */ | ||
console.log(re.exec(str)); | ||
@@ -68,0 +51,0 @@ |
@@ -55,3 +55,3 @@ /* | ||
it("should match simple", function () { | ||
this.re.add(/(foo)o/).exec('fooo').should.deep.equal([['fooo', 'foo', 4]]); | ||
this.re.add(/(foo)o/).exec('fooo').should.deep.equal([['fooo', 'foo'], '', 0, 4]); | ||
}); | ||
@@ -61,4 +61,4 @@ it("should match multiple", function () { | ||
this.re.add(/(foo)/, /bar/); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo', 3], [undefined]]); | ||
this.re.exec(str).should.deep.equal([[undefined, undefined], ['bar', 6]]); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo'], '', 0, 3]); | ||
this.re.exec(str).should.deep.equal([['bar'], '', 1, 6]); | ||
this.re._regexn.should.deep.equal([/((foo))|(bar)/g]); | ||
@@ -69,4 +69,4 @@ }); | ||
this.re.add(/(foo)o/, /(bar)r/i); | ||
this.re.exec(str).should.deep.equal([['fooo', 'foo', 4], [undefined, undefined]]); | ||
this.re.exec(str).should.deep.equal([[undefined, undefined], ['Barr', 'Bar', 8]]); | ||
this.re.exec(str).should.deep.equal([['fooo', 'foo'], '', 0, 4]); | ||
this.re.exec(str).should.deep.equal([['Barr', 'Bar'], '', 1, 8]); | ||
this.re._regexn.should.deep.equal([/((foo)o)/g, /((bar)r)/ig]); | ||
@@ -80,11 +80,6 @@ }); | ||
}); | ||
it("should emit named match", function (done) { | ||
it("should return named match", function () { | ||
var str = 'foo'; | ||
this.re.add({name: 'foo.foo', re: /foo/}); | ||
this.re.on('foo.*', function (name, match) { | ||
name.should.equal('foo.foo'); | ||
match.should.deep.equal(['foo', 3]); | ||
done(); | ||
}); | ||
this.re.exec(str); | ||
this.re.exec(str).should.deep.equal([['foo'], 'foo.foo', 0, 3]); | ||
}); | ||
@@ -107,6 +102,6 @@ it("should not match", function () { | ||
this.re.add(/(foo)/); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo', 3]]); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo'], '', 0, 3]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo', 3]]); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo', 9]]); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo'], '', 0, 3]); | ||
this.re.exec(str).should.deep.equal([['foo', 'foo'], '', 0, 9]); | ||
}); | ||
@@ -116,6 +111,6 @@ it("should reject length", function () { | ||
this.re.add(/(ooo|rrr)/); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo', 4]]); | ||
this.re.exec(str).should.deep.equal([['rrr', 'rrr', 11]]); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo'], '', 0, 4]); | ||
this.re.exec(str).should.deep.equal([['rrr', 'rrr'], '', 0, 11]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo', 4]]); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo'], '', 0, 4]); | ||
}); | ||
@@ -125,8 +120,8 @@ it("should reject multi", function () { | ||
this.re.add(/(ooo)/, /rrr/); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo'], '', 0, 4]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([[undefined, undefined],['rrr', 11]]); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo'], '', 0, 4]); | ||
this.re.exec(str).should.deep.equal([['rrr'], '', 1, 11]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([['ooo', 'ooo'], '', 0, 4]); | ||
}); | ||
@@ -136,24 +131,14 @@ it("should reject multi no-join", function () { | ||
this.re.add(/ooo/, /rrr/i); | ||
this.re.exec(str).should.deep.equal([['ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([['ooo'], '', 0, 4]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([[undefined], ['rrr', 11]]); | ||
this.re.exec(str).should.deep.equal([['ooo'], '', 0, 4]); | ||
this.re.exec(str).should.deep.equal([['rrr'], '', 1, 11]); | ||
this.re.reject(str); | ||
this.re.exec(str).should.deep.equal([['ooo', 4], [undefined]]); | ||
this.re.exec(str).should.deep.equal([['ooo'], '', 0, 4]); | ||
}); | ||
it("should allow named regex", function (done) { | ||
var str = 'foobar', cnt = 0; | ||
it("should allow named regex", function () { | ||
var str = 'foobar'; | ||
this.re.add({name: "foo", re: new lib.ReMix(/foo/, /bar/)}); | ||
this.re.on('foo', function (name, match) { | ||
name.should.equal("foo"); | ||
cnt++; | ||
if (cnt === 1) { | ||
match.should.deep.equal(['foo', 3]); | ||
} else { | ||
match.should.deep.equal(['bar', 6]); | ||
done(); | ||
} | ||
}); | ||
this.re.exec(str); | ||
this.re.exec(str); | ||
this.re.exec(str).should.deep.equal([['foo'], 'foo', 0, 3]); | ||
this.re.exec(str).should.deep.equal([['bar'], 'foo', 1, 6]); | ||
}); | ||
@@ -160,0 +145,0 @@ }); |
1
21921
522
73
- Removedeventemitter2@^0.4.14
- Removedeventemitter2@0.4.14(transitive)