From d9df06644e2a2e96e8c0d7e9ab32fba77ee56d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Mon, 15 Jul 2013 00:30:44 +0100 Subject: [PATCH] Initial commit. --- packages/bower-endpoint-parser/.editorconfig | 8 ++ packages/bower-endpoint-parser/.gitignore | 2 + packages/bower-endpoint-parser/.jshintrc | 61 +++++++++++ packages/bower-endpoint-parser/.travis.yml | 4 + packages/bower-endpoint-parser/LICENSE | 19 ++++ packages/bower-endpoint-parser/README.md | 100 +++++++++++++++++++ packages/bower-endpoint-parser/index.js | 88 ++++++++++++++++ packages/bower-endpoint-parser/package.json | 27 +++++ packages/bower-endpoint-parser/test/test.js | 1 + 9 files changed, 310 insertions(+) create mode 100644 packages/bower-endpoint-parser/.editorconfig create mode 100644 packages/bower-endpoint-parser/.gitignore create mode 100644 packages/bower-endpoint-parser/.jshintrc create mode 100644 packages/bower-endpoint-parser/.travis.yml create mode 100644 packages/bower-endpoint-parser/LICENSE create mode 100644 packages/bower-endpoint-parser/README.md create mode 100644 packages/bower-endpoint-parser/index.js create mode 100644 packages/bower-endpoint-parser/package.json create mode 100644 packages/bower-endpoint-parser/test/test.js diff --git a/packages/bower-endpoint-parser/.editorconfig b/packages/bower-endpoint-parser/.editorconfig new file mode 100644 index 00000000..ba442890 --- /dev/null +++ b/packages/bower-endpoint-parser/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] + +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 diff --git a/packages/bower-endpoint-parser/.gitignore b/packages/bower-endpoint-parser/.gitignore new file mode 100644 index 00000000..1b6c50d9 --- /dev/null +++ b/packages/bower-endpoint-parser/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/npm-debug.* \ No newline at end of file diff --git a/packages/bower-endpoint-parser/.jshintrc b/packages/bower-endpoint-parser/.jshintrc new file mode 100644 index 00000000..50467983 --- /dev/null +++ b/packages/bower-endpoint-parser/.jshintrc @@ -0,0 +1,61 @@ +{ + "predef": [ + "console", + "describe", + "it", + "after", + "afterEach", + "before", + "beforeEach" + ], + + "indent": 4, + "node": true, + "devel": true, + + "bitwise": false, + "curly": false, + "eqeqeq": true, + "forin": false, + "immed": true, + "latedef": false, + "newcap": true, + "noarg": true, + "noempty": false, + "nonew": true, + "plusplus": false, + "regexp": false, + "undef": true, + "unused": true, + "quotmark": "single", + "strict": false, + "trailing": true, + "camelcase": true, + + "asi": false, + "boss": true, + "debug": false, + "eqnull": true, + "esnext": false, + "evil": false, + "expr": true, + "funcscope": false, + "globalstrict": false, + "iterator": false, + "lastsemic": false, + "laxbreak": true, + "laxcomma": false, + "loopfunc": true, + "multistr": false, + "onecase": true, + "regexdash": false, + "scripturl": false, + "smarttabs": false, + "shadow": false, + "sub": false, + "supernew": true, + "validthis": false, + + "nomen": false, + "white": true +} diff --git a/packages/bower-endpoint-parser/.travis.yml b/packages/bower-endpoint-parser/.travis.yml new file mode 100644 index 00000000..df63076b --- /dev/null +++ b/packages/bower-endpoint-parser/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.10" + - "0.8" diff --git a/packages/bower-endpoint-parser/LICENSE b/packages/bower-endpoint-parser/LICENSE new file mode 100644 index 00000000..13e8246f --- /dev/null +++ b/packages/bower-endpoint-parser/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2012 Twitter and other contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/bower-endpoint-parser/README.md b/packages/bower-endpoint-parser/README.md new file mode 100644 index 00000000..98cb9e4f --- /dev/null +++ b/packages/bower-endpoint-parser/README.md @@ -0,0 +1,100 @@ +# bower-endpoint-parser [![Build Status](https://secure.travis-ci.org/bower/endpoint-parser.png?branch=master)](http://travis-ci.org/bower/endpoint-parser) + +Little module that helps with endpoints parsing. + + +## API + +### .decompose(endpoint) + +Decomposes a endpoint into `name`, `source` and `target`. + +```js +var endpointParser = require('bower-endpoint-parser'); + +endpointParser.decompose('jquery#~2.0.0'); +// { name: '', source: 'jquery', target: '~2.0.0' } + +endpointParser.decompose('backbone=backbone-amd#~1.0.0'); +// { name: 'backbone', source: 'backbone-amd', target: '~1.0.0' } + +endpointParser.decompose('http://twitter.github.io/bootstrap/assets/bootstrap.zip'); +// { name: '', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' } + +endpointParser.decompose('bootstrap=http://twitter.github.io/bootstrap/assets/bootstrap.zip'); +// { name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' } +``` + +### .compose(decEndpoint) + +Inverse of `compose()`. +Takes a decomposed endpoint and composes it back into a string. + +```js +var endpointParser = require('bower-endpoint-parser'); + +endpointParser.compose({ name: '', source: 'jquery', target: '~2.0.0' }); +// jquery=~2.0.0 + +endpointParser.compose({ name: 'backbone', source: 'backbone-amd', target: '~1.0.0' }); +// backbone=backbone-amd#~1.0.0 + +endpointParser.compose({ name: '', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }); +// http://twitter.github.io/bootstrap/assets/bootstrap.zip + +endpointParser.compose({ name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }); +// bootstrap=http://twitter.github.io/bootstrap/assets/bootstrap.zip +``` + +### .json2decomposed(key, value) + +Similar to `decompose()` but specially designed to be used when parsing `bower.json` dependencies. +For instance, in a `bower.json` like this: + +```js +{ + name: "foo", + "version": "0.1.0", + "dependencies": { + "jquery": "~1.9.1", + "backbone": "backbone-amd#~1.0.0", + "bootstrap": "http://twitter.github.io/bootstrap/assets/bootstrap" + } +} +``` + +You would call `json2decomposed` like so: + +``` +endpointParser.json2decomposed('jquery', '~1.9.1'); +// { name: 'jquery', source: 'jquery', target: '~1.9.1' } + +endpointParser.json2decomposed('backbone', 'backbone-amd#~1.0.0'); +// { name: 'backbone', source: 'backbone-amd', target: '~1.0.0' } + +endpointParser.json2decomposed('bootstrap', 'http://twitter.github.io/bootstrap/assets/bootstrap'); +// { name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' } +``` + +### .decomposed2json(decEndpoint) + +Inverse of `json2decomposed()`. +Takes a decomposed endpoint and composes it to an object. + +```js +var endpointParser = require('bower-endpoint-parser'); + +endpointParser.decomposed2json({ name: '', source: 'jquery', target: '~2.0.0' }); +// { jquery: '~2.0.0' } + +endpointParser.decomposed2json({ name: 'backbone', source: 'backbone-amd', target: '~1.0.0' }); +// { backbone: 'backbone-amd#~2.0.0' } + +endpointParser.decomposed2json({ name: 'bootstrap', source: 'http://twitter.github.io/bootstrap/assets/bootstrap', target: '*' }); +// { backbone: 'http://twitter.github.io/bootstrap/assets/bootstrap' } +``` + + +## License + +Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php). diff --git a/packages/bower-endpoint-parser/index.js b/packages/bower-endpoint-parser/index.js new file mode 100644 index 00000000..d4c83eed --- /dev/null +++ b/packages/bower-endpoint-parser/index.js @@ -0,0 +1,88 @@ +function decompose(endpoint) { + var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/; + var matches = endpoint.match(regExp); + var target; + var error; + + if (!matches) { + error = new Error('Invalid endpoint: ' + endpoint); + error.code = 'EINVEND'; + throw error; + } + + target = matches[3]; + + return { + name: matches[1] || '', + source: matches[2], + target: !target || target === 'latest' ? '*' : target + }; +} + +function compose(decEndpoint) { + var composed = ''; + + if (decEndpoint.name) { + composed += decEndpoint.name + '='; + } + + composed += decEndpoint.source; + + if (!isWildcard(decEndpoint.target)) { + composed += '#' + decEndpoint.target; + } + + return composed; +} + +function json2decomposed(key, value) { + var endpoint = key + '='; + var split = value.split('#'); + + // If # was found, the source was specified + if (split.length > 1) { + endpoint += split[0] + '#' + split[1]; + // If value has a /, it's probably a source + } else if (value.indexOf('/') !== -1) { + endpoint += value + '#*'; + // Otherwise use the key as the source + } else { + endpoint += key + '#' + split[0]; + } + + return decompose(endpoint); +} + +function decomposed2json(decEndpoint) { + var error; + var key = decEndpoint.name; + var value = ''; + var ret = {}; + + if (!key) { + error = new Error('Decomposed endpoint must have a name'); + error.code = 'EINVEND'; + throw error; + } + + if (decEndpoint.source !== decEndpoint.name) { + value += decEndpoint.source + '#'; + } + + if (!isWildcard(decEndpoint.target)) { + value += decEndpoint.target; + } + + ret[key] = value; + + return ret; +} + +function isWildcard(target) { + return !target || target === '*' || target === 'latest'; +} + +module.exports.decompose = decompose; +module.exports.compose = compose; +module.exports.json2decomposed = json2decomposed; +module.exports.decomposed2json = decomposed2json; diff --git a/packages/bower-endpoint-parser/package.json b/packages/bower-endpoint-parser/package.json new file mode 100644 index 00000000..f80795c6 --- /dev/null +++ b/packages/bower-endpoint-parser/package.json @@ -0,0 +1,27 @@ +{ + "name": "bower-endpoint-parser", + "version": "0.1.0-rc.1", + "description": "Little module that helps with endpoints parsing.", + "author": "Twitter", + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/bower/endpoint-parser/blob/master/LICENSE" + } + ], + "repository": { + "type": "git", + "url": "git://github.com/bower/endpoint-parser.git" + }, + "main": "index.js", + "engines": { + "node": ">=0.8.0" + }, + "devDependencies": { + "expect.js": "~0.2.0", + "mocha": "~1.12.0" + }, + "scripts": { + "test": "npm test" + } +} diff --git a/packages/bower-endpoint-parser/test/test.js b/packages/bower-endpoint-parser/test/test.js new file mode 100644 index 00000000..0ffdd02f --- /dev/null +++ b/packages/bower-endpoint-parser/test/test.js @@ -0,0 +1 @@ +// TODO \ No newline at end of file