@ztimson/utils 0.23.4 → 0.23.6
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/dist/arg-parser.d.ts +0 -1
- package/dist/color.d.ts +6 -0
- package/dist/index.cjs +21 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +21 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2;
|
|
3
3
|
var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value2);
|
|
4
|
-
|
|
4
|
+
class ArgParser {
|
|
5
5
|
/**
|
|
6
6
|
* Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
|
|
7
7
|
*
|
|
@@ -19,12 +19,15 @@ const _ArgParser = class _ArgParser {
|
|
|
19
19
|
this.desc = desc;
|
|
20
20
|
this.argList = argList;
|
|
21
21
|
this.examples = examples;
|
|
22
|
-
this.commands = argList.filter((arg) => arg instanceof
|
|
22
|
+
this.commands = argList.filter((arg) => arg instanceof ArgParser);
|
|
23
23
|
this.args = argList.filter((arg) => {
|
|
24
24
|
var _a;
|
|
25
|
-
return !(arg instanceof
|
|
25
|
+
return !(arg instanceof ArgParser) && !((_a = arg.flags) == null ? void 0 : _a.length);
|
|
26
26
|
});
|
|
27
|
-
this.flags = [
|
|
27
|
+
this.flags = [
|
|
28
|
+
...argList.filter((arg) => !(arg instanceof ArgParser) && arg.flags && arg.flags.length),
|
|
29
|
+
{ name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false }
|
|
30
|
+
];
|
|
28
31
|
this.defaults = argList.reduce((acc, arg) => ({ ...acc, [arg.name]: arg["extras"] ? [] : arg.default ?? null }), {});
|
|
29
32
|
this.examples = [
|
|
30
33
|
...examples,
|
|
@@ -101,21 +104,19 @@ const _ArgParser = class _ArgParser {
|
|
|
101
104
|
let msg = `
|
|
102
105
|
|
|
103
106
|
${opts.message || this.desc}`;
|
|
104
|
-
msg += "\n\nUsage: " + this.examples.map((ex) =>
|
|
105
|
-
if (this.args.length) msg += "\n\n
|
|
106
|
-
msg += "\n\nOptions:\n
|
|
107
|
+
msg += "\n\nUsage: " + this.examples.map((ex) => `${this.name} ${ex}`).join("\n ");
|
|
108
|
+
if (this.args.length) msg += "\n\n " + this.args.map((arg) => `${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join("\n ");
|
|
109
|
+
msg += "\n\nOptions:\n " + this.flags.map((flag) => {
|
|
107
110
|
var _a;
|
|
108
111
|
const flags = ((_a = flag.flags) == null ? void 0 : _a.join(", ")) || "";
|
|
109
112
|
return `${flags}${spacer(flags)}${flag.desc}`;
|
|
110
|
-
}).join("\n
|
|
111
|
-
if (this.commands.length) msg += "\n\nCommands:\n
|
|
113
|
+
}).join("\n ");
|
|
114
|
+
if (this.commands.length) msg += "\n\nCommands:\n " + this.commands.map((command) => `${command.name}${spacer(command.name)}${command.desc}`).join("\n ");
|
|
112
115
|
return `${msg}
|
|
113
116
|
|
|
114
117
|
`;
|
|
115
118
|
}
|
|
116
|
-
}
|
|
117
|
-
__publicField(_ArgParser, "helpArg", { name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false });
|
|
118
|
-
let ArgParser = _ArgParser;
|
|
119
|
+
}
|
|
119
120
|
function clean(obj, undefinedOnly = false) {
|
|
120
121
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
121
122
|
if (Array.isArray(obj)) {
|
|
@@ -527,6 +528,13 @@ class Cache {
|
|
|
527
528
|
return this;
|
|
528
529
|
}
|
|
529
530
|
}
|
|
531
|
+
function blackOrWhite(background) {
|
|
532
|
+
const exploded = background.match(background.length >= 6 ? /\w\w/g : /\w/g);
|
|
533
|
+
if (!exploded) return "black";
|
|
534
|
+
const [r2, g, b2] = exploded.map((hex) => parseInt(hex, 16));
|
|
535
|
+
const luminance = (0.299 * r2 + 0.587 * g + 0.114 * b2) / 255;
|
|
536
|
+
return luminance > 0.5 ? "black" : "white";
|
|
537
|
+
}
|
|
530
538
|
const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
531
539
|
const NUMBER_LIST = "0123456789";
|
|
532
540
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
@@ -1691,6 +1699,7 @@ export {
|
|
|
1691
1699
|
addUnique,
|
|
1692
1700
|
adjustedInterval,
|
|
1693
1701
|
arrayDiff,
|
|
1702
|
+
blackOrWhite,
|
|
1694
1703
|
caseInsensitiveSort,
|
|
1695
1704
|
clean,
|
|
1696
1705
|
dec2Frac,
|