Skip to content

Commit

Permalink
Properly support wildcard when not in a section of its own (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonOehlman authored Jan 7, 2019
1 parent c9f4558 commit 4797dae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* jshint node: true */
'use strict';

var REGEXP_PARTS = /(\*|\?)/g;

/**
# wildcard
Expand Down Expand Up @@ -35,9 +37,9 @@

function WildcardMatcher(text, separator) {
this.text = text = text || '';
this.hasWild = ~text.indexOf('*');
this.hasWild = text.indexOf('*') >= 0;
this.separator = separator;
this.parts = text.split(separator);
this.parts = text.split(separator).map(this.classifyPart.bind(this));
}

WildcardMatcher.prototype.match = function(input) {
Expand All @@ -56,7 +58,9 @@ WildcardMatcher.prototype.match = function(input) {
if (parts[ii] === '*') {
continue;
} else if (ii < testParts.length) {
matches = parts[ii] === testParts[ii];
matches = parts[ii] instanceof RegExp
? parts[ii].test(testParts[ii])
: parts[ii] === testParts[ii];
} else {
matches = false;
}
Expand Down Expand Up @@ -88,6 +92,18 @@ WildcardMatcher.prototype.match = function(input) {
return matches;
};

WildcardMatcher.prototype.classifyPart = function(part) {
// in the event that we have been provided a part that is not just a wildcard
// then turn this into a regular expression for matching purposes
if (part === '*') {
return part;
} else if (part.indexOf('*') >= 0 || part.indexOf('?') >= 0) {
return new RegExp(part.replace(REGEXP_PARTS, '\.$1'));
}

return part;
};

module.exports = function(text, test, separator) {
var matcher = new WildcardMatcher(text, separator || /[\/\.]/);
if (typeof test != 'undefined') {
Expand Down
13 changes: 11 additions & 2 deletions test/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ var test = require('tape'),

test('general wild card matching tests', function(t) {

t.plan(6);
t.plan(7);
t.ok(wildcard('foo.*', 'foo.bar'), 'foo.* should match foo.bar');
t.ok(wildcard('foo.*', 'foo'), 'foo.* should match foo');
t.ok(wildcard('*.foo.com', 'test.foo.com'), 'test.foo.com should match *.foo.com');
t.notOk(wildcard('foo.*', 'bar'), 'foo.* should not match bar');
t.ok(wildcard('a.*.c', 'a.b.c'), 'a.*.c should match a.b.c');
t.notOk(wildcard('a.*.c', 'a.b'), 'a.*.c should not match a.b');
t.notOk(wildcard('a', 'a.b.c'), 'a should not match a.b.c');
t.notOk(wildcard('a', 'a.b.c'), 'a should not match a.b.c');
});

test('regex wildcard matching tests', function(t) {
t.plan(4);
t.ok(wildcard('*foo', 'foo'), '*foo should match foo');
t.ok(wildcard('*foo.b', 'foo.b'), '*foo.b should match foo.b');
t.ok(wildcard('a.*foo.c', 'a.barfoo.c'), 'a.barfoo.c should match a.*foo.c');
t.ok(wildcard('a.foo*.c', 'a.foobar.c'), 'a.foobar.c should match a.foo*.c');
});

test('general wild card with separator matching tests', function(t) {
Expand Down

0 comments on commit 4797dae

Please sign in to comment.