Skip to content

Commit aafbe81

Browse files
committed
refactor: add functions module
1 parent 80e1f1d commit aafbe81

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

src/functions.mjs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @file TODO ADD SHORT MODULE DESCRIPTION.
3+
*
4+
* @module Functions
5+
*/
6+
7+
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8+
/**
9+
* Normalizes and formats argument keys according to specified casing convention.
10+
*
11+
* Removes leading dashes from command-line flags and applies optional case conversion.
12+
*
13+
* Supports:
14+
*
15+
* - camelCase conversion (--some-flag → someFlag)
16+
* - snake_case conversion (--some-flag → some_flag)
17+
* - No conversion (--some-flag → some-flag)
18+
*
19+
* @exports
20+
* @private
21+
* @function formatKey
22+
* @param {string} key - The raw key from command-line arguments
23+
* @param {'camelcase'|'snakecase'} [mode] - Target formatting mode
24+
* @returns {string} Normalized and formatted key
25+
* @throws {TypeError} If key is not a string
26+
* @example
27+
* formatKey('--output-format', 'camelcase') // returns 'outputFormat'
28+
* formatKey('-v', 'snakecase') // returns 'v' (no underscores needed)
29+
* formatKey('--enable-logging') // returns 'enable-logging'
30+
*/
31+
const formatKey = (key, mode) => {
32+
if (typeof key !== 'string') {
33+
throw new TypeError('Key must be a string');
34+
}
35+
const sanitizedKey = key.replace(/^-{1,2}/, '');
36+
37+
if (mode === 'camelcase') {
38+
return sanitizedKey.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
39+
}
40+
41+
if (mode === 'snakecase') {
42+
return sanitizedKey.replace(/-/g, '_');
43+
}
44+
45+
return sanitizedKey;
46+
};
47+
48+
/**
49+
* Converts string values from command-line arguments to proper JavaScript types.
50+
*
51+
* Performs automatic conversion of:
52+
*
53+
* - 'true'/'false' strings → boolean
54+
* - Numeric strings → number
55+
* - undefined values → true (for flag arguments)
56+
* - All other values remain as strings
57+
*
58+
* @exports
59+
* @private
60+
* @function convertValue
61+
* @param {string} [value] - The value to convert
62+
* @returns {string|boolean|number} Converted value
63+
* @example
64+
* convertValue('true') // returns true
65+
* convertValue('42') // returns 42
66+
* convertValue() // returns true (for flag arguments)
67+
* convertValue('text') // returns 'text'
68+
*/
69+
const convertValue = value => {
70+
if (value === undefined) return true;
71+
if (value === 'true') return true;
72+
if (value === 'false') return false;
73+
if (!Number.isNaN(value) && !Number.isNaN(parseFloat(value))) return Number(value);
74+
return value;
75+
};
76+
77+
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
78+
/**
79+
* Utility functions for argument processing.
80+
*
81+
* @namespace
82+
* @property {Function} formatKey - Key formatting/normalization function.
83+
* @property {Function} convertValue - Value type conversion function.
84+
*/
85+
export { formatKey, convertValue };

0 commit comments

Comments
 (0)