Skip to content

Commit

Permalink
Merge pull request #35 from oncletom/fix-35
Browse files Browse the repository at this point in the history
[WIP] tldjs gives inconsistent results
  • Loading branch information
Thomas Parisot committed Jan 17, 2014
2 parents cd18296 + 126fda8 commit 1b9e1b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ language: "node_js"
node_js:
- "0.8"
- "0.10"

cache:
directories:
- node_modules

notifications:
email:
on_success: change
on_failure: change

before_script:
- npm run build
12 changes: 7 additions & 5 deletions lib/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tld.getCandidateRule = function getCandidateRule (host, rules) {
rules.some(function (r) {
var pattern;

//sld matching? escape the loop immediately (except if it's an exception)
// sld matching? escape the loop immediately (except if it's an exception)
if ('.' + host === r.getNormalXld()) {
if (r.exception === true) {
rule.normal = r;
Expand All @@ -40,8 +40,8 @@ tld.getCandidateRule = function getCandidateRule (host, rules) {
return true;
}

//otherwise check as a complete host
//if it's an exception, we want to loop a bit more to a normal rule
// otherwise check as a complete host
// if it's an exception, we want to loop a bit more to a normal rule
pattern = '.+' + r.getNormalPattern() + '$';

if ((new RegExp(pattern)).test(host)) {
Expand All @@ -52,9 +52,11 @@ tld.getCandidateRule = function getCandidateRule (host, rules) {
return false;
});

//if there is an exception, we challenge its rules without changing pattern
// favouring the exception if encountered
// previously we were copy-altering a rule, creating inconsistent results based on rule order order
// @see https://github.com/oncletom/tld.js/pull/35
if (rule.normal && rule.exception) {
rule.normal.wildcard = rule.exception.wildcard;
return rule.exception;
}

return rule.normal;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test": "npm run lint && npm run test-node",
"lint": "jshint --config .jshintrc lib/**/*.js",
"test-node": "mocha --reporter dot",
"test-watch": "mocha --reporter dot --watch",
"test-browser": "testling",
"build": "npm run build-rules && npm run build-browser && npm run build-compress",
"build-rules": "grunt update",
Expand Down
11 changes: 11 additions & 0 deletions test/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('tld.js', function () {
it('should not break on specific RegExp characters', function () {
expect(tld.getDomain('www.weir)domain.com')).to.equal('weir)domain.com');
});

it('should provide consistent results', function(){
expect(tld.getDomain('www.bl.uk')).to.equal('bl.uk');
expect(tld.getDomain('www.majestic12.co.uk')).to.equal('majestic12.co.uk');
});
});

describe('tldExists method', function () {
Expand Down Expand Up @@ -124,5 +129,11 @@ describe('tld.js', function () {
it('should not break on specific RegExp characters', function () {
expect(tld.getSubdomain('www.weir)domain.com')).to.equal('www');
});

//@see https://github.com/oncletom/tld.js/issues/35
it('should provide consistent results', function(){
expect(tld.getSubdomain('www.bl.uk')).to.equal('www');
expect(tld.getSubdomain('www.majestic12.co.uk')).to.equal('www');
});
});
});

0 comments on commit 1b9e1b4

Please sign in to comment.