@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.
@@ -17,7 +17,6 @@ export declare class ArgParser {
17
17
  readonly desc: string;
18
18
  readonly argList: (ArgParser | Arg)[];
19
19
  readonly examples: string[];
20
- static readonly helpArg: Arg;
21
20
  commands: ArgParser[];
22
21
  args: Arg[];
23
22
  flags: Arg[];
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Determine if either black or white provides more contrast to the provided color
3
+ * @param {string} background Color to compare against
4
+ * @return {"white" | "black"} Color with the most contrast
5
+ */
6
+ export declare function blackOrWhite(background: string): 'white' | 'black';
package/dist/index.cjs CHANGED
@@ -5,7 +5,7 @@
5
5
  var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2;
6
6
  var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value2);
7
7
 
8
- const _ArgParser = class _ArgParser {
8
+ class ArgParser {
9
9
  /**
10
10
  * Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
11
11
  *
@@ -23,12 +23,15 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
23
23
  this.desc = desc;
24
24
  this.argList = argList;
25
25
  this.examples = examples;
26
- this.commands = argList.filter((arg) => arg instanceof _ArgParser);
26
+ this.commands = argList.filter((arg) => arg instanceof ArgParser);
27
27
  this.args = argList.filter((arg) => {
28
28
  var _a;
29
- return !(arg instanceof _ArgParser) && !((_a = arg.flags) == null ? void 0 : _a.length);
29
+ return !(arg instanceof ArgParser) && !((_a = arg.flags) == null ? void 0 : _a.length);
30
30
  });
31
- this.flags = [...argList.filter((arg) => !(arg instanceof _ArgParser) && arg.flags && arg.flags.length), ...this.flags];
31
+ this.flags = [
32
+ ...argList.filter((arg) => !(arg instanceof ArgParser) && arg.flags && arg.flags.length),
33
+ { name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false }
34
+ ];
32
35
  this.defaults = argList.reduce((acc, arg) => ({ ...acc, [arg.name]: arg["extras"] ? [] : arg.default ?? null }), {});
33
36
  this.examples = [
34
37
  ...examples,
@@ -105,21 +108,19 @@ var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "s
105
108
  let msg = `
106
109
 
107
110
  ${opts.message || this.desc}`;
108
- msg += "\n\nUsage: " + this.examples.map((ex) => `run ${this.name} ${ex}`).join("\n ");
109
- if (this.args.length) msg += "\n\n " + this.args.map((arg) => `${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join("\n ");
110
- msg += "\n\nOptions:\n " + this.flags.map((flag) => {
111
+ msg += "\n\nUsage: " + this.examples.map((ex) => `${this.name} ${ex}`).join("\n ");
112
+ if (this.args.length) msg += "\n\n " + this.args.map((arg) => `${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join("\n ");
113
+ msg += "\n\nOptions:\n " + this.flags.map((flag) => {
111
114
  var _a;
112
115
  const flags = ((_a = flag.flags) == null ? void 0 : _a.join(", ")) || "";
113
116
  return `${flags}${spacer(flags)}${flag.desc}`;
114
- }).join("\n ");
115
- if (this.commands.length) msg += "\n\nCommands:\n " + this.commands.map((command) => `${command.name}${spacer(command.name)}${command.desc}`).join("\n ");
117
+ }).join("\n ");
118
+ if (this.commands.length) msg += "\n\nCommands:\n " + this.commands.map((command) => `${command.name}${spacer(command.name)}${command.desc}`).join("\n ");
116
119
  return `${msg}
117
120
 
118
121
  `;
119
122
  }
120
- };
121
- __publicField(_ArgParser, "helpArg", { name: "help", desc: "Display command's help message", flags: ["-h", "--help"], default: false });
122
- let ArgParser = _ArgParser;
123
+ }
123
124
  function clean(obj, undefinedOnly = false) {
124
125
  if (obj == null) throw new Error("Cannot clean a NULL value");
125
126
  if (Array.isArray(obj)) {
@@ -531,6 +532,13 @@ ${opts.message || this.desc}`;
531
532
  return this;
532
533
  }
533
534
  }
535
+ function blackOrWhite(background) {
536
+ const exploded = background.match(background.length >= 6 ? /\w\w/g : /\w/g);
537
+ if (!exploded) return "black";
538
+ const [r2, g, b2] = exploded.map((hex) => parseInt(hex, 16));
539
+ const luminance = (0.299 * r2 + 0.587 * g + 0.114 * b2) / 255;
540
+ return luminance > 0.5 ? "black" : "white";
541
+ }
534
542
  const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
535
543
  const NUMBER_LIST = "0123456789";
536
544
  const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
@@ -1694,6 +1702,7 @@ ${opts.message || this.desc}`;
1694
1702
  exports.addUnique = addUnique;
1695
1703
  exports.adjustedInterval = adjustedInterval;
1696
1704
  exports.arrayDiff = arrayDiff;
1705
+ exports.blackOrWhite = blackOrWhite;
1697
1706
  exports.caseInsensitiveSort = caseInsensitiveSort;
1698
1707
  exports.clean = clean;
1699
1708
  exports.dec2Frac = dec2Frac;