diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..2f38162 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,7 @@ +{ + "extends": "./node_modules/wealthfront-javascript/.eslintrc", + "parser": "babel-eslint", + "env": { + "node": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93f1361 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +npm-debug.log diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..6579bd5 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,9 @@ +{ + "plugins": ["wealthfront-javascript"], + "preset": "wealthfront-javascript", + "esnext": true, + "validateQuoteMarks": { + "mark": "'", + "escape": true + } +} diff --git a/.yo-rc.json b/.yo-rc.json new file mode 100644 index 0000000..ffd4bc1 --- /dev/null +++ b/.yo-rc.json @@ -0,0 +1,10 @@ +{ + "generator-wf-npm": { + "prompts": { + "packageName": "clean-git-ref", + "targetEnv": [ + "Node" + ] + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d31a1a6 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# clean-git-ref + +Clean an input string into a usable git ref. + +For more reference, read https://git-scm.com/docs/git-check-ref-format + +## Installation + +```sh +$ npm install clean-git-ref --save-dev +``` + +## API Usage + +### clean(string input) -> string output +``` +var cleanGitRef = require('clean-git-ref'); + +assert.stricEqual(cleanGitRef.clean('bad git ref formats/'), bad-git-ref-formats'); +``` + +## CLI Usage + +```bash +> clean-git-ref 'bad git ref formats/' +bad-git-ref-formats +``` diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..1243ced --- /dev/null +++ b/lib/index.js @@ -0,0 +1,9 @@ +'use strict'; + +var CleanGitRef = { + clean: function clean(value) { + return value.replace(/[^0-9.]+/, ''); + } +}; + +module.exports = CleanGitRef; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..03929d6 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "clean-git-ref", + "version": "1.0.0", + "license": "Apache-2.0", + "author": "Eli White ", + "files": [ + "lib" + ], + "main": "lib/index.js", + "scripts": { + "build": "babel src -d lib", + "style": "eslint src test && jscs src test", + "pretest": "npm run style", + "test": "mocha", + "posttest": "npm run build" + }, + "devDependencies": { + "babel-cli": "6.6.5", + "babel-eslint": "5.0.0", + "babel-preset-es2015": "6.6.0", + "babel-register": "^6.7.2", + "chai": "3.5.0", + "eslint": "1.10.3", + "jscs": "2.9.0", + "mocha": "2.3.4", + "wealthfront-javascript": "2.4.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..c55608a --- /dev/null +++ b/src/index.js @@ -0,0 +1,9 @@ +'use strict'; + +const CleanGitRef = { + clean(value) { + return value.replace(/[^0-9.]+/, ''); + } +}; + +module.exports = CleanGitRef; diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 0000000..7eeefc3 --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,5 @@ +{ + "env": { + "mocha": true + } +} diff --git a/test/js/setup.js b/test/js/setup.js new file mode 100644 index 0000000..7f519d9 --- /dev/null +++ b/test/js/setup.js @@ -0,0 +1,9 @@ +'use strict'; + +var chai = require('chai'); + +chai.config.includeStack = true; + +chai.assert.equal = function() { + throw new Error("Chai's assert.equal function does == instead of ===. Use assert.strictEqual instead"); +}; diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..85d32d2 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,5 @@ +test/js/setup.js +test/src-test/**/*.js +--compilers js:babel-register +-R dot +-u bdd diff --git a/test/src-test/index_test.js b/test/src-test/index_test.js new file mode 100644 index 0000000..1fc47a8 --- /dev/null +++ b/test/src-test/index_test.js @@ -0,0 +1,34 @@ +'use strict'; + +const assert = require('chai').assert; +const spawn = require('child_process').spawn; +const cleanGitRef = require('../../src/index'); + +function assertValidBranchName(branchName) { + const checkRefFormat = spawn('git', ['check-ref-format', 'refs/' + branchName]); + + return new Promise(function(resolve, reject) { + checkRefFormat.on('close', (code) => { + if (code === 0) { + resolve(); + return; + } + + reject(); + }); + }); +} + +function assertOutputAndVerifyValid(input, output) { + it("should convert '" + input + "' to '" + output + "'", () => { + var result = cleanGitRef.clean(input); + assert.strictEqual(result, output); + return assertValidBranchName(result); + }); +} + +describe('CleanGitRef', function() { + describe('clean', function() { + assertOutputAndVerifyValid('^0.2.3', '0.2.3'); + }); +});