language-tags
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -61,34 +61,43 @@ /** | ||
tags.search = function(description, all) { | ||
var i, l, test, push, results = []; | ||
tags.search = function(query, all) { | ||
var test; | ||
push = function(record) { | ||
if (record.Subtag) { | ||
results.push(new Subtag(record.Subtag, record.Type)); | ||
} else if (all) { | ||
results.push(new Tag(record.Tag)); | ||
} | ||
}; | ||
if ('function' === typeof query.test) { | ||
test = function(description) { | ||
return query.test(description); | ||
}; | ||
if ('string' === typeof description) { | ||
description = description.toLowerCase(); | ||
test = function(record) { | ||
if (-1 !== record.Description.join(', ').toLowerCase().indexOf(description)) { | ||
push(record); | ||
} | ||
// If the query is all lowercase, make a case-insensitive match. | ||
} else if (query.toLowerCase() === query) { | ||
test = function(description) { | ||
return -1 !== description.toLowerCase().indexOf(query); | ||
}; | ||
} else if ('function' === typeof description.test) { | ||
test = function(record) { | ||
if (description.test(record.Description.join(', '))) { | ||
push(record); | ||
} | ||
} else { | ||
test = function(description) { | ||
return -1 !== description.indexOf(query); | ||
}; | ||
} | ||
for (i = 0, l = registry.length; i < l; i++) { | ||
test(registry[i]); | ||
} | ||
return registry.filter(function(record) { | ||
if (!record.Subtag && !all) { | ||
return false; | ||
} | ||
return results; | ||
return record.Description.some(test); | ||
// Sort by matched description string length. | ||
// This is a quick way to push precise matches towards the top. | ||
}).sort(function(a, b) { | ||
return Math.min.apply(Math, a.Description.filter(test).map(function(description) { | ||
return description.length; | ||
})) > Math.min.apply(Math, b.Description.filter(test).map(function(description) { | ||
return description.length; | ||
})); | ||
}).map(function(record) { | ||
if (record.Subtag) { | ||
return new Subtag(record.Subtag, record.Type); | ||
} | ||
return new Tag(record.Tag); | ||
}); | ||
}; | ||
@@ -95,0 +104,0 @@ |
{ | ||
"name": "language-tags", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"implements": ["CommonJS/Modules/1.0"], | ||
@@ -8,2 +8,3 @@ "description": "Work with IANA language tags.", | ||
"homepage": "https://github.com/mattcg/language-tags", | ||
"author": "Matthew Caruana Galizia <m@m.cg>", | ||
"repository": { | ||
@@ -13,14 +14,6 @@ "type": "git", | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Matthew Caruana Galizia", | ||
"email": "m@m.cg" | ||
} | ||
], | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://mattcg.mit-license.org/" | ||
} | ||
], | ||
"bugs": { | ||
"url": "https://github.com/mattcg/language-tags/issues" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
@@ -27,0 +20,0 @@ "test": "make test" |
@@ -87,2 +87,12 @@ /** | ||
test('search() puts exact match at the top', function() { | ||
var subtags; | ||
subtags = tags.search('Dari'); | ||
assert(subtags.length > 0); | ||
assert.equal(subtags[0].type(), 'language'); | ||
assert.equal(subtags[0].format(), 'prs'); | ||
}); | ||
test('subtags() returns subtags', function() { | ||
@@ -89,0 +99,0 @@ var subtags; |
@@ -331,3 +331,3 @@ /** | ||
// Grandfathered but not deprecated, therefore valid. | ||
tag = new Tag('en-GB-oed'); | ||
tag = new Tag('i-default'); | ||
assert.equal(tag.type(), 'grandfathered'); | ||
@@ -460,3 +460,3 @@ assert(!tag.deprecated()); | ||
// Grandfathered but not deprecated. | ||
tag = new Tag('en-GB-oed'); | ||
tag = new Tag('i-default'); | ||
assert.equal(tag.type(), 'grandfathered'); | ||
@@ -486,6 +486,6 @@ assert(!tag.deprecated()); | ||
// Grandfathered but not deprecated. | ||
tag = new Tag('en-GB-oed'); | ||
tag = new Tag('i-default'); | ||
assert.equal(tag.type(), 'grandfathered'); | ||
assert(!tag.deprecated()); | ||
assert.equal(tag.added(), '2003-07-09'); | ||
assert.equal(tag.added(), '1998-03-10'); | ||
}); | ||
@@ -496,6 +496,6 @@ | ||
tag = new Tag('en-GB-oed'); | ||
tag = new Tag('i-default'); | ||
assert.equal(tag.type(), 'grandfathered'); | ||
assert(!tag.deprecated()); | ||
assert.deepEqual(tag.descriptions(), ['English, Oxford English Dictionary spelling']); | ||
assert.deepEqual(tag.descriptions(), ['Default Language']); | ||
@@ -502,0 +502,0 @@ // Otherwise returns an empty array. |
47505
1116