argv2object 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,6 +7,7 @@ A utility function for converting command-line arguments to a key-value object.
7
7
  - [Installation](#installation)
8
8
  - [Importing](#importing)
9
9
  - [Usage](#usage)
10
+ - [Dual Module Support](#dual-module-support)
10
11
  - [Examples](#examples)
11
12
  - [Simple key-value pairs](#simple-key-value-pairs)
12
13
  - [Unix-style command-line options](#unix-style-command-line-options)
@@ -61,34 +62,49 @@ console.log(args.name); // "John"
61
62
 
62
63
  ```
63
64
 
65
+ ## Dual Module Support
66
+
67
+ This package supports both:
68
+
69
+ - ES Modules: `import argv2object from 'argv2object'`
70
+ - CommonJS: `const argv2object = require('argv2object')`
71
+
72
+ The architecture uses separate entry points while sharing the same implementation.
73
+
74
+ ## System Requirements
75
+
76
+ - **Node.js 14+** (Recommended for full support)
77
+ - **Node.js 12.x** (Experimental support)
78
+ - **Versiones anteriores**: Use version 1.x of this package
79
+
64
80
  ## Examples
65
81
 
66
82
  ### Simple key-value pairs
67
83
 
68
84
  ```sh
69
85
  # Command line input:
70
- node script.js task=some-task name=John level=admin
86
+ node script.js task=some-task age=30 name=John level=admin
71
87
 
72
88
  ```
73
89
 
74
90
  ```js
75
91
  const args = argv2Object();
76
92
  console.log(args);
77
- // Output: { name: 'John', age: '30', level: 'admin' }
93
+ // Output: { name: 'John', age: 30, level: 'admin' }
78
94
  ```
79
95
 
80
96
  ### Unix-style command-line options
81
97
 
82
98
  ```sh
83
99
  # Command line input:
84
- node --watch script.js -h --name=John --is-admin
100
+ node --watch script.js -h --help --name=John --is-admin
85
101
 
86
102
  ```
87
103
 
88
104
  ```js
89
105
  const args = argv2Object(true);
90
106
  console.log(args);
91
- // Output: { h: true, name: 'John', is_admin: true }
107
+ // Output: { h: true, help: true, name: 'John', is_admin: true }
92
108
  ```
93
109
 
94
110
  ## API
@@ -113,11 +129,12 @@ Returns an `Object` with keys and values corresponding to the provided arguments
113
129
 
114
130
  Throws
115
131
 
116
- | Type | Description |
117
- |---------|---------------------------------------------------------------------------------------------|
118
- | `Error` | If no arguments are provided from command line. |
119
- | `Error` | If `unixmode` is `true` and an argument does not follow the Unix-style command-line format. |
120
- | `Error` | If `unixmode` is `false` and an argument does not follow the "key=value" format. |
132
+ | Type | Description |
133
+ |---------|-------------------------------------------------------------------------------------------------|
134
+ | `TypeError` | If `unixmode` is not of type `boolean`. |
135
+ | `Error` | If no arguments are provided from command line. |
136
+ | `Error` | If `unixmode` is `true` and an argument does not follow the Unix-style command-line format. |
137
+ | `Error` | If `unixmode` is `false` and an argument does not follow the "key=value" format. |
121
138
 
122
139
  ## Contributing
123
140
 
package/index.cjs ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @file Entry point for CommonJS consumers.
3
+ *
4
+ * Provides compatibility layer for Node.js environments
5
+ * using CommonJS require() system.
6
+ *
7
+ * This wrapper adapts the ESM default export to CommonJS
8
+ * module.exports pattern.
9
+ *
10
+ * @module argv2object/cjs
11
+ * @see module:argv2Object
12
+ * @since 1.1.0
13
+ * @example
14
+ * // CJS Usage
15
+ * const argv2object = require('argv2object');
16
+ */
17
+
18
+ // ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
19
+ // » IMPORT LOCAL MODULES
20
+ const argv2Object = require('#argv2Object');
21
+
22
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
23
+ module.exports = argv2Object.default;
package/index.mjs CHANGED
@@ -1,22 +1,22 @@
1
1
  /**
2
- * @author Victor Giovanni Beltrán Rodríguez
3
- * @file Manages main entry point.
4
- */
5
-
6
- // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7
- /**
8
- * The `argv2Object()` function, converts command-line arguments to a key-value
9
- * object.
2
+ * @file Entry point for ES Module consumers.
10
3
  *
11
- * This function takes an array of command-line arguments and converts them to a
12
- * JavaScript object with keys and values based on the provided argument format.
13
- * The format can be either simple key-value pairs (e.g. "key=value") or
14
- * Unix-style command-line options (e.g. "-o --option=value").
4
+ * Provides the default export of argv2object functionality
5
+ * for modern JavaScript environments using ES Modules.
15
6
  *
16
- * @version 1.0.0
17
- * @author Victor Giovanni Beltrán Rodríguez
18
- * @module argv2Object
7
+ * This file re-exports the main functionality from the
8
+ * implementation module while maintaining ESM compatibility.
9
+ *
10
+ * @module argv2object/esm
11
+ * @see module:argv2Object
12
+ * @since 1.1.0
13
+ * @example
14
+ * // ESM Usage
15
+ * import argv2object from 'argv2object';
19
16
  */
17
+
18
+ // ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
19
+ // » IMPORT LOCAL MODULES
20
20
  import argv2Object from '#argv2Object';
21
21
 
22
22
  // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "argv2object",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "private": false,
5
5
  "description": "A utility function for converting command-line arguments to a key-value object.",
6
6
  "keywords": [
@@ -38,7 +38,8 @@
38
38
  "type": "module",
39
39
  "main": "index.mjs",
40
40
  "exports": {
41
- ".": "./index.mjs"
41
+ "import": "./index.mjs",
42
+ "require": "./index.cjs"
42
43
  },
43
44
  "imports": {
44
45
  "#argv2Object": "./src/argv2Object.mjs",
@@ -47,7 +48,7 @@
47
48
  },
48
49
  "scripts": {
49
50
  "test": "cross-env NODE_ENV=test node --test test/*.test.mjs",
50
- "lint": "npx eslint",
51
+ "lint": "npx eslint . --ext .mjs --ext .js --ext .cjs",
51
52
  "prepare": "husky install",
52
53
  "prepublish": "npm test"
53
54
  },
@@ -55,19 +56,17 @@
55
56
  "@commitlint/cli": "^19.8.0",
56
57
  "@commitlint/config-conventional": "^19.8.0",
57
58
  "cross-env": "^7.0.3",
58
- "eslint": "^8.42.0",
59
+ "eslint": "^8.57.1",
59
60
  "eslint-config-airbnb-base": "^15.0.0",
60
- "eslint-config-prettier": "^8.8.0",
61
+ "eslint-config-prettier": "^8.10.0",
62
+ "eslint-import-resolver-node": "^0.3.9",
61
63
  "eslint-plugin-import": "^2.27.5",
62
- "eslint-plugin-jsdoc": "^46.2.6",
63
64
  "eslint-plugin-prettier": "^4.2.1",
64
65
  "husky": "^8.0.3",
65
- "jsdoc": "^4.0.2",
66
- "jsdoc-to-markdown": "^8.0.0",
67
- "lint-staged": "^13.2.1",
68
- "prettier": "^2.8.7"
66
+ "lint-staged": "^15.5.2",
67
+ "prettier": "^2.8.8"
69
68
  },
70
69
  "engines": {
71
- "node": ">=12.0.0"
70
+ "node": ">=14.0.0"
72
71
  }
73
72
  }
@@ -1,134 +1,94 @@
1
- /**
2
- * @file Converts command-line arguments to a key-value object.
3
- *
4
- * A utility function for converting command-line arguments to a key-value
5
- * object.
6
- *
7
- * This file defines a single function, `argv2Object`, which parses
8
- * command-line arguments and returns an object with keys and values
9
- * corresponding to the provided arguments. The function can handle two types of
10
- * arguments: simple key-value pairs (e.g. "key=value") and Unix-style
11
- * command-line arguments (e.g. "-k value" or "--key=value").
12
- *
13
- * @author Victor Giovanni Beltrán Rodríguez
14
- * @version 1.0.0
15
- *
16
- * @example
17
- * Simple key-value pairs
18
- * Command line input: node script.js name=John age=30 level=admin
19
- * Output: { name: 'John', age: '30', level: "admin" }
20
- *
21
- * @example
22
- * Unix-style command-line options
23
- * Command line input: node script.js -h --name=John --is-admin
24
- * Output: { h: true, name: 'John', is_admin: true }
25
- */
26
-
27
- // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
28
- /**
29
- * Regular expressions used to validate command-line arguments.
30
- *
31
- * @private
32
- * @typedef {object} Regexps
33
- * @property {RegExp} UNIXMODE - Regular expression for validating Unix-style command-line arguments.
34
- * @property {RegExp} SIMPLE - Regular expression for validating simple key-value arguments.
35
- */
36
-
37
- /**
38
- * Error messages used in case of invalid command-line arguments.
39
- *
40
- * @private
41
- * @typedef {object} Errors
42
- * @property {string} NO_ARGS - Error message for when no arguments are provided.
43
- * @property {string} NO_MATCH_SIMPLE - Error message for when one or more simple key-value arguments do not follow the expected format.
44
- * @property {string} NO_MATCH_UNIXMODE - Error message for when one or more Unix-style command-line arguments do not follow the expected format.
45
- */
46
-
47
- /**
48
- * An `Object` with keys and values corresponding to the provided arguments.
49
- *
50
- * @typedef {object} ArgvObject
51
- * @property {string|boolean} [key=value] - The key-value pairs in the provided arguments.
52
- */
53
-
54
- // ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
55
- /**
56
- * Regular expressions used to validate command-line arguments.
57
- *
58
- * @private
59
- * @type {Regexps}
60
- */
61
- const REGEXPS = {
62
- UNIXMODE: /^-[a-z]{1}|-[a-z]{1}=.*$|--[a-zA-Z]+|--[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
63
- SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
64
- };
65
-
66
- /**
67
- * Error messages used in case of invalid command-line arguments.
68
- *
69
- * @private
70
- * @type {Errors}
71
- */
72
- const THROWS_ERRORS_MESSAGES = {
73
- INVALID_UNIXMODE_TYPE: 'The "unixmode" value must be a boolean type',
74
- NO_ARGUMENTS: 'No arguments added',
75
- NO_MATCH_SIMPLE: `Some argument(s) do not follow the 'key=value' format.`,
76
- NO_MATCH_UNIXMODE: `Some argument(s) do not follow the Unix-style command-line format.`,
77
- };
78
-
79
- // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
80
- /**
81
- * The `argv2Object()` function, converts command-line arguments to a key-value
82
- * object.
83
- *
84
- * This function takes an array of command-line arguments and converts them to a
85
- * JavaScript object with keys and values based on the provided argument format.
86
- * The format can be either simple key-value pairs (e.g. "key=value") or
87
- * Unix-style command-line options (e.g. "-o --option=value").
88
- *
89
- * @function
90
- * @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
91
- * @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
92
- * @throws {Error} If no arguments are provided from command line.
93
- * @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
94
- * @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
95
- * @example
96
- * ```js
97
- * // Command line input: node script.js --task=some-task --execute=true
98
- *
99
- * const args = argv2Object();
100
- * console.log(args.task); // prints 'some-task'
101
- * console.log(args.execute); // prints 'true'
102
- * ```
103
- */
104
- const argv2Object = (unixmode = false) => {
105
- if (typeof unixmode !== 'boolean') {
106
- throw new TypeError(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
107
- }
108
-
109
- const argumentsv = process.argv.slice(2);
110
- if (argumentsv.length === 0) {
111
- throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
112
- }
113
-
114
- const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
115
-
116
- if (!argumentsv.every(argumentv => regexp.test(argumentv))) {
117
- const message = unixmode
118
- ? THROWS_ERRORS_MESSAGES.NO_MATCH_UNIXMODE
119
- : THROWS_ERRORS_MESSAGES.NO_MATCH_SIMPLE;
120
- throw new Error(message);
121
- }
122
-
123
- const entries = [...argumentsv].map(argumentv => {
124
- const [k, v] = argumentv.split('=');
125
- const key = k.replace(/^-{1,2}/, '').replace(/-/g, '_');
126
- const value = v === undefined ? true : v;
127
- return [key, value];
128
- });
129
-
130
- return Object.fromEntries(entries);
131
- };
132
-
133
- // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
134
- export default argv2Object;
1
+ /**
2
+ * @file Converts command-line arguments to a key-value object.
3
+ *
4
+ * A utility function for converting command-line arguments to a key-value
5
+ * object.
6
+ *
7
+ * This file defines a single function, `argv2Object`, which parses
8
+ * command-line arguments and returns an object with keys and values
9
+ * corresponding to the provided arguments. The function can handle two types of
10
+ * arguments: simple key-value pairs (e.g. "key=value") and Unix-style
11
+ * command-line arguments (e.g. "-k value" or "--key=value").
12
+ *
13
+ * @version 1.1.0
14
+ *
15
+ * @example
16
+ * Simple key-value pairs
17
+ * Command line input: node script.js name=John age=30 level=admin
18
+ * Output: { name: 'John', age: '30', level: "admin" }
19
+ *
20
+ * @example
21
+ * Unix-style command-line options
22
+ * Command line input: node script.js -h --name=John --is-admin
23
+ * Output: { h: true, name: 'John', is_admin: true }
24
+ */
25
+ // ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
26
+ // » IMPORT LOCAL MODULES
27
+ import { REGEXPS, THROWS_ERRORS_MESSAGES } from '#constants';
28
+ import { formatKey, convertValue } from '#functions';
29
+
30
+ // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
31
+ /**
32
+ * An `Object` with keys and values corresponding to the provided arguments.
33
+ *
34
+ * @typedef {object} ArgvObject
35
+ * @property {string|boolean} [key=value] - The key-value pairs in the provided arguments.
36
+ */
37
+
38
+ // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
39
+ /**
40
+ * The `argv2Object()` function, converts command-line arguments to a key-value
41
+ * object.
42
+ *
43
+ * This function takes an array of command-line arguments and converts them to a
44
+ * JavaScript object with keys and values based on the provided argument format.
45
+ * The format can be either simple key-value pairs (e.g. "key=value") or
46
+ * Unix-style command-line options (e.g. "-o --option=value").
47
+ *
48
+ * @function
49
+ * @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
50
+ * @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
51
+ * @throws {TypeError} If `unixmode` is not is not of type `boolean`.
52
+ * @throws {Error} If no arguments are provided from command line.
53
+ * @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
54
+ * @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
55
+ * @example
56
+ * ```js
57
+ * // Command line input: node script.js --task=some-task --execute=true
58
+ *
59
+ * const args = argv2Object();
60
+ * console.log(args.task); // prints 'some-task'
61
+ * console.log(args.execute); // prints 'true'
62
+ * ```
63
+ */
64
+ const argv2Object = (unixmode = false) => {
65
+ if (typeof unixmode !== 'boolean') {
66
+ throw new TypeError(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
67
+ }
68
+
69
+ const argumentsv = process.argv.slice(2);
70
+ if (argumentsv.length === 0) {
71
+ throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
72
+ }
73
+
74
+ const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
75
+
76
+ if (!argumentsv.every(argumentv => regexp.test(argumentv))) {
77
+ const message = unixmode
78
+ ? THROWS_ERRORS_MESSAGES.NO_MATCH_UNIXMODE
79
+ : THROWS_ERRORS_MESSAGES.NO_MATCH_SIMPLE;
80
+ throw new Error(message);
81
+ }
82
+
83
+ const entries = [...argumentsv].map(argumentv => {
84
+ const [k, v] = argumentv.split('=');
85
+ const key = formatKey(k, 'snakecase');
86
+ const value = convertValue(v);
87
+ return [key, value];
88
+ });
89
+
90
+ return Object.fromEntries(entries);
91
+ };
92
+
93
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
94
+ export default argv2Object;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @file Module containing constants for argument validation.
3
+ *
4
+ * This module exports regular expressions for argument pattern matching
5
+ * and standardized error messages used throughout the argv2object library.
6
+ * These constants ensure consistent validation and error reporting when
7
+ * parsing command-line arguments.
8
+ *
9
+ * @module constants
10
+ * @since 1.0.0
11
+ */
12
+
13
+ // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14
+ /**
15
+ * Regular expression patterns for argument validation.
16
+ *
17
+ * @typedef {object} Regexps
18
+ * @property {RegExp} UNIXMODE - Pattern for Unix-style arguments (-a, --arg=value).
19
+ * @property {RegExp} SIMPLE - Pattern for simple key=value arguments.
20
+ */
21
+
22
+ /**
23
+ * Standardized error messages for argument validation failures.
24
+ *
25
+ * @typedef {object} ErrorMessages
26
+ * @property {string} INVALID_UNIXMODE_TYPE - When unixmode parameter is not boolean.
27
+ * @property {string} NO_ARGUMENTS - When no arguments are provided.
28
+ * @property {string} NO_MATCH_SIMPLE - When simple args don't match key=value format
29
+ * @property {string} NO_MATCH_UNIXMODE - When args don't match Unix-style format
30
+ */
31
+
32
+ // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
33
+ /**
34
+ * Regular expressions for validating different argument formats.
35
+ *
36
+ * @exports
37
+ * @private
38
+ * @constant REGEXPS
39
+ * @type {Regexps}
40
+ * @example
41
+ * // Unix-style pattern matches:
42
+ * // -a, -b=value, --flag, --option=value
43
+ * REGEXPS.UNIXMODE.test('--output=json') // true
44
+ *
45
+ * @example
46
+ * // Simple pattern matches:
47
+ * // key=value, multi-part-key=value
48
+ * REGEXPS.SIMPLE.test('output_format=json') // true
49
+ */
50
+ const REGEXPS = {
51
+ UNIXMODE: /^-[a-z]{1}|-[a-z]{1}=.*$|--[a-zA-Z]+|--[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
52
+ SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
53
+ };
54
+
55
+ /**
56
+ * Standardized error messages for consistent validation feedback.
57
+ *
58
+ * @exports
59
+ * @private
60
+ * @constant THROWS_ERRORS_MESSAGES
61
+ * @type {ErrorMessages}
62
+ * @example
63
+ * // Error cases:
64
+ * throw new Error(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
65
+ * throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
66
+ */
67
+ const THROWS_ERRORS_MESSAGES = {
68
+ INVALID_UNIXMODE_TYPE: 'The "unixmode" parameter must be a boolean value',
69
+ NO_ARGUMENTS: 'No command-line arguments were provided',
70
+ NO_MATCH_SIMPLE: 'Arguments must follow "key=value" format',
71
+ NO_MATCH_UNIXMODE: 'Arguments must follow Unix-style format (-a, --option=value)',
72
+ };
73
+
74
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
75
+ /**
76
+ * Exported constants for argument validation.
77
+ *
78
+ * @type {object}
79
+ * @property {Regexps} REGEXPS - Validation patterns.
80
+ * @property {ErrorMessages} THROWS_ERRORS_MESSAGES - Standard error messages.
81
+ */
82
+ export { REGEXPS, THROWS_ERRORS_MESSAGES };
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @file Module containing utility functions for argument processing.
3
+ *
4
+ * Provides formatting and type conversion functions used internally
5
+ * by the argv2object library to normalize and transform command-line
6
+ * arguments into consistent JavaScript data structures.
7
+ *
8
+ * @module Functions
9
+ * @since 1.0.0
10
+ */
11
+
12
+ // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
13
+ /**
14
+ * Normalizes and formats argument keys according to specified casing convention.
15
+ *
16
+ * Removes leading dashes from command-line flags and applies optional case conversion.
17
+ *
18
+ * Supports:
19
+ *
20
+ * - camelCase conversion (--some-flag → someFlag)
21
+ * - snake_case conversion (--some-flag → some_flag)
22
+ * - No conversion (--some-flag → some-flag)
23
+ *
24
+ * @exports
25
+ * @private
26
+ * @function formatKey
27
+ * @param {string} key - The raw key from command-line arguments
28
+ * @param {'camelcase'|'snakecase'} [mode] - Target formatting mode
29
+ * @returns {string} Normalized and formatted key
30
+ * @throws {TypeError} If key is not a string
31
+ * @example
32
+ * formatKey('--output-format', 'camelcase') // returns 'outputFormat'
33
+ * formatKey('-v', 'snakecase') // returns 'v' (no underscores needed)
34
+ * formatKey('--enable-logging') // returns 'enable-logging'
35
+ */
36
+ const formatKey = (key, mode) => {
37
+ if (typeof key !== 'string') {
38
+ throw new TypeError('Key must be a string');
39
+ }
40
+ const sanitizedKey = key.replace(/^-{1,2}/, '');
41
+
42
+ if (mode === 'camelcase') {
43
+ return sanitizedKey.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
44
+ }
45
+
46
+ if (mode === 'snakecase') {
47
+ return sanitizedKey.replace(/-/g, '_');
48
+ }
49
+
50
+ return sanitizedKey;
51
+ };
52
+
53
+ /**
54
+ * Converts string values from command-line arguments to proper JavaScript types.
55
+ *
56
+ * Performs automatic conversion of:
57
+ *
58
+ * - 'true'/'false' strings → boolean
59
+ * - Numeric strings → number
60
+ * - undefined values → true (for flag arguments)
61
+ * - All other values remain as strings
62
+ *
63
+ * @exports
64
+ * @private
65
+ * @function convertValue
66
+ * @param {string} [value] - The value to convert
67
+ * @returns {string|boolean|number} Converted value
68
+ * @example
69
+ * convertValue('true') // returns true
70
+ * convertValue('42') // returns 42
71
+ * convertValue() // returns true (for flag arguments)
72
+ * convertValue('text') // returns 'text'
73
+ */
74
+ const convertValue = value => {
75
+ if (value === undefined) return true;
76
+ if (value === 'true') return true;
77
+ if (value === 'false') return false;
78
+ if (!Number.isNaN(value) && !Number.isNaN(parseFloat(value))) return Number(value);
79
+ return value;
80
+ };
81
+
82
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
83
+ /**
84
+ * Utility functions for argument processing.
85
+ *
86
+ * @namespace
87
+ * @property {Function} formatKey - Key formatting/normalization function.
88
+ * @property {Function} convertValue - Value type conversion function.
89
+ */
90
+ export { formatKey, convertValue };