Skip to content

Commit

Permalink
[eslint] more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 16, 2022
1 parent e5f5067 commit 36ac5d0
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 156 deletions.
35 changes: 12 additions & 23 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,25 @@

"rules": {
"array-element-newline": 0,
"camelcase": 1,
"comma-dangle": 1,
"complexity": 0,
"curly": 1,
"dot-notation": 1,
"func-style": 1,
"func-style": [2, "declaration"],
"max-lines-per-function": 0,
"max-nested-callbacks": 1,
"max-statements-per-line": 1,
"max-statements": 0,
"multiline-comment-style": 0,
"no-array-constructor": 1,
"no-continue": 1,
"no-div-regex": 1,
"no-extra-parens": 1,
"no-mixed-operators": 1,
"no-param-reassign": 1,
"no-plusplus": 1,
"no-proto": 1,
"no-redeclare": 1,
"no-restricted-syntax": 1,
"no-shadow": 1,
"no-unused-vars": 1,
"no-use-before-define": 1,
"object-curly-newline": 1,
"operator-linebreak": 1,
"quote-props": 1,
"quotes": 1,
"semi-style": 1,
"semi": 1,
"strict": 1,
"wrap-regex": 1,
"object-curly-newline": 0,
},

"overrides": [
{
"files": "test/**",
"rules": {
"camelcase": 0,
},
},
]
}
2 changes: 2 additions & 0 deletions example/parse.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
'use strict';

var argv = require('../')(process.argv.slice(2));
console.dir(argv);
192 changes: 102 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
'use strict';

function isNumber(x) {
if (typeof x === 'number') { return true; }
if ((/^0x[0-9a-f]+$/i).test(x)) { return true; }
return (/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/).test(x);
}

function hasKey(obj, keys) {
var o = obj;
keys.slice(0, -1).forEach(function (key) {
o = o[key] || {};
});

var key = keys[keys.length - 1];
return key in o;
}

function setKey(obj, keys, value) {
var o = obj;
var key;
for (var i = 0; i < keys.length - 1; i++) {
key = keys[i];
if (key === '__proto__') { return; }
if (o[key] === undefined) { o[key] = {}; }
if (
o[key] === Object.prototype
|| o[key] === Number.prototype
|| o[key] === String.prototype
) {
o[key] = {};
}
if (o[key] === Array.prototype) { o[key] = []; }
o = o[key];
}

key = keys[keys.length - 1];
if (key === '__proto__') { return; }
if (
o === Object.prototype
|| o === Number.prototype
|| o === String.prototype
) {
o = {};
}
if (o === Array.prototype) { o = []; }
if (o[key] === undefined || typeof o[key] === 'boolean') {
o[key] = value;
} else if (Array.isArray(o[key])) {
o[key].push(value);
} else {
o[key] = [o[key], value];
}
}

module.exports = function (args, opts) {
if (!opts) opts = {};
if (!opts) { opts = {}; }

var flags = { bools: {}, strings: {} };

if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
if (typeof opts.boolean === 'boolean' && opts.boolean) {
flags.allBools = true;
} else {
[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
[].concat(opts.boolean).filter(Boolean).forEach(function (key) {
flags.bools[key] = true;
});
}
Expand All @@ -28,9 +83,21 @@ module.exports = function (args, opts) {
}
});

var defaults = opts['default'] || {};
var defaults = opts.default || {};

var argv = { _: [] };

function setArg(key, val) {
var value = !flags.strings[key] && isNumber(val)
? Number(val)
: val;
setKey(argv, key.split('.'), value);

(aliases[key] || []).forEach(function (x) {
setKey(argv, x.split('.'), value);
});
}

Object.keys(flags.bools).forEach(function (key) {
setArg(key, defaults[key] === undefined ? false : defaults[key]);
});
Expand All @@ -42,58 +109,49 @@ module.exports = function (args, opts) {
args = args.slice(0, args.indexOf('--'));
}

function setArg(key, val) {
var value = !flags.strings[key] && isNumber(val)
? Number(val) : val
;
setKey(argv, key.split('.'), value);

(aliases[key] || []).forEach(function (x) {
setKey(argv, x.split('.'), value);
});
}

