Skip to content

Commit

Permalink
feat: add debug() method
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Oct 17, 2016
1 parent b608fff commit 00f1da9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,48 @@ myMatch.test('http://google.com/#aaa'); // true
myMatch.test('http://google.com/#aaabbb'); // true
```

### Debug

You can use `debug()` method to get detailed information about matching of each part of the pattern against each tested URL.

Use it the same way you would use `test()` method. But instead of boolean value, it returns object where keys are tested URLs and values are deconstructed results.

```javascript
myMatch = new UrlMatch('*://aaa.bbb/');
myMatch.debug('http://aaa.bbb/');
/*
{
"*://aaa.bbb/": {
"scheme": {
"pattern": "*",
"value": "http",
"result": true
},
"host": {
"pattern": "aaa.bbb",
"value": "aaa.bbb",
"result": true
},
"path": {
"pattern": "",
"value": "",
"result": true
},
"params": {
"pattern": null,
"value": null,
"result": true
},
"fragment": {
"pattern": null,
"value": null,
"result": true
}
}
}
*/
```

## Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either [file an issue at GitHub](https://github.com/fczbkk/UrlMatch/issues) or send me an e-mail at <a href="mailto:riki@fczbkk.com">riki@fczbkk.com</a>.
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ export default class {
return result;
}

debug (content) {
let result = {};

this.patterns.forEach((pattern) => {
const pattern_obj = new Pattern(pattern);
result[pattern] = pattern_obj.debug(content);
});

return result;
}

}
15 changes: 15 additions & 0 deletions src/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ export default class {
return result;
}

debug (url) {
const splits = this.split(url);
const result = {};

Object.keys(splits).forEach((key) => {
result[key] = {
pattern: this.url_parts[key].original_pattern,
value: splits[key],
result: this.url_parts[key].test(splits[key])
}
});

return result;
}

}


15 changes: 15 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,19 @@ describe('General', function() {

});

describe('debug', function () {

it('should return debug object', function () {
const pattern = '*://aaa.bbb/';
url_match = new UrlMatch(pattern);
const result = url_match.debug('http://aaa.bbb/');
expect(result[pattern].scheme).toEqual({
pattern: '*',
value: 'http',
result: true
});
});

});

});
11 changes: 11 additions & 0 deletions test/pattern.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,15 @@ describe('Pattern', function() {

});

describe('debug', function () {

it('should return debug object', function () {
pattern = new Pattern('*://aaa.bbb/');
const result = pattern.debug('http://aaa.bbb/');
expect(result.scheme)
.toEqual({pattern: '*', value: 'http', result: true});
});

});

});

0 comments on commit 00f1da9

Please sign in to comment.