Skip to content

Commit

Permalink
Add support for camelCasing option property names
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed Dec 3, 2015
1 parent b2a222b commit 2691b5c
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions index.js
Expand Up @@ -95,10 +95,29 @@ class Option{



function getOpts(input, optdef){
let shortNames = {};
let longNames = {};
let result = {
/**
* Convert a kebab-cased-string into a camelCasedString.
*
* @param {String} input
* @return {String}
*/
function kebabToCamelCase(input){
return input.toLowerCase().replace(/([a-z])-+([a-z])/g, function(match, a, b){
return a + b.toUpperCase();
});
}


function getOpts(input, optdef, config){

/** Optional options hash controlling option-creation */
config = config || {};
let noCamelCase = config.noCamelCase;


let shortNames = {};
let longNames = {};
let result = {
options: new Object(null),
argv: []
};
Expand Down Expand Up @@ -139,7 +158,14 @@ function getOpts(input, optdef){
if(currentOption.arity === 1)
optValue = optValue[0];

currentOption.names.forEach(n => {result.options[n] = optValue});
currentOption.names.forEach(n => {

/** Decide whether to camelCase this option name */
if(!noCamelCase && /-/.test(n))
n = kebabToCamelCase(n);

result.options[n] = optValue;
});
currentOption = null;
};

Expand Down

0 comments on commit 2691b5c

Please sign in to comment.