argv2object 1.0.0 → 1.0.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 Beltran Rodriguez Victor
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Beltran Rodriguez Victor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js CHANGED
@@ -17,7 +17,7 @@
17
17
  * @author Victor Giovanni Beltrán Rodríguez
18
18
  * @module argv2Object
19
19
  */
20
- const argv2Object = require('./src');
20
+ import argv2Object from '#argv2Object';
21
21
 
22
22
  // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
23
- module.exports = argv2Object;
23
+ export default argv2Object;
package/package.json CHANGED
@@ -1,67 +1,70 @@
1
- {
2
- "name": "argv2object",
3
- "version": "1.0.0",
4
- "private": false,
5
- "description": "A utility function for converting command-line arguments to a key-value object.",
6
- "keywords": [
7
- "command-line",
8
- "arguments",
9
- "object",
10
- "conversion",
11
- "key-value",
12
- "unix-style",
13
- "parse"
14
- ],
15
- "directories": {
16
- "doc": "docs",
17
- "test": "test"
18
- },
19
- "homepage": "https://github.com/vgbr-dev/argv2object#readme",
20
- "bugs": {
21
- "url": "https://github.com/vgbr-dev/argv2object/issues"
22
- },
23
- "repository": {
24
- "type": "git",
25
- "url": "git+https://github.com/vgbr-dev/argv2object.git"
26
- },
27
- "author": {
28
- "name": "Victor Giovanni Beltrán Rodríguez",
29
- "email": "vgbr.dev@gmail.com",
30
- "url": "https://vgbr-dev.blogspot.com"
31
- },
32
- "contributors": [
33
- {
34
- "name": "Victor Giovanni Beltrán Rodriguez",
35
- "email": "vgbr.dev@gmail.com",
36
- "url": "https://vgbr-dev.blogspot.com"
37
- }
38
- ],
39
- "license": "MIT",
40
- "main": "index.js",
41
- "scripts": {
42
- "test": "cross-env NODE_ENV=test node --test test/",
43
- "develop": "node --watch develop.js",
44
- "lint": "npx eslint",
45
- "prepare": "husky install",
46
- "prepublish": "npm test"
47
- },
48
- "devDependencies": {
49
- "@commitlint/cli": "^17.6.1",
50
- "@commitlint/config-conventional": "^17.6.1",
51
- "cross-env": "^7.0.3",
52
- "eslint": "^7.32.0",
53
- "eslint-config-airbnb-base": "^15.0.0",
54
- "eslint-config-prettier": "^8.8.0",
55
- "eslint-plugin-import": "^2.27.5",
56
- "eslint-plugin-jsdoc": "^41.1.1",
57
- "eslint-plugin-prettier": "^4.2.1",
58
- "husky": "^8.0.3",
59
- "jsdoc": "^4.0.2",
60
- "jsdoc-to-markdown": "^8.0.0",
61
- "lint-staged": "^13.2.1",
62
- "prettier": "^2.8.7"
63
- },
64
- "engines": {
65
- "node": ">=12.0.0"
66
- }
67
- }
1
+ {
2
+ "name": "argv2object",
3
+ "version": "1.0.1",
4
+ "private": false,
5
+ "description": "A utility function for converting command-line arguments to a key-value object.",
6
+ "keywords": [
7
+ "arguments",
8
+ "command-line",
9
+ "conversion",
10
+ "key-value",
11
+ "nodejs",
12
+ "object",
13
+ "parse",
14
+ "process.argv",
15
+ "unix-style"
16
+ ],
17
+ "homepage": "https://github.com/vgbr-dev/argv2object#readme",
18
+ "bugs": {
19
+ "url": "https://github.com/vgbr-dev/argv2object/issues"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/vgbr-dev/argv2object.git"
24
+ },
25
+ "author": {
26
+ "name": "Victor Giovanni Beltrán Rodríguez",
27
+ "email": "vgbr.dev@gmail.com",
28
+ "url": "https://vgbr-dev.blogspot.com"
29
+ },
30
+ "contributors": [
31
+ {
32
+ "name": "Victor Giovanni Beltrán Rodriguez",
33
+ "email": "vgbr.dev@gmail.com",
34
+ "url": "https://vgbr-dev.blogspot.com"
35
+ }
36
+ ],
37
+ "license": "MIT",
38
+ "type": "module",
39
+ "main": "index.js",
40
+ "imports": {
41
+ "#argv2Object": "./src/argv2Object.mjs",
42
+ "#constants": "./src/constants.mjs",
43
+ "#functions": "./src/functions.mjs"
44
+ },
45
+ "scripts": {
46
+ "test": "cross-env NODE_ENV=test node --watch --test test/*.test.mjs",
47
+ "lint": "npx eslint",
48
+ "prepare": "husky install",
49
+ "prepublish": "npm test"
50
+ },
51
+ "devDependencies": {
52
+ "@commitlint/cli": "^17.6.1",
53
+ "@commitlint/config-conventional": "^17.6.1",
54
+ "cross-env": "^7.0.3",
55
+ "eslint": "^8.42.0",
56
+ "eslint-config-airbnb-base": "^15.0.0",
57
+ "eslint-config-prettier": "^8.8.0",
58
+ "eslint-plugin-import": "^2.27.5",
59
+ "eslint-plugin-jsdoc": "^46.2.6",
60
+ "eslint-plugin-prettier": "^4.2.1",
61
+ "husky": "^8.0.3",
62
+ "jsdoc": "^4.0.2",
63
+ "jsdoc-to-markdown": "^8.0.0",
64
+ "lint-staged": "^13.2.1",
65
+ "prettier": "^2.8.7"
66
+ },
67
+ "engines": {
68
+ "node": ">=12.0.0"
69
+ }
70
+ }
@@ -1,130 +1,134 @@
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 ERRORS = {
73
- NO_ARGS: 'No arguments added',
74
- NO_MATCH_SIMPLE: `Some argument(s) do not follow the 'key=value' format.`,
75
- NO_MATCH_UNIXMODE: `Some argument(s) do not follow the Unix-style command-line format.`,
76
- };
77
-
78
- // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
79
- /**
80
- * The `argv2Object()` function, converts command-line arguments to a key-value
81
- * object.
82
- *
83
- * This function takes an array of command-line arguments and converts them to a
84
- * JavaScript object with keys and values based on the provided argument format.
85
- * The format can be either simple key-value pairs (e.g. "key=value") or
86
- * Unix-style command-line options (e.g. "-o --option=value").
87
- *
88
- * @function
89
- * @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
90
- * @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
91
- * @throws {Error} If no arguments are provided from command line.
92
- * @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
93
- * @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
94
- * @example
95
- * ```js
96
- * // Command line input: node script.js --task=some-task --execute=true
97
- *
98
- * const args = argv2Object();
99
- * console.log(args.task); // prints 'some-task'
100
- * console.log(args.execute); // prints 'true'
101
- * ```
102
- */
103
- const argv2Object = (unixmode = false) => {
104
- const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
105
-
106
- const args = process.argv.slice(2);
107
- if (args.length === 0) {
108
- throw new Error(ERRORS.NO_ARGS);
109
- }
110
-
111
- if (!args.every(arg => regexp.test(arg))) {
112
- throw new Error(unixmode ? ERRORS.NO_MATCH_UNIXMODE : ERRORS.NO_MATCH_SIMPLE);
113
- }
114
-
115
- return [...args]
116
- .map(arg => {
117
- const [k, v] = arg.split('=');
118
- const key = k.replace(/^-{1,2}/, '').replace(/-/g, '_');
119
- const value = v === undefined ? true : v;
120
- return [key, value];
121
- })
122
- .reduce((acc, arg) => {
123
- const [key, value] = arg;
124
- acc[key] = value;
125
- return acc;
126
- }, {});
127
- };
128
-
129
- // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
130
- module.exports = 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
+ * @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;