Skip to content

Commit

Permalink
Allow wildcard rule's criterion applied
Browse files Browse the repository at this point in the history
  • Loading branch information
hippich committed Jun 29, 2015
1 parent e86c328 commit d7413d9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
9 changes: 7 additions & 2 deletions dist/index.js
Expand Up @@ -139,10 +139,15 @@ MapTable.prototype.match = function(values) {
return false;
}

var matchType = that.getTypeOfMatch(values[key]);
if (!rule[idx]) {
match = false;
return true;
}

var matchType = that.getTypeOfMatch(rule[idx]);
var matcher = that.matchers[matchType];

if (!rule[idx] || !matcher(values[key], rule[idx])) {
if (!matcher(values[key], rule[idx])) {
match = false;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "map-table",
"version": "0.0.7",
"description": "Maps object to a single value based on rules defined as a array/table.",
"main": "src/index.js",
"main": "dist/index.js",
"scripts": {
"test": "./node_modules/.bin/testem ci -l mocha,phantomjs,firefox"
},
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Expand Up @@ -14,6 +14,10 @@ MapTable = function MapTable(rules) {
MapTable.prototype.clone = function(obj) {
var out, i = 0;

if (obj == null) {
return obj;
}

if (Object.prototype.toString.call(obj) === '[object Array]') {
var len = obj.length;
out = [];
Expand Down Expand Up @@ -123,9 +127,10 @@ MapTable.prototype.match = function(values) {
return false;
}

if (!rule[idx]) {
match = false;
return true;
// If key is found, but rule value is null, assume it is wildcard rule match
// and skip checking this criterion.
if (rule[idx] == null) {
return false;
}

var matchType = that.getTypeOfMatch(rule[idx]);
Expand Down
12 changes: 10 additions & 2 deletions tests/index.js
Expand Up @@ -19,7 +19,8 @@ describe('MapTable', function() {
['id', 'prop1', 'prop2', 'prop3'],
[ 1 , 'xyz' , '123' , 'qwe' ] ,
[ 2 , 'zyx' , '321' , 'ewq' ] ,
[ 3 , '/abc/', '123' , 'asd' ]
[ 3 , '/abc/', '123' , 'asd' ] ,
[ 4 , 'xyz' , null , null ]
];

beforeEach(function() {
Expand Down Expand Up @@ -58,10 +59,17 @@ describe('MapTable', function() {
it('should do simple match', function() {
var match = mapTable.match({ prop1: 'xyz' });
should.exist(match);
match.prop1.should.equal('xyz');
match.prop2.should.equal('123');
match.id.should.equal(1);
});

it('should do simple match with wildcard rule', function() {
var match = mapTable.match({ prop1: 'xyz', prop2: '9999' });
should.exist(match);
should.not.exist(match.prop2);
match.id.should.equal(4);
});

it('should do regexp match', function() {
var match = mapTable.match({ prop1: 'oabcd' });
should.exist(match);
Expand Down

0 comments on commit d7413d9

Please sign in to comment.