From d3b17beeb00ecc35fb53b89efa9cbaf26c4181c7 Mon Sep 17 00:00:00 2001 From: Amit Dhamu Date: Tue, 3 May 2022 20:49:44 +0100 Subject: [PATCH] v1.1.1 --- README.md | 20 ++++++++++-------- esbuild.config.js | 2 +- package.json | 2 +- src/__tests__/index.test.ts | 2 +- src/index.ts | 42 +++++++++++++++++++------------------ 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 2724e20..262bb66 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,27 @@ A tiny package to parse CLI flags and arguments into an object. -## Requirements - -- Node 16+ - ## Installation ```shell yarn add arge + +# or +npm i arge ``` ## Usage -```typescript +```javascript +const { arge } = require('arge') + +// or import { arge } from 'arge' +``` +Then: + +```typescript const args = arge(process.argv) ``` @@ -72,8 +78,6 @@ If you wanted to pass an arbitrary array of flags that don't come from `process. For example ```typescript -import { arge } from 'arge' - const flags = [ '--dry-run', '--mode=development', @@ -104,8 +108,6 @@ Default: `true` This converts hyphen separated keys into camel case. To prevent this behaviour, you can set this value to `false`. ```typescript -import { arge } from 'arge' - const flags = ['--dry-run'] const args = arge(flags, { camelCaseKeys: false }) diff --git a/esbuild.config.js b/esbuild.config.js index 3c68340..d7edccc 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -7,7 +7,7 @@ const options = { external: Object.keys(require('./package.json').dependencies || {}), logLevel: 'info', minify: true, - target: ['esnext'], + target: [require('./tsconfig.json').compilerOptions.target], } build({ diff --git a/package.json b/package.json index 8585fda..e04cfd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arge", - "version": "1.1.0", + "version": "1.1.1", "description": "A simple utility to parse command line arguments and flags", "keywords": [ "argv", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 7aa06ea..ec24248 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -7,7 +7,7 @@ describe('arge', () => { const argv = [ ...baseArgs, '-f', - '--dryRun', + '--dry-run', '--mode=development', '--test=false', '--retries=100', diff --git a/src/index.ts b/src/index.ts index db2911f..452fb07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,24 +3,27 @@ interface Options { camelCaseKeys?: boolean } -const parse = (value: string | boolean | number) => { - if (['true', 'false', true, false].includes(value as string)) { - return JSON.parse(value as string) +const toArray = (value: string, delimiter: string) => + value + .split(delimiter) + .map(v => v.trim()) + .filter(v => v) + +const parse = (value: string): string | boolean | number | string[] => { + if (['true', 'false', true, false].includes(value)) { + return JSON.parse(value) } if (!Number.isNaN(Number(value))) { - return parseFloat(value as string) + return parseFloat(value) } - if ((value as string).includes(',')) { - return (value as string).split(',').map(v => v.trim()) + if (value.includes(',')) { + return toArray(value, ',') } - if ((value as string).includes(' ')) { - return (value as string) - .split(' ') - .map(v => v.trim()) - .filter(v => !!v) + if (value.includes(' ')) { + return toArray(value, ' ') } return value @@ -32,21 +35,20 @@ export const arge = ( isArgv: true, camelCaseKeys: true, } -) => { - const flags = - options?.isArgv !== false ? args.filter((_, index) => index > 1) : args - - return flags.reduce((acc, curr) => { - const [k, v] = curr.split('=') +) => + (options.isArgv !== false + ? args.filter((_, index) => index > 1) + : args + ).reduce((acc, curr) => { + const [k, v = 'true'] = curr.split('=') let key = k.replace(/^-+/, '') - key = options?.camelCaseKeys + key = options.camelCaseKeys ? key.replace(/-([a-z])/g, g => g[1].toUpperCase()) : key return { ...acc, - [key]: parse(v || true), + [key]: parse(v), } }, {}) -}