From 975eb31ced14a1ff5cf4aae4e5d07bd422d6b33d Mon Sep 17 00:00:00 2001 From: gemwuu Date: Wed, 19 Jan 2022 15:22:46 +0800 Subject: [PATCH 1/2] feat(options): add delimiter for unparse --- index.js | 26 +++++++++++++++++++------- package.json | 10 +++++----- test/index.spec.js | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index c2b7581..c5b53f5 100644 --- a/index.js +++ b/index.js @@ -57,26 +57,38 @@ function parseCommand(cmd) { return parsedCommand; } -function unparseOption(key, value, unparsed) { +function unparseOption(key, value, unparsed, options) { + const delimiter = options.delimiter; + const useEqualSign = delimiter === '='; + const flaggedKey = keyToFlag(key); + if (typeof value === 'string') { - unparsed.push(keyToFlag(key), value); + if (useEqualSign) { + unparsed.push(`${flaggedKey}=${value}`); + } else { + unparsed.push(flaggedKey, value); + } } else if (value === true) { - unparsed.push(keyToFlag(key)); + unparsed.push(flaggedKey); } else if (value === false) { unparsed.push(`--no-${key}`); } else if (Array.isArray(value)) { - value.forEach((item) => unparseOption(key, item, unparsed)); + value.forEach((item) => unparseOption(key, item, unparsed, options)); } else if (isPlainObj(value)) { const flattened = flatten(value, { safe: true }); for (const flattenedKey in flattened) { if (!isCamelCased(flattenedKey, flattened)) { - unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed); + unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed, options); } } // Fallback case (numbers and other types) } else if (value != null) { - unparsed.push(keyToFlag(key), `${value}`); + if (useEqualSign) { + unparsed.push(`${flaggedKey}=${value}`); + } else { + unparsed.push(flaggedKey, `${value}`); + } } } @@ -130,7 +142,7 @@ function unparseOptions(argv, options, knownPositional, unparsed) { continue; } - unparseOption(key, argv[key], unparsed); + unparseOption(key, argv[key], unparsed, options); } } diff --git a/package.json b/package.json index 1cd24a1..a48e17c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "yargs-unparser", + "name": "yargs-unparser-bak", "description": "Converts back a yargs argv object to its original array form", - "version": "2.0.0", + "version": "1.0.0", "keywords": [ "yargs", "unparse", @@ -9,12 +9,12 @@ "inverse", "argv" ], - "author": "André Cruz ", + "author": "gemwuu ", "engines": { "node": ">=10" }, - "homepage": "https://github.com/yargs/yargs-unparser", - "repository": "yargs/yargs-unparser", + "homepage": "https://github.com/gemwuu/yargs-unparser.git", + "repository": "gemwuu/yargs-unparser", "license": "MIT", "main": "index.js", "files": [], diff --git a/test/index.spec.js b/test/index.spec.js index a050d8e..ea0f38e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -219,3 +219,26 @@ describe('interoperation with other libraries', () => { }); }); }); + +describe('use equal mark as delimiter', () => { + it('should success', () => { + const argv = parse(['--no-cache', '--optimize', '--host', '0.0.0.0', '--collect', 'x', 'y', '--env', 'node', '--env', 'python'], { + boolean: ['cache', 'optimize'], + string: 'host', + array: ['collect', 'env'], + }); + const argvArray = unparse(argv, { + delimiter: '=', + }); + + expect(argvArray).toEqual(['--no-cache', '--optimize', '--host=0.0.0.0', '--collect=x', '--collect=y', '--env=node', '--env=python']); + + expect(minimist(argvArray)).toMatchObject({ + cache: false, + optimize: true, + host: '0.0.0.0', + collect: ['x', 'y'], + env: ['node', 'python'], + }); + }); +}); From 470c324f85f1ccb10f0983f4a18ea41f26f527bd Mon Sep 17 00:00:00 2001 From: gemwuu Date: Wed, 19 Jan 2022 16:29:05 +0800 Subject: [PATCH 2/2] fix: revert package.json --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a48e17c..1cd24a1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "yargs-unparser-bak", + "name": "yargs-unparser", "description": "Converts back a yargs argv object to its original array form", - "version": "1.0.0", + "version": "2.0.0", "keywords": [ "yargs", "unparse", @@ -9,12 +9,12 @@ "inverse", "argv" ], - "author": "gemwuu ", + "author": "André Cruz ", "engines": { "node": ">=10" }, - "homepage": "https://github.com/gemwuu/yargs-unparser.git", - "repository": "gemwuu/yargs-unparser", + "homepage": "https://github.com/yargs/yargs-unparser", + "repository": "yargs/yargs-unparser", "license": "MIT", "main": "index.js", "files": [],