Skip to content

Commit

Permalink
Fix #3: return subtags as usual for redundant tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcg committed Jan 12, 2016
1 parent 37e16a1 commit 4809fff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -211,6 +211,14 @@ Returns `grandfathered` if the tag is grandfathered, `redundant` if the tag is r

Returns an array of subtags making up the tag, as `Subtag` objects.

#### tag.language(), tag.region(), tag.script() ####

Shortcuts for `tag.find('language')`, `tag.find('region')` and `tag.find('script')`.

#### tag.find(type) ####

Find a subtag of the given type from those making up the tag.

#### tag.valid() ####

Returns `true` if the tag is valid, `false` otherwise.
Expand Down
28 changes: 26 additions & 2 deletions lib/Tag.js
Expand Up @@ -57,8 +57,8 @@ Tag.prototype.preferred = function() {
Tag.prototype.subtags = function() {
var codes, data = this.data, subtags = [];

// No subtags if the tag is grandfathered or redundant.
if (data.record) {
// No subtags if the tag is grandfathered.
if (data.record && this.type() === 'grandfathered') {
return subtags;
}

Expand Down Expand Up @@ -141,6 +141,30 @@ Tag.prototype.subtags = function() {
return subtags;
};

Tag.prototype.language = function() {
return this.find('language');
};

Tag.prototype.region = function() {
return this.find('region');
};

Tag.prototype.script = function() {
return this.find('script');
};

Tag.prototype.find = function(type) {
var i, l, subtag, subtags = this.subtags();

for (i = 0, l = subtags.length; i < l; i++) {
subtag = subtags[i];

if (subtag.type() === type) {
return subtag;
}
}
};

Tag.prototype.valid = function() {
return this.errors().length < 1;
};
Expand Down
22 changes: 19 additions & 3 deletions test/lib/TagTest.js
Expand Up @@ -95,15 +95,19 @@ suite('Tag', function() {
assert.equal(tag.type(), 'grandfathered');
subtags = tag.subtags();
assert.deepEqual(subtags, []);
assert.equal(undefined, tag.region());
assert.equal(undefined, tag.language());
});

test('tag.subtags() returns empty array for redundant tag', function() {
test('tag.subtags() returns array for redundant tag', function() {
var tag, subtags;

tag = new Tag('az-Arab');
assert.equal(tag.type(), 'redundant');
subtags = tag.subtags();
assert.deepEqual(subtags, []);
assert.equal(2, subtags.length);
assert.equal(subtags[0].format(), 'az');
assert.equal(subtags[1].format(), 'Arab');
});

test('tag.errors() returns error for deprecated grandfathered tag', function() {
Expand Down Expand Up @@ -487,7 +491,7 @@ suite('Tag', function() {
assert.equal(tag.added(), '1998-03-10');
});

test('tag.description() returns description when available', function() {
test('tag.descriptions() returns descriptions when available', function() {
var tag;

tag = new Tag('i-default');
Expand Down Expand Up @@ -520,4 +524,16 @@ suite('Tag', function() {

assert.equal(new Tag('zh-Hans').preferred(), null);
});

test('tag.region() and tag.language() return subtags for redundant tags', function() {
var tag;

tag = new Tag('es-419');
assert.deepEqual(tag.region().descriptions(), ['Latin America and the Caribbean']);
assert.deepEqual(tag.language().descriptions(), ['Spanish', 'Castilian']);

tag = new Tag('sgn-NL');
assert.deepEqual(tag.region().descriptions(), ['Netherlands']);
assert.deepEqual(tag.language().descriptions(), ['Sign languages']);
});
});

0 comments on commit 4809fff

Please sign in to comment.