Skip to content

Commit 347e86d

Browse files
authored
JavaScript (v3): Update utils. (#6950)
- Update the getUniqueName util to throw an error if a prefix is not passed in. - Add a validateArgs util for checking parseArg options marked as required.
1 parent 790b580 commit 347e86d

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

javascriptv3/example_code/libs/tests/util-string.unit.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe("util-string", () => {
1717
expect(u1).not.toEqual(u2);
1818
});
1919

20-
it("should return undefined if a falsy value is passed in", () => {
21-
expect(getUniqueName()).toBeUndefined();
22-
expect(getUniqueName("")).toBeUndefined();
23-
expect(getUniqueName(0)).toBeUndefined();
20+
it("should throw an error if a falsy value is passed in for the prefix", () => {
21+
expect(() => getUniqueName()).toThrowError();
22+
expect(() => getUniqueName("")).toThrowError();
23+
expect(() => getUniqueName(0)).toThrowError();
2424
});
2525
});
2626

javascriptv3/example_code/libs/utils/util-node.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,32 @@ export const setEnv = (/** @type {string} */ key, value) => {
1212
* @param {string | URL} fileUrl
1313
*/
1414
export const isMain = (fileUrl) => process.argv[1] === fileURLToPath(fileUrl);
15+
16+
/**
17+
* @typedef {import("node:util").ParseArgsConfig} ParseArgsConfig
18+
* @typedef {ReturnType<import("node:util").parseArgs>} ParsedResults
19+
*
20+
* @param {import("node:util").ParseArgsConfig} config
21+
* @param {ParsedResults} results
22+
* @returns {{ errors: string[] | null }}
23+
*/
24+
export const validateArgs = (config, results) => {
25+
if (!config.options) {
26+
return {};
27+
}
28+
29+
/** @type {string[] | null} */
30+
let errors = null;
31+
32+
for (const option in config.options) {
33+
const optionRequired = config.options[option]?.required;
34+
const optionPresent = Object.hasOwn(results.values, option);
35+
36+
if (optionRequired && !optionPresent) {
37+
errors = errors ?? [];
38+
errors.push(`Missing required argument "${option}".`);
39+
}
40+
41+
return { errors };
42+
}
43+
};

javascriptv3/example_code/libs/utils/util-string.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import { v4 as uuidv4 } from "uuid";
44

55
/**
6-
* @param {string} name
6+
* @param {string} prefix
77
*/
8-
export const getUniqueName = (name) => {
9-
if (!name) {
10-
return;
8+
export const getUniqueName = (prefix) => {
9+
class GetUniqueNameError extends Error {
10+
name = GetUniqueNameError.name;
1111
}
1212

13-
return `${name.toLowerCase()}-${uuidv4()}`;
13+
if (!prefix) {
14+
throw new GetUniqueNameError("Prefix is missing.");
15+
}
16+
17+
return `${prefix.toLowerCase()}-${uuidv4()}`;
1418
};
1519

1620
/**

0 commit comments

Comments
 (0)