for (var i = 0; i < args.length; i++) {
var arg = args[i];
var key;
var next;

if (/^--.+=/.test(arg)) {
if ((/^--.+=/).test(arg)) {
// Using [\s\S] instead of . because js doesn't support the
// 'dotall' regex modifier. See:
// http://stackoverflow.com/a/1068308/13216
var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
setArg(m[1], m[2]);
} else if (/^--no-.+/.test(arg)) {
var key = arg.match(/^--no-(.+)/)[1];
} else if ((/^--no-.+/).test(arg)) {
key = arg.match(/^--no-(.+)/)[1];
setArg(key, false);
} else if (/^--.+/.test(arg)) {
var key = arg.match(/^--(.+)/)[1];
var next = args[i + 1];
if (next !== undefined && !/^-/.test(next)
} else if ((/^--.+/).test(arg)) {
key = arg.match(/^--(.+)/)[1];
next = args[i + 1];
if (next !== undefined && !(/^-/).test(next)
&& !flags.bools[key]
&& !flags.allBools
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, next);
i++;
} else if (/^(true|false)$/.test(next)) {
i += 1;
} else if ((/^(true|false)$/).test(next)) {
setArg(key, next === 'true');
i++;
i += 1;
} else {
setArg(key, flags.strings[key] ? '' : true);
}
} else if (/^-[^-]+/.test(arg)) {
} else if ((/^-[^-]+/).test(arg)) {
var letters = arg.slice(1, -1).split('');

var broken = false;
for (var j = 0; j < letters.length; j++) {
var next = arg.slice(j + 2);
next = arg.slice(j + 2);

if (next === '-') {
setArg(letters[j], next)
setArg(letters[j], next);
continue;
}

if (/[A-Za-z]/.test(letters[j])
&& /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
if ((/[A-Za-z]/).test(letters[j])
&& (/-?\d+(\.\d*)?(e-?\d+)?$/).test(next)) {
setArg(letters[j], next);
broken = true;
break;
Expand All @@ -108,88 +166,42 @@ module.exports = function (args, opts) {
}
}

var key = arg.slice(-1)[0];
key = arg.slice(-1)[0];
if (!broken && key !== '-') {
if (args[i + 1] && !/^(-|--)[^-]/.test(args[i + 1])
if (args[i + 1] && !(/^(-|--)[^-]/).test(args[i + 1])
&& !flags.bools[key]
&& (aliases[key] ? !flags.bools[aliases[key]] : true)) {
setArg(key, args[i + 1]);
i++;
} else if (args[i + 1] && /true|false/.test(args[i + 1])) {
i += 1;
} else if (args[i + 1] && (/true|false/).test(args[i + 1])) {
setArg(key, args[i + 1] === 'true');
i++;
i += 1;
} else {
setArg(key, flags.strings[key] ? '' : true);
}
}
} else {
argv._.push(flags.strings['_'] || !isNumber(arg) ? arg : Number(arg));
argv._.push(flags.strings._ || !isNumber(arg) ? arg : Number(arg));
}
}

Object.keys(defaults).forEach(function (key) {
if (!hasKey(argv, key.split('.'))) {
setKey(argv, key.split('.'), defaults[key]);
Object.keys(defaults).forEach(function (k) {
if (!hasKey(argv, k.split('.'))) {
setKey(argv, k.split('.'), defaults[k]);

(aliases[key] || []).forEach(function (x) {
setKey(argv, x.split('.'), defaults[key]);
(aliases[k] || []).forEach(function (x) {
setKey(argv, x.split('.'), defaults[k]);
});
}
});

if (opts['--']) {
argv['--'] = new Array();
notFlags.forEach(function (key) {
argv['--'].push(key);
});
argv['--'] = notFlags.slice();
} else {
notFlags.forEach(function (key) {
argv._.push(key);
notFlags.forEach(function (k) {
argv._.push(k);
});
}

return argv;
};

function hasKey(obj, keys) {
var o = obj;
keys.slice(0, -1).forEach(function (key) {
o = (o[key] || {});
});

var key = keys[keys.length - 1];
return key in o;
}

function setKey(obj, keys, value) {
var o = obj;
for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i];
if (key === '__proto__') return;
if (o[key] === undefined) o[key] = {};
if (o[key] === Object.prototype || o[key] === Number.prototype
|| o[key] === String.prototype) o[key] = {};
if (o[key] === Array.prototype) o[key] = [];
o = o[key];
}

var key = keys[keys.length - 1];
if (key === '__proto__') return;
if (o === Object.prototype || o === Number.prototype
|| o === String.prototype) o = {};
if (o === Array.prototype) o = [];
if (o[key] === undefined || typeof o[key] === 'boolean') {
o[key] = value;
} else if (Array.isArray(o[key])) {
o[key].push(value);
} else {
o[key] = [o[key], value];
}
}

function isNumber(x) {
if (typeof x === 'number') return true;
if (/^0x[0-9a-f]+$/i.test(x)) return true;
return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
}

10 changes: 6 additions & 4 deletions test/all_bool.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

var parse = require('../');
var test = require('tape');

test('flag boolean true (default all --args to boolean)', function (t) {
var argv = parse(['moo', '--honk', 'cow'], {
boolean: true
boolean: true,
});

t.deepEqual(argv, {
honk: true,
_: ['moo', 'cow']
_: ['moo', 'cow'],
});

t.deepEqual(typeof argv.honk, 'boolean');
Expand All @@ -17,14 +19,14 @@ test('flag boolean true (default all --args to boolean)', function (t) {

test('flag boolean true only affects double hyphen arguments without equals signs', function (t) {
var argv = parse(['moo', '--honk', 'cow', '-p', '55', '--tacos=good'], {
boolean: true
boolean: true,
});

t.deepEqual(argv, {
_: ['moo', 'cow'],
honk: true,
p: 55,
tacos: 'good'
tacos: 'good',
});

t.deepEqual(typeof argv.honk, 'boolean');
Expand Down
Loading

0 comments on commit 36ac5d0

Please sign in to comment.