bower-endpoint-parser
Advanced tools
Comparing version 0.1.0 to 0.2.0
61
index.js
function decompose(endpoint) { | ||
// Note that we allow spaces in targets and sources but they are trimmed | ||
var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/; | ||
@@ -13,8 +14,8 @@ var matches = endpoint.match(regExp); | ||
target = matches[3]; | ||
target = trim(matches[3]); | ||
return { | ||
name: matches[1] || '', | ||
source: matches[2], | ||
target: !target || target === 'latest' ? '*' : target | ||
name: trim(matches[1]), | ||
source: trim(matches[2]), | ||
target: isWildcard(target) ? '*' : target | ||
}; | ||
@@ -24,12 +25,15 @@ } | ||
function compose(decEndpoint) { | ||
var name = trim(decEndpoint.name); | ||
var source = trim(decEndpoint.source); | ||
var target = trim(decEndpoint.target); | ||
var composed = ''; | ||
if (decEndpoint.name) { | ||
composed += decEndpoint.name + '='; | ||
if (name) { | ||
composed += name + '='; | ||
} | ||
composed += decEndpoint.source; | ||
composed += source; | ||
if (!isWildcard(decEndpoint.target)) { | ||
composed += '#' + decEndpoint.target; | ||
if (!isWildcard(target)) { | ||
composed += '#' + target; | ||
} | ||
@@ -41,5 +45,18 @@ | ||
function json2decomposed(key, value) { | ||
var endpoint = key + '='; | ||
var split = value.split('#'); | ||
var endpoint; | ||
var split; | ||
var error; | ||
key = trim(key); | ||
value = trim(value); | ||
if (!key) { | ||
error = new Error('The key must be specified'); | ||
error.code = 'EINVEND'; | ||
throw error; | ||
} | ||
endpoint = key + '='; | ||
split = value.split('#').map(trim); | ||
// If # was found, the source was specified | ||
@@ -61,7 +78,9 @@ if (split.length > 1) { | ||
var error; | ||
var key = decEndpoint.name; | ||
var name = trim(decEndpoint.name); | ||
var source = trim(decEndpoint.source); | ||
var target = trim(decEndpoint.target); | ||
var value = ''; | ||
var ret = {}; | ||
if (!key) { | ||
if (!name) { | ||
error = new Error('Decomposed endpoint must have a name'); | ||
@@ -73,4 +92,4 @@ error.code = 'EINVEND'; | ||
// Add source only if different than the name | ||
if (decEndpoint.source !== decEndpoint.name) { | ||
value += decEndpoint.source; | ||
if (source !== name) { | ||
value += source; | ||
} | ||
@@ -80,9 +99,9 @@ | ||
if (!value) { | ||
value += isWildcard(decEndpoint.target) ? '*' : decEndpoint.target; | ||
value += isWildcard(target) ? '*' : target; | ||
// Otherwise append only if not a wildcard | ||
} else if (!isWildcard(decEndpoint.target)) { | ||
value += '#' + decEndpoint.target; | ||
} else if (!isWildcard(target)) { | ||
value += '#' + target; | ||
} | ||
ret[key] = value; | ||
ret[name] = value; | ||
@@ -92,2 +111,6 @@ return ret; | ||
function trim(str) { | ||
return str ? str.trim() : ''; | ||
} | ||
function isWildcard(target) { | ||
@@ -94,0 +117,0 @@ return !target || target === '*' || target === 'latest'; |
{ | ||
"name": "bower-endpoint-parser", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Little module that helps with endpoints parsing.", | ||
@@ -5,0 +5,0 @@ "author": "Twitter", |
173
test/test.js
@@ -26,2 +26,16 @@ var expect = require('expect.js'); | ||
}); | ||
it('should trim sources and targets', function () { | ||
var decEndpoint = endpointParser.decompose('foo= source # ~1.0.2 '); | ||
expect(decEndpoint.source).to.equal('source'); | ||
expect(decEndpoint.target).to.equal('~1.0.2'); | ||
decEndpoint = endpointParser.decompose('foo= source # latest'); | ||
expect(decEndpoint.source).to.equal('source'); | ||
expect(decEndpoint.target).to.equal('*'); | ||
decEndpoint = endpointParser.decompose('foo= source # *'); | ||
expect(decEndpoint.source).to.equal('source'); | ||
expect(decEndpoint.target).to.equal('*'); | ||
}); | ||
}); | ||
@@ -49,5 +63,52 @@ | ||
}); | ||
it('should trim values', function () { | ||
expect(endpointParser.compose({ | ||
name: ' foo ', | ||
source: ' bar ', | ||
target: ' ~1.0.2 ' | ||
})).to.equal('foo=bar#~1.0.2'); | ||
expect(endpointParser.compose({ | ||
name: ' foo ', | ||
source: ' foo ', | ||
target: ' ~1.0.2 ' | ||
})).to.equal('foo=foo#~1.0.2'); | ||
expect(endpointParser.compose({ | ||
name: ' foo ', | ||
source: ' foo ', | ||
target: ' * ' | ||
})).to.equal('foo=foo'); | ||
expect(endpointParser.compose({ | ||
name: ' foo ', | ||
source: ' foo ', | ||
target: ' * ' | ||
})).to.equal('foo=foo'); | ||
expect(endpointParser.compose({ | ||
name: ' ', | ||
source: ' foo ', | ||
target: '' | ||
})).to.equal('foo'); | ||
}); | ||
}); | ||
describe('.json2decomposed', function () { | ||
var expected = [ | ||
{ name: 'jquery', source: 'jquery', target: '~1.9.1' }, | ||
{ name: 'foo', source: 'foo', target: '*' }, | ||
{ name: 'bar', source: 'bar', target: '*' }, | ||
{ name: 'baz', source: 'baz', target: '~0.2.0' }, | ||
{ name: 'backbone', source: 'backbone-amd', target: '~1.0.0' }, | ||
{ name: 'backbone2', source: 'backbone=backbone-amd', target: '~1.0.0' }, | ||
{ name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }, | ||
{ name: 'bootstrap2', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }, | ||
{ name: 'ssh', source: 'git@example.com', target: '*' }, | ||
{ name: 'git', source: 'git://example.com', target: '*' }, | ||
{ name: 'path', source: '/foo', target: '*' }, | ||
{ name: 'winpath', source: 'c:\\foo', target: '*' } | ||
]; | ||
it('should decompose json endpoints correctly', function () { | ||
@@ -68,16 +129,2 @@ var dependencies = { | ||
}; | ||
var expected = [ | ||
{ name: 'jquery', source: 'jquery', target: '~1.9.1' }, | ||
{ name: 'foo', source: 'foo', target: '*' }, | ||
{ name: 'bar', source: 'bar', target: '*' }, | ||
{ name: 'baz', source: 'baz', target: '~0.2.0' }, | ||
{ name: 'backbone', source: 'backbone-amd', target: '~1.0.0' }, | ||
{ name: 'backbone2', source: 'backbone=backbone-amd', target: '~1.0.0' }, | ||
{ name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }, | ||
{ name: 'bootstrap2', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }, | ||
{ name: 'ssh', source: 'git@example.com', target: '*' }, | ||
{ name: 'git', source: 'git://example.com', target: '*' }, | ||
{ name: 'path', source: '/foo', target: '*' }, | ||
{ name: 'winpath', source: 'c:\\foo', target: '*' } | ||
]; | ||
var x = 0; | ||
@@ -90,5 +137,60 @@ | ||
}); | ||
it('should trim values', function () { | ||
var dependencies = { | ||
' jquery ': ' ~1.9.1 ', | ||
' foo ': ' latest ', | ||
' bar ': ' * ', | ||
' baz ': '# ~0.2.0 ', | ||
' backbone ': ' backbone-amd#~1.0.0 ', | ||
' backbone2 ': ' backbone=backbone-amd # ~1.0.0 ', | ||
' bootstrap ': ' http://twitter.github.io/bootstrap/assets/bootstrap', | ||
' bootstrap2 ': ' http://twitter.github.io/bootstrap/assets/bootstrap # *', | ||
' ssh ': ' git@example.com ', | ||
' git ': ' git://example.com ', | ||
' path ': ' /foo ', | ||
' winpath ': ' c:\\foo ' | ||
}; | ||
var x = 0; | ||
mout.object.forOwn(dependencies, function (value, key) { | ||
expect(endpointParser.json2decomposed(key, value)).to.eql(expected[x]); | ||
x += 1; | ||
}); | ||
}); | ||
it('should error out if key is not specified', function () { | ||
try { | ||
endpointParser.json2decomposed(null); | ||
throw new Error('Should have failed'); | ||
} catch (e) { | ||
expect(e.code).to.equal('EINVEND'); | ||
expect(e.message).to.contain('key must be specified'); | ||
} | ||
try { | ||
endpointParser.json2decomposed(''); | ||
throw new Error('Should have failed'); | ||
} catch (e) { | ||
expect(e.code).to.equal('EINVEND'); | ||
expect(e.message).to.contain('key must be specified'); | ||
} | ||
}); | ||
}); | ||
describe('.decomposed2json', function () { | ||
var expected = [ | ||
{ jquery: '~1.9.1' }, | ||
{ foo: '*' }, | ||
{ bar: '*' }, | ||
{ baz: '*' }, | ||
{ jqueryx: 'jquery#~1.9.1' }, | ||
{ backbone: 'backbone-amd#~1.0.0' }, | ||
{ backbone : 'backbone=backbone-amd#~1.0.0' }, | ||
{ bootstrap: 'http://twitter.github.io/bootstrap/assets/bootstrap' }, | ||
{ bootstrap: 'http://twitter.github.io/bootstrap/assets/bootstrap' }, | ||
{ ssh: 'git@example.com' }, | ||
{ git: 'git://example.com' } | ||
]; | ||
it('should compose endpoints to json correctly', function () { | ||
@@ -108,14 +210,23 @@ var decEndpoints = [ | ||
]; | ||
var expected = [ | ||
{ jquery: '~1.9.1' }, | ||
{ foo: '*' }, | ||
{ bar: '*' }, | ||
{ baz: '*' }, | ||
{ jqueryx: 'jquery#~1.9.1' }, | ||
{ backbone: 'backbone-amd#~1.0.0' }, | ||
{ backbone : 'backbone=backbone-amd#~1.0.0' }, | ||
{ bootstrap: 'http://twitter.github.io/bootstrap/assets/bootstrap' }, | ||
{ bootstrap: 'http://twitter.github.io/bootstrap/assets/bootstrap' }, | ||
{ ssh: 'git@example.com' }, | ||
{ git: 'git://example.com' } | ||
var x = 0; | ||
decEndpoints.forEach(function (decEndpoint) { | ||
expect(endpointParser.decomposed2json(decEndpoint)).to.eql(expected[x]); | ||
x += 1; | ||
}); | ||
}); | ||
it('should trim values', function () { | ||
var decEndpoints = [ | ||
{ name: ' jquery ', source: ' jquery ', target: ' ~1.9.1 ' }, | ||
{ name: 'foo', source: ' foo', target: ' latest ' }, | ||
{ name: 'bar', source: 'bar ', target: ' * ' }, | ||
{ name: 'baz ', source: 'baz', target: ' ' }, | ||
{ name: ' jqueryx ', source: ' jquery ', target: ' ~1.9.1 ' }, | ||
{ name: ' backbone ', source: ' backbone-amd ', target: ' ~1.0.0 ' }, | ||
{ name: ' backbone ', source: ' backbone=backbone-amd ', target: ' ~1.0.0 ' }, | ||
{ name: ' bootstrap ', source: ' http://twitter.github.io/bootstrap/assets/bootstrap ', target: ' ' }, | ||
{ name: ' bootstrap ', source: ' http://twitter.github.io/bootstrap/assets/bootstrap ', target: ' * ' }, | ||
{ name: ' ssh ', source: ' git@example.com ', target: ' * ' }, | ||
{ name: ' git ', source: ' git://example.com ', target: ' * ' } | ||
]; | ||
@@ -133,11 +244,17 @@ var x = 0; | ||
endpointParser.decomposed2json({ name: '', source: 'jquery', target: '*' }); | ||
throw new Error('Should have failed'); | ||
} catch (e) { | ||
expect(e.code).to.equal('EINVEND'); | ||
expect(e.message).to.contain('must have a name'); | ||
return; | ||
} | ||
throw new Error('Should have failed'); | ||
try { | ||
endpointParser.decomposed2json({ name: ' ', source: 'jquery', target: '*' }); | ||
throw new Error('Should have failed'); | ||
} catch (e) { | ||
expect(e.code).to.equal('EINVEND'); | ||
expect(e.message).to.contain('must have a name'); | ||
} | ||
}); | ||
}); | ||
}); |
21709
324