Skip to content

Commit 80e1f1d

Browse files
committed
refactor: add constants module
1 parent 564d4c8 commit 80e1f1d

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

src/constants.mjs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)