-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dea890
commit 57c350e
Showing
9 changed files
with
249 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
'use strict'; | ||
|
||
module.exports = parser(); | ||
|
||
function parser() { | ||
var args = process.argv.slice(2); | ||
args = require('./parser')(args); | ||
args = require('./parser.es5.js')(args); | ||
|
||
return args; | ||
} | ||
|
||
module.exports = parser(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
module.exports = parser; | ||
|
||
function typeCast(value) { | ||
var isNumber = value !== true && !isNaN(value); | ||
var isFalse = value === 'false'; | ||
var isTrue = value === 'true'; | ||
|
||
// to number | ||
value = isNumber ? Number(value) : value; | ||
|
||
// to boolean | ||
value = isFalse ? false : isTrue ? true : value; | ||
|
||
return value; | ||
}; | ||
|
||
function setArgs(args) { | ||
// if unit test, receive a args in string | ||
// then parsed this to generate a array like a process.argv | ||
if (typeof args === 'string') { | ||
args = args.split(' '); | ||
args.forEach(removeQuotes); | ||
} | ||
|
||
return args; | ||
|
||
function removeQuotes(value, index) { | ||
var arg = value.replace(/(\"|\')/g, ''); | ||
args[index] = arg; | ||
} | ||
}; | ||
|
||
function parser(args) { | ||
var object = {}; | ||
var regs = { | ||
flag: /--(.+)/, | ||
flaghWithEqual: /--(.+)=(.+)/, | ||
shortFlag: /-(.+)/ | ||
}; | ||
|
||
args = setArgs(args); | ||
args.forEach(parse); | ||
|
||
return object; | ||
|
||
function parse(arg, index, array) { | ||
var key = void 0; | ||
var value = true; | ||
|
||
var isLongFlagWithEqual = regs.flaghWithEqual.test(arg); | ||
var isLongFlag = regs.flag.test(arg); | ||
var isShortFlag = regs.shortFlag.test(arg); | ||
var isValue = regs.flag.test(array[index - 1]); | ||
|
||
if (isLongFlagWithEqual) { | ||
var explode = arg.match(regs.flaghWithEqual); | ||
key = explode[1]; | ||
value = explode[2]; | ||
} else if (isLongFlag) { | ||
key = arg.replace(regs.flag, '$1'); | ||
} else if (isShortFlag) { | ||
key = arg.replace(regs.shortFlag, '$1').split(''); | ||
} else if (isValue) { | ||
key = array[index - 1].replace(regs.flag, '$1'); | ||
value = arg; | ||
} | ||
|
||
value = typeCast(value); | ||
|
||
if (key) { | ||
// if has array (short flags) | ||
if (Array.isArray(key)) { | ||
var applyValue = function applyValue(key) { | ||
object[key] = value; | ||
}; | ||
|
||
key.forEach(applyValue); | ||
} | ||
// if has flag or value of flag | ||
else { | ||
object[key] = value; | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
babel app/parser.js --out-file app/parser.es5.js; | ||
babel test/unit/parser.spec.js --out-file test/unit/parser.spec.es5.js; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* globals describe, it */ | ||
'use strict'; | ||
|
||
var expect = require('chai').expect; | ||
var parser = require('../../app/parser.js'); | ||
|
||
describe('parser - unit tests', function () { | ||
describe('short flags (-f, -abc)', shortFlags); | ||
describe('long flags (--test, --env production)', longFlags); | ||
|
||
function shortFlags() { | ||
it('parse to true values', function () { | ||
expect(parser('node index.js -f')).to.deep.equal({ f: true }); | ||
|
||
expect(parser('node index.js -abc')).to.deep.equal({ | ||
a: true, | ||
b: true, | ||
c: true | ||
}); | ||
}); | ||
} | ||
|
||
function longFlags() { | ||
it('parse to boolean values', function () { | ||
expect(parser('node index.js --test')).to.deep.equal({ test: true }); | ||
|
||
expect(parser('node index.js --test true --test2')).to.deep.equal({ | ||
test: true, | ||
test2: true | ||
}); | ||
|
||
expect(parser('node index.js --test=true --test2="true"')).to.deep.equal({ | ||
test: true, | ||
test2: true | ||
}); | ||
|
||
expect(parser('node index.js --test=false --test2="false"')).to.deep.equal({ | ||
test: false, | ||
test2: false | ||
}); | ||
|
||
expect(parser('node index.js --no false')).to.deep.equal({ no: false }); | ||
}); | ||
|
||
it('parse to number values', function () { | ||
expect(parser('node index.js --number 3 --n 2')).to.deep.equal({ | ||
number: 3, | ||
n: 2 | ||
}); | ||
|
||
expect(parser('node index.js --number "3" --n "2"')).to.deep.equal({ | ||
number: 3, | ||
n: 2 | ||
}); | ||
|
||
expect(parser('node index.js --number="3" --n=2')).to.deep.equal({ | ||
number: 3, | ||
n: 2 | ||
}); | ||
}); | ||
|
||
it('parse to string values', function () { | ||
expect(parser('node index.js --test production --env sandbox')).to.deep.equal({ | ||
test: 'production', | ||
env: 'sandbox' | ||
}); | ||
|
||
expect(parser('node index.js --test "production"')).to.deep.equal({ | ||
test: 'production' | ||
}); | ||
|
||
expect(parser('node index.js --test \'production\'')).to.deep.equal({ | ||
test: 'production' | ||
}); | ||
|
||
expect(parser('node index.js --test=production')).to.deep.equal({ | ||
test: 'production' | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters