[Pkg-javascript-commits] [node-dargs] 46/54: [BREAKING] ES2015ify

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:05 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-dargs.

commit c9ff67776b08de4a8ac48fdd7b22707425bccc7d
Author: Sindre Sorhus <sindresorhus at gmail.com>
Date:   Fri Jul 1 00:52:00 2016 +0200

    [BREAKING] ES2015ify
    
    Now requires Node.js >=4
---
 .editorconfig |  3 ---
 .travis.yml   |  4 +---
 index.js      | 76 ++++++++++++++++++++++++++---------------------------------
 package.json  | 42 ++++++++++++++++-----------------
 readme.md     | 38 +++++++++++++-----------------
 test.js       | 20 ++++++++--------
 6 files changed, 82 insertions(+), 101 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 8f9d77e..98a761d 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -10,6 +10,3 @@ insert_final_newline = true
 [{package.json,*.yml}]
 indent_style = space
 indent_size = 2
-
-[*.md]
-trim_trailing_whitespace = false
diff --git a/.travis.yml b/.travis.yml
index a78e23d..b18bae5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,5 @@
 sudo: false
 language: node_js
 node_js:
-  - '5'
+  - '6'
   - '4'
-  - '0.12'
-  - '0.10'
diff --git a/index.js b/index.js
index cc2afab..d3aa2c5 100644
--- a/index.js
+++ b/index.js
@@ -1,50 +1,40 @@
 'use strict';
-var numberIsNan = require('number-is-nan');
 
