argv2object 1.1.1 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "argv2object",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "private": false,
5
5
  "description": "A utility function for converting command-line arguments to a key-value object.",
6
6
  "keywords": [
@@ -49,22 +49,27 @@
49
49
  "scripts": {
50
50
  "test": "cross-env NODE_ENV=test node --test test/*.test.mjs",
51
51
  "lint": "npx eslint . --ext .mjs --ext .js --ext .cjs",
52
- "prepare": "husky install",
52
+ "prepare": "husky",
53
53
  "prepublish": "npm test"
54
54
  },
55
55
  "devDependencies": {
56
- "@commitlint/cli": "^19.8.0",
57
- "@commitlint/config-conventional": "^19.8.0",
58
- "cross-env": "^7.0.3",
59
- "eslint": "^8.57.1",
56
+ "@commitlint/cli": "^20.1.0",
57
+ "@commitlint/config-conventional": "^20.0.0",
58
+ "@eslint/compat": "^1.4.1",
59
+ "@eslint/core": "^0.17.0",
60
+ "@eslint/eslintrc": "^3.3.3",
61
+ "cross-env": "^10.1.0",
62
+ "eslint": "^9.39.1",
60
63
  "eslint-config-airbnb-base": "^15.0.0",
61
- "eslint-config-prettier": "^8.10.0",
64
+ "eslint-config-prettier": "^10.1.8",
65
+ "eslint-import-resolver-alias": "^1.1.2",
62
66
  "eslint-import-resolver-node": "^0.3.9",
63
- "eslint-plugin-import": "^2.27.5",
64
- "eslint-plugin-prettier": "^4.2.1",
65
- "husky": "^8.0.3",
66
- "lint-staged": "^15.5.2",
67
- "prettier": "^2.8.8"
67
+ "eslint-plugin-import": "^2.32.0",
68
+ "eslint-plugin-jsdoc": "^61.4.1",
69
+ "eslint-plugin-prettier": "^5.5.4",
70
+ "husky": "^9.1.7",
71
+ "lint-staged": "^16.2.7",
72
+ "prettier": "^3.7.3"
68
73
  },
69
74
  "engines": {
70
75
  "node": ">=14.0.0"
@@ -24,8 +24,8 @@
24
24
  */
25
25
  // ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
26
26
  // » IMPORT LOCAL MODULES
27
- import { REGEXPS, THROWS_ERRORS_MESSAGES } from '#constants';
28
- import { formatKey, convertValue } from '#functions';
27
+ import { COMMAND_LINE_PATTERNS, ERROR_MESSAGES } from '#constants';
28
+ import { createObjectFromArguments } from '#functions';
29
29
 
30
30
  // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
31
31
  /**
@@ -46,7 +46,7 @@ import { formatKey, convertValue } from '#functions';
46
46
  * Unix-style command-line options (e.g. "-o --option=value").
47
47
  *
48
48
  * @function
49
- * @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
49
+ * @param {boolean} [unixmode] - Whether to parse Unix-style command-line options, the default value is `false`.
50
50
  * @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
51
51
  * @throws {TypeError} If `unixmode` is not is not of type `boolean`.
52
52
  * @throws {Error} If no arguments are provided from command line.
@@ -56,38 +56,33 @@ import { formatKey, convertValue } from '#functions';
56
56
  * ```js
57
57
  * // Command line input: node script.js --task=some-task --execute=true
58
58
  *
59
- * const args = argv2Object();
60
- * console.log(args.task); // prints 'some-task'
61
- * console.log(args.execute); // prints 'true'
59
+ * const argumentsv = argv2Object();
60
+ * console.log(argumentsv.task); // prints 'some-task'
61
+ * console.log(argumentsv.execute); // prints true
62
62
  * ```
63
63
  */
64
64
  const argv2Object = (unixmode = false) => {
65
65
  if (typeof unixmode !== 'boolean') {
66
- throw new TypeError(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
66
+ throw new TypeError(ERROR_MESSAGES.INVALID_UNIXMODE_TYPE);
67
67
  }
68
68
 
69
69
  const argumentsv = process.argv.slice(2);
70
70
  if (argumentsv.length === 0) {
71
- throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
71
+ throw new Error(ERROR_MESSAGES.NO_ARGUMENTS);
72
72
  }
73
73
 
74
- const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
74
+ const patterns = unixmode
75
+ ? [COMMAND_LINE_PATTERNS.UNIX_LONG, COMMAND_LINE_PATTERNS.UNIX_SHORT]
76
+ : [COMMAND_LINE_PATTERNS.SIMPLE];
75
77
 
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;
78
+ const isValid = argumentsv.every(argument => patterns.some(pattern => pattern.test(argument)));
79
+
80
+ if (!isValid) {
81
+ const message = unixmode ? ERROR_MESSAGES.NO_MATCH_UNIXMODE : ERROR_MESSAGES.NO_MATCH_SIMPLE;
80
82
  throw new Error(message);
81
83
  }
82
84
 
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);
85
+ return createObjectFromArguments(argumentsv, 'snakecase');
91
86
  };
92
87
 
93
88
  // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
package/src/constants.mjs CHANGED
@@ -12,11 +12,12 @@
12
12
 
13
13
  // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14
14
  /**
15
- * Regular expression patterns for argument validation.
15
+ * Type definition for command line argument patterns.
16
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.
17
+ * @typedef {object} CommandLinePatterns
18
+ * @property {RegExp} UNIX_SHORT - Matches short Unix flags: `-a`, `-v`, `-f=value`.
19
+ * @property {RegExp} UNIX_LONG - Matches long Unix flags: `--help`, `--output=json`.
20
+ * @property {RegExp} SIMPLE - Pattern for simple key=value arguments: key=value, multi-part-key=value.
20
21
  */
21
22
 
22
23
  /**
@@ -25,30 +26,60 @@
25
26
  * @typedef {object} ErrorMessages
26
27
  * @property {string} INVALID_UNIXMODE_TYPE - When unixmode parameter is not boolean.
27
28
  * @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
29
+ * @property {string} NO_MATCH_SIMPLE - When simple args don't match key=value format.
30
+ * @property {string} NO_MATCH_UNIXMODE - When args don't match Unix-style format.
30
31
  */
31
32
 
32
33
  // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
33
34
  /**
34
- * Regular expressions for validating different argument formats.
35
+ * Regular expression patterns for validating command line argument formats.
36
+ *
37
+ * Provides comprehensive pattern matching for different command line argument
38
+ * styles commonly used in Node.js applications and CLI tools.
35
39
  *
36
40
  * @exports
37
41
  * @private
38
- * @constant REGEXPS
39
- * @type {Regexps}
42
+ * @constant COMMAND_LINE_PATTERNS
43
+ * @type {CommandLinePatterns}
44
+ *
40
45
  * @example
41
- * // Unix-style pattern matches:
42
- * // -a, -b=value, --flag, --option=value
43
- * REGEXPS.UNIXMODE.test('--output=json') // true
46
+ * ```js
47
+ * // Valid short flags:
48
+ * COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-h') // true
49
+ * COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-v') // true
50
+ * COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-f=json') // true
51
+ *
52
+ * // Invalid short flags:
53
+ * COMMAND_LINE_PATTERNS.UNIX_SHORT.test('--help') // false
54
+ * COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-abc') // false
55
+ * ```
44
56
  *
45
57
  * @example
46
- * // Simple pattern matches:
47
- * // key=value, multi-part-key=value
48
- * REGEXPS.SIMPLE.test('output_format=json') // true
58
+ * ```js
59
+ * // Valid long flags:
60
+ * COMMAND_LINE_PATTERNS.UNIX_LONG.test('--help') // true
61
+ * COMMAND_LINE_PATTERNS.UNIX_LONG.test('--output=json') // true
62
+ * COMMAND_LINE_PATTERNS.UNIX_LONG.test('--dry-run') // true
63
+ *
64
+ * // Invalid long flags:
65
+ * COMMAND_LINE_PATTERNS.UNIX_LONG.test('-h') // false
66
+ * COMMAND_LINE_PATTERNS.UNIX_LONG.test('--123') // false
67
+ * ```
68
+ *
69
+ * @example
70
+ * ```js
71
+ * // Valid simple arguments:
72
+ * COMMAND_LINE_PATTERNS.SIMPLE.test('output=json') // true
73
+ * COMMAND_LINE_PATTERNS.SIMPLE.test('config-file=settings') // true
74
+ *
75
+ * // Invalid simple arguments:
76
+ * COMMAND_LINE_PATTERNS.SIMPLE.test('--output=json') // false
77
+ * COMMAND_LINE_PATTERNS.SIMPLE.test('output') // false
78
+ * ```
49
79
  */
50
- const REGEXPS = {
51
- UNIXMODE: /^-[a-z]{1}|-[a-z]{1}=.*$|--[a-zA-Z]+|--[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
80
+ const COMMAND_LINE_PATTERNS = {
81
+ UNIX_SHORT: /^-[a-zA-Z](=.*)?$/,
82
+ UNIX_LONG: /^--[a-zA-Z][a-zA-Z0-9-]*(=.*)?$/,
52
83
  SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
53
84
  };
54
85
 
@@ -60,11 +91,13 @@ const REGEXPS = {
60
91
  * @constant THROWS_ERRORS_MESSAGES
61
92
  * @type {ErrorMessages}
62
93
  * @example
94
+ * ```js
63
95
  * // Error cases:
64
- * throw new Error(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
65
- * throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
96
+ * throw new Error(ERROR_MESSAGES.INVALID_UNIXMODE_TYPE);
97
+ * throw new Error(ERROR_MESSAGES.NO_ARGUMENTS);
98
+ * ```
66
99
  */
67
- const THROWS_ERRORS_MESSAGES = {
100
+ const ERROR_MESSAGES = {
68
101
  INVALID_UNIXMODE_TYPE: 'The "unixmode" parameter must be a boolean value',
69
102
  NO_ARGUMENTS: 'No command-line arguments were provided',
70
103
  NO_MATCH_SIMPLE: 'Arguments must follow "key=value" format',
@@ -76,7 +109,7 @@ const THROWS_ERRORS_MESSAGES = {
76
109
  * Exported constants for argument validation.
77
110
  *
78
111
  * @type {object}
79
- * @property {Regexps} REGEXPS - Validation patterns.
80
- * @property {ErrorMessages} THROWS_ERRORS_MESSAGES - Standard error messages.
112
+ * @property {CommandLinePatterns} REGEXPS - Validation patterns.
113
+ * @property {ErrorMessages} THROWS_ERRORS_MESSAGES - Standard error messages.
81
114
  */
82
- export { REGEXPS, THROWS_ERRORS_MESSAGES };
115
+ export { COMMAND_LINE_PATTERNS, ERROR_MESSAGES };
package/src/functions.mjs CHANGED
@@ -17,21 +17,23 @@
17
17
  *
18
18
  * Supports:
19
19
  *
20
- * - camelCase conversion (--some-flag → someFlag)
21
- * - snake_case conversion (--some-flag → some_flag)
22
- * - No conversion (--some-flag → some-flag)
20
+ * - camelCase conversion (--some-flag → someFlag).
21
+ * - snake_case conversion (--some-flag → some_flag).
22
+ * - No conversion (--some-flag → some-flag).
23
23
  *
24
24
  * @exports
25
25
  * @private
26
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
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
31
  * @example
32
+ * ```js
32
33
  * formatKey('--output-format', 'camelcase') // returns 'outputFormat'
33
34
  * formatKey('-v', 'snakecase') // returns 'v' (no underscores needed)
34
35
  * formatKey('--enable-logging') // returns 'enable-logging'
36
+ * ```
35
37
  */
36
38
  const formatKey = (key, mode) => {
37
39
  if (typeof key !== 'string') {
@@ -55,21 +57,23 @@ const formatKey = (key, mode) => {
55
57
  *
56
58
  * Performs automatic conversion of:
57
59
  *
58
- * - 'true'/'false' strings → boolean
59
- * - Numeric strings → number
60
- * - undefined values → true (for flag arguments)
61
- * - All other values remain as strings
60
+ * - 'true'/'false' strings → boolean.
61
+ * - Numeric strings → number.
62
+ * - undefined values → true (for flag arguments).
63
+ * - All other values remain as strings.
62
64
  *
63
65
  * @exports
64
66
  * @private
65
67
  * @function convertValue
66
- * @param {string} [value] - The value to convert
67
- * @returns {string|boolean|number} Converted value
68
+ * @param {string} [value] - The value to convert.
69
+ * @returns {string|boolean|number} Converted value.
68
70
  * @example
71
+ * ```js
69
72
  * convertValue('true') // returns true
70
73
  * convertValue('42') // returns 42
71
74
  * convertValue() // returns true (for flag arguments)
72
75
  * convertValue('text') // returns 'text'
76
+ * ```
73
77
  */
74
78
  const convertValue = value => {
75
79
  if (value === undefined) return true;
@@ -79,6 +83,36 @@ const convertValue = value => {
79
83
  return value;
80
84
  };
81
85
 
86
+ /**
87
+ * Creates an object from command line arguments by parsing key-value pairs.
88
+ *
89
+ * @param {string[]} argumentsv - Array of command line arguments to process.
90
+ * @param {'camelcase'|'snakecase'} mode - Target formatting mode.
91
+ * @returns {object} Object with parsed keys and converted values.
92
+ * @throws {Error} When arguments cannot be parsed into valid key-value pairs.
93
+ *
94
+ * @example
95
+ * ```js
96
+ * // Simple mode: key=value pairs
97
+ * createObjectFromArguments(['name=John', 'age=30'], 'snakecase');
98
+ * // Returns: { name: 'John', age: 30 }
99
+ *
100
+ * // Unix mode: flags and options
101
+ * createObjectFromArguments(['--name=John', '-a'], 'snakecase');
102
+ * // Returns: { name: 'John', a: true }
103
+ * ```
104
+ */
105
+ const createObjectFromArguments = (argumentsv, mode) => {
106
+ const entries = [...argumentsv].map(argumentv => {
107
+ const [k, v] = argumentv.split('=');
108
+ const key = formatKey(k, mode);
109
+ const value = convertValue(v);
110
+ return [key, value];
111
+ });
112
+
113
+ return Object.fromEntries(entries);
114
+ };
115
+
82
116
  // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
83
117
  /**
84
118
  * Utility functions for argument processing.
@@ -87,4 +121,4 @@ const convertValue = value => {
87
121
  * @property {Function} formatKey - Key formatting/normalization function.
88
122
  * @property {Function} convertValue - Value type conversion function.
89
123
  */
90
- export { formatKey, convertValue };
124
+ export { formatKey, convertValue, createObjectFromArguments };