Skip to content

Commit 9040b8c

Browse files
committed
added test and fixed hasPrefix/hasSuffix bug in simplifySet, bump patch
1 parent 5955470 commit 9040b8c

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "regex-trigram",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "JavaScript port of portions of Google Code Search",
55
"author": "Bright Fulton",
66
"main": "lib/regex.js",

src/regex-query.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ var analyze = function(re) {
2222
//println("analyze", re.String())
2323
//defer func() { println("->", ret.String()) }()
2424
var info = new RegexInfo({});
25-
var i;
2625
switch (re.type) {
2726
case "NO_MATCH":
2827
return RegexInfo.noMatch();
@@ -690,7 +689,7 @@ RegexInfo.prototype.simplifySet = function(s) {
690689
// doesn't help at all to know that "abc" is also a possible
691690
// prefix, so delete "abc".
692691
w = 0;
693-
var f = s == this.suffix ? hasSuffix : hasPrefix;
692+
var f = isEqualTo(s, this.suffix) ? hasSuffix : hasPrefix;
694693
for (i = 0; i < t.length; i++) {
695694
str = t[i];
696695
if (w === 0 || !f(str, t[w - 1])) {
@@ -703,11 +702,11 @@ RegexInfo.prototype.simplifySet = function(s) {
703702
};
704703

705704
var hasPrefix = function(str, prefix) {
706-
return str.slice(0, prefix.length) == str;
705+
return str.slice(0, prefix.length) == prefix;
707706
};
708707

709708
var hasSuffix = function(str, suffix) {
710-
return str.slice(-suffix.length) == str;
709+
return str.slice(-suffix.length) == suffix;
711710
};
712711

713712
var suffixComparator = function(a, b) {
@@ -762,8 +761,7 @@ var cross = function(s, t, isSuffix) {
762761
p.push(ss + t[j]);
763762
}
764763
}
765-
var ret = clean(p, isSuffix);
766-
return ret;
764+
return clean(p, isSuffix);
767765
};
768766

769767
// isSubsetOf returns true if all strings in s are also in t.
@@ -781,3 +779,18 @@ var isSubsetOf = function(s, t) {
781779
}
782780
return true;
783781
};
782+
783+
var isEqualTo = function(s, t) {
784+
if (s.length != t.length) {
785+
return false;
786+
}
787+
if (s == t) {
788+
return true;
789+
}
790+
for (var i = 0; i < s.length; i++) {
791+
if (s[i] != t[i]) {
792+
return false;
793+
}
794+
}
795+
return true;
796+
};

test/regex-peg_test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,42 @@ module.exports = {
155155
]
156156
});
157157
test.done();
158+
},
159+
160+
"/([0-9]+) ?sec/": function(test) {
161+
var tree = peg.parse("([0-9]+) ?sec");
162+
test.deepEqual(tree, {
163+
type: 'concat',
164+
value: [
165+
{ type: 'repetition',
166+
quantifier: '+',
167+
value: {
168+
type: 'char_class',
169+
value: '0-9'
170+
}
171+
},
172+
{ type: 'concat',
173+
value: [
174+
{ type: 'repetition',
175+
quantifier: '?',
176+
value: { type: 'literal', value: ' ' }
177+
},
178+
{ type: 'concat',
179+
value: [
180+
{ type: 'literal', value: 's' },
181+
{ type: 'concat',
182+
value: [
183+
{ type: 'literal', value: 'e' },
184+
{ type: 'literal', value: 'c' }
185+
]
186+
}
187+
]
188+
}
189+
]
190+
}
191+
]
192+
});
193+
test.done();
158194
}
159195
}
160196
};

test/regex-trigram_test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ module.exports = {
135135
]
136136
});
137137
test.done();
138+
},
139+
140+
"/([0-9]+) ?sec/": function(test) {
141+
var re = regex.parse("([0-9]+) ?sec");
142+
var q = regex.query(re);
143+
test.deepEqual(q, {
144+
op: 'AND',
145+
trigram: [ 'sec' ],
146+
sub:
147+
[
148+
{ op: 'OR',
149+
trigram: [ '1se', '2se', '3se', '4se', '5se', '6se', '7se', '8se', '9se' ],
150+
sub: [
151+
{ op: 'AND',
152+
trigram: [ ' se' ],
153+
sub: [
154+
{ op: 'OR',
155+
trigram: [ '0 s', '1 s', '2 s', '3 s', '4 s', '5 s', '6 s', '7 s', '8 s', '9 s' ],
156+
sub: []
157+
}
158+
]
159+
},
160+
{ op: 'AND',
161+
trigram: [ '0se' ],
162+
sub: []
163+
}
164+
]
165+
}
166+
]
167+
});
168+
test.done();
138169
}
139170
}
140171
};

0 commit comments

Comments
 (0)