-function createArg(key, val) {
-	key = key.replace(/[A-Z]/g, '-$&').toLowerCase();
+const match = (arr, val) => arr.some(x => x instanceof RegExp ? x.test(val) : x === val);
 
-	var ret = ['--' + key];
+module.exports = (input, opts) => {
+	const args = [];
+	let extraArgs = [];
 
-	if (val) {
-		ret.push(val);
-	}
+	opts = Object.assign({
+		useEquals: true
+	}, opts);
 
-	return ret;
-}
+	const makeArg = (key, val) => {
+		key = '--' + key.replace(/[A-Z]/g, '-$&').toLowerCase(); // eslint-disable-line prefer-template
 
-function match(arr, val) {
-	return arr.some(function (x) {
-		return x instanceof RegExp ? x.test(val) : x === val;
-	});
-}
-
-module.exports = function (input, opts) {
-	var args = [];
-	var extraArgs = [];
+		if (opts.useEquals) {
+			args.push(key + (val ? `=${val}` : ''));
+		} else {
+			args.push(key);
 
-	opts = opts || {};
+			if (val) {
+				args.push(val);
+			}
+		}
+	};
 
-	var createAliasArg = function (key, val) {
-		args.push('-' + key);
+	const makeAliasArg = (key, val) => {
+		args.push(`-${key}`);
 
 		if (val) {
 			args.push(val);
 		}
 	};
 
-	Object.keys(input).forEach(function (key) {
-		var val = input[key];
-
-		var argFn = (key, val) => {
-			var arg = createArg(key, val);
-
-			if (opts.useEquals === false) {
-				args.push.apply(args, arg);
-			} else {
-				args.push(arg.join('='));
-			}
-		};
+	Object.keys(input).forEach(key => {
+		const val = input[key];
+		let pushArg = makeArg;
 
 		if (Array.isArray(opts.excludes) && match(opts.excludes, key)) {
 			return;
@@ -56,12 +46,12 @@ module.exports = function (input, opts) {
 
 		if (typeof opts.aliases === 'object' && opts.aliases[key]) {
 			key = opts.aliases[key];
-			argFn = createAliasArg;
+			pushArg = makeAliasArg;
 		}
 
 		if (key === '_') {
 			if (!Array.isArray(val)) {
-				throw new TypeError('Expected key \'_\' to be an array, but found ' + (typeof val));
+				throw new TypeError(`Expected key \`_\` to be Array, got ${typeof val}`);
 			}
 
 			extraArgs = val;
@@ -69,29 +59,29 @@ module.exports = function (input, opts) {
 		}
 
 		if (val === true) {
-			argFn(key, '');
+			pushArg(key, '');
 		}
 
 		if (val === false && !opts.ignoreFalse) {
-			argFn('no-' + key);
+			pushArg(`no-${key}`);
 		}
 
 		if (typeof val === 'string') {
-			argFn(key, val);
+			pushArg(key, val);
 		}
 
-		if (typeof val === 'number' && !numberIsNan(val)) {
-			argFn(key, String(val));
+		if (typeof val === 'number' && !Number.isNaN(val)) {
+			pushArg(key, String(val));
 		}
 
 		if (Array.isArray(val)) {
-			val.forEach(function (arrVal) {
-				argFn(key, arrVal);
+			val.forEach(arrVal => {
+				pushArg(key, arrVal);
 			});
 		}
 	});
 
-	extraArgs.forEach(function (extraArgVal) {
+	extraArgs.forEach(extraArgVal => {
 		args.push(String(extraArgVal));
 	});
 
diff --git a/package.json b/package.json
index 61e9bb3..645c3c4 100644
--- a/package.json
+++ b/package.json
@@ -2,20 +2,36 @@
   "name": "dargs",
   "version": "4.1.0",
   "description": "Reverse minimist. Convert an object of options into an array of command-line arguments.",
+  "license": "MIT",
   "repository": "sindresorhus/dargs",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=4"
+  },
+  "scripts": {
+    "test": "xo && ava"
+  },
+  "files": [
+    "index.js"
+  ],
   "keywords": [
+    "reverse",
+    "minimist",
     "options",
     "arguments",
     "args",
     "flags",
     "cli",
     "nopt",
-    "minimist",
+    "commander",
     "bin",
     "binary",
     "command",
     "cmd",
-    "reverse",
     "inverse",
     "opposite",
     "invert",
@@ -25,27 +41,11 @@
     "parser",
     "argv"
   ],
-  "author": {
-    "name": "Sindre Sorhus",
-    "email": "sindresorhus at gmail.com",
-    "url": "sindresorhus.com"
-  },
-  "scripts": {
-    "test": "xo && ava"
-  },
-  "dependencies": {
-    "number-is-nan": "^1.0.0"
-  },
   "devDependencies": {
-    "array-equal": "^1.0.0",
     "ava": "*",
     "xo": "*"
   },
-  "engines": {
-    "node": ">=0.10.0"
-  },
-  "license": "MIT",
-  "files": [
-    "index.js"
-  ]
+  "xo": {
+    "esnext": true
+  }
 }
diff --git a/readme.md b/readme.md
index aa1c6fc..192af30 100644
--- a/readme.md
+++ b/readme.md
@@ -12,7 +12,7 @@ $ npm install --save dargs
 ```
 
 
-#### Usage
+## Usage
 
 ```js
 const dargs = require('dargs');
@@ -32,7 +32,7 @@ const excludes = ['sad', /.*Kind$/];  // excludes and includes accept regular ex
 const includes = ['camelCase', 'multiple', 'sad', /^pie.*/];
 const aliases = {file: 'f'};
 
-console.log(dargs(input, {excludes: excludes}));
+console.log(dargs(input, {excludes}));
 /*
 [
 	'--foo=bar',
@@ -46,10 +46,7 @@ console.log(dargs(input, {excludes: excludes}));
 ]
 */
 
-console.log(dargs(input, {
-	excludes: excludes,
-	includes: includes
-}));
+console.log(dargs(input, {excludes, includes}));
 /*
 [
 	'--camel-case=5',
@@ -59,7 +56,7 @@ console.log(dargs(input, {
 */
 
 
-console.log(dargs(input, {includes: includes}));
+console.log(dargs(input, {includes}));
 /*
 [
 	'--camel-case=5',
@@ -75,9 +72,7 @@ console.log(dargs({
 	foo: 'bar',
 	hello: true,
 	file: 'baz'
-}, {
-	aliases: aliases
-}));
+}, {aliases}));
 /*
 [
 	'--foo=bar',
@@ -87,41 +82,42 @@ console.log(dargs({
 */
 ```
 
+
 ## API
 
 ### dargs(input, [options])
 
 #### input
 
-Type: `object`
+Type: `Object`
 
 Object to convert to command-line arguments.
 
 #### options
 
-Type: `object`
+Type: `Object`
 
 ##### excludes
 
-Type: `array`
+Type: `Array`
 
 Keys or regex of keys to exclude. Takes precedence over `includes`.
 
 ##### includes
 
-Type: `array`
+Type: `Array`
 
 Keys or regex of keys to include.
 
 ##### aliases
 
-Type: `object`
+Type: `Object`
 
-Maps keys in `input` to an aliased name. Matching keys are converted to options with a single dash ("-") in front of the aliased name and a space separating the aliased name from the value. Keys are still affected by `includes` and `excludes`.
+Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
 
 ##### useEquals
 
-Type: `boolean`  
+Type: `boolean`<br>
 Default: `true`
 
 Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags. For example:
@@ -130,19 +126,19 @@ Setting this to `false` makes it return the key and value as separate array item
 console.log(dargs({foo: 'bar'}, {useEquals: false}));
 /*
 [
-    '--foo', 'bar'
+	'--foo', 'bar'
 ]
 */
 ```
 
 ##### ignoreFalse
 
-Type: `boolean`  
+Type: `boolean`<br>
 Default: `false`
 
-Don't include `false` values. This is mostly useful when dealing with strict argument parsers that would throw on unknown arguments like `--no-foo`.
+Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
 
 
 ## License
 
-MIT © [Sindre Sorhus](http://sindresorhus.com)
+MIT © [Sindre Sorhus](https://sindresorhus.com)
diff --git a/test.js b/test.js
index cb39bff..1b5eefc 100644
--- a/test.js
+++ b/test.js
@@ -1,5 +1,5 @@
 import test from 'ava';
-import fn from './';
+import m from './';
 
 const fixture = {
 	_: ['some', 'option'],
@@ -16,7 +16,7 @@ const fixture = {
 };
 
 test('convert options to cli flags', t => {
-	t.deepEqual(fn(fixture), [
+	t.deepEqual(m(fixture), [
 		'--a=foo',
 		'--b',
 		'--no-c',
@@ -32,11 +32,11 @@ test('convert options to cli flags', t => {
 });
 
 test('raises a TypeError if  \'_\' value is not an Array', t => {
-	t.throws(fn.bind(fn, {a: 'foo', _: 'baz'}), TypeError);
+	t.throws(m.bind(m, {a: 'foo', _: 'baz'}), TypeError);
 });
 
 test('useEquals options', t => {
-	t.deepEqual(fn(fixture, {
+	t.deepEqual(m(fixture, {
 		useEquals: false
 	}), [
 		'--a', 'foo',
@@ -54,7 +54,7 @@ test('useEquals options', t => {
 });
 
 test('exclude options', t => {
-	t.deepEqual(fn(fixture, {excludes: ['b', /^e$/, 'h', 'i']}), [
+	t.deepEqual(m(fixture, {excludes: ['b', /^e$/, 'h', 'i']}), [
 		'--a=foo',
 		'--no-c',
 		'--d=5',
@@ -65,7 +65,7 @@ test('exclude options', t => {
 });
 
 test('includes options', t => {
-	t.deepEqual(fn(fixture, {includes: ['a', 'c', 'd', 'e', /^camelCase.*/]}), [
+	t.deepEqual(m(fixture, {includes: ['a', 'c', 'd', 'e', /^camelCase.*/]}), [
 		'--a=foo',
 		'--no-c',
 		'--d=5',
@@ -76,7 +76,7 @@ test('includes options', t => {
 });
 
 test('excludes and includes options', t => {
-	t.deepEqual(fn(fixture, {
+	t.deepEqual(m(fixture, {
 		excludes: ['a', 'd'],
 		includes: ['a', 'c', /^[de]$/, 'camelCaseCamel']
 	}), [
@@ -88,11 +88,11 @@ test('excludes and includes options', t => {
 });
 
 test('option to ignore false values', t => {
-	t.deepEqual(fn({foo: false}, {ignoreFalse: true}), []);
+	t.deepEqual(m({foo: false}, {ignoreFalse: true}), []);
 });
 
 test('aliases option', t => {
-	t.deepEqual(fn({a: 'foo', file: 'test'}, {
+	t.deepEqual(m({a: 'foo', file: 'test'}, {
 		aliases: {file: 'f'}
 	}), [
 		'--a=foo',
@@ -101,7 +101,7 @@ test('aliases option', t => {
 });
 
 test('includes and aliases options', t => {
-	t.deepEqual(fn(fixture, {
+	t.deepEqual(m(fixture, {
 		includes: ['a', 'c', 'd', 'e', 'camelCaseCamel'],
 		aliases: {a: 'a'}
 	}), [

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-dargs.git



More information about the Pkg-javascript-commits mailing list