Skip to content

Commit 4f460e5

Browse files
committed
Format Code, and Fix Spacing and Name in bench.js
The spacing in scripts/bench.js was off, leading to an unconsitent visual output when running the benchmark. Furthermore, the benchmark was still named ultraflags. Prettier was used to format the rest of the code
1 parent 57986b5 commit 4f460e5

File tree

6 files changed

+257
-204
lines changed

6 files changed

+257
-204
lines changed

scripts/bench.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
import { parseArgs } from 'node:util'
1+
import { parseArgs } from "node:util";
22
import benchmark from "benchmark";
3-
import minimist from 'minimist';
4-
import mri from 'mri';
5-
import yargs from 'yargs-parser';
6-
import { parse as ultraflag } from "../dist/index.js";
3+
import minimist from "minimist";
4+
import mri from "mri";
5+
import yargs from "yargs-parser";
6+
import { parse as arg } from "../dist/index.js";
77

88
const bench = new benchmark.Suite();
9-
const args = ['--a=1', '-b', '--bool', '--no-boop', '--multi=foo', '--multi=baz', '-xyz'];
9+
const args = [
10+
"--a=1",
11+
"-b",
12+
"--bool",
13+
"--no-boop",
14+
"--multi=foo",
15+
"--multi=baz",
16+
"-xyz",
17+
];
1018

1119
bench
12-
.add('ultraflag ', () => ultraflag(args))
13-
.add('mri ', () => mri(args))
14-
.add('minimist ', () => minimist(args))
15-
.add('node:util', () => parseArgs({ args, strict: false, allowNegative: true, options: { bool: { type: 'boolean', short: 'b' }, boop: { type: 'boolean' }, multi: { type: 'string' }} }))
16-
.add('yargs-parser ', () => yargs(args))
17-
.on('cycle', e => console.log(String(e.target)))
20+
.add("args ", () => arg(args))
21+
.add("minimist ", () => minimist(args))
22+
.add("mri ", () => mri(args))
23+
.add("node:util ", () =>
24+
parseArgs({
25+
args,
26+
strict: false,
27+
allowNegative: true,
28+
options: {
29+
bool: { type: "boolean", short: "b" },
30+
boop: { type: "boolean" },
31+
multi: { type: "string" },
32+
},
33+
}),
34+
)
35+
.add("yargs-parser", () => yargs(args))
36+
.on("cycle", (e) => console.log(String(e.target)))
1837
.run();

scripts/build.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import colors from 'chalk';
1+
import colors from "chalk";
22
import { build } from "esbuild";
3-
import { gzipSizeFromFileSync } from 'gzip-size';
4-
import bytes from 'pretty-bytes';
3+
import { gzipSizeFromFileSync } from "gzip-size";
4+
import bytes from "pretty-bytes";
55

66
async function run() {
7-
const files = ['src/index.ts'];
8-
const output = {};
9-
const promises = [];
10-
for (const file of files) {
11-
promises.push(
12-
build({
13-
metafile: true,
14-
entryPoints: [file],
15-
outfile: file.replace('src', 'dist').replace('.ts', '.js'),
16-
external: ["../selector.js", "../index.js", "./index.js"],
17-
bundle: true,
18-
format: 'esm',
19-
minify: true,
20-
sourcemap: 'external',
21-
target: 'node16',
22-
platform: 'node',
23-
}).then((metadata) => {
24-
const file = Object.keys(metadata.metafile.outputs)[1]
25-
const size = gzipSizeFromFileSync(file);
26-
const b = bytes(size);
27-
output[file] = b;
28-
})
29-
)
30-
}
31-
await Promise.all(promises);
32-
for (const [file, size] of Object.entries(output).sort(([a], [b]) => a.localeCompare(b))) {
33-
console.log(`${file} ${colors.green(size)}`);
34-
}
7+
const files = ["src/index.ts"];
8+
const output = {};
9+
const promises = [];
10+
for (const file of files) {
11+
promises.push(
12+
build({
13+
metafile: true,
14+
entryPoints: [file],
15+
outfile: file.replace("src", "dist").replace(".ts", ".js"),
16+
external: ["../selector.js", "../index.js", "./index.js"],
17+
bundle: true,
18+
format: "esm",
19+
minify: true,
20+
sourcemap: "external",
21+
target: "node16",
22+
platform: "node",
23+
}).then((metadata) => {
24+
const file = Object.keys(metadata.metafile.outputs)[1];
25+
const size = gzipSizeFromFileSync(file);
26+
const b = bytes(size);
27+
output[file] = b;
28+
}),
29+
);
30+
}
31+
await Promise.all(promises);
32+
for (const [file, size] of Object.entries(output).sort(([a], [b]) =>
33+
a.localeCompare(b),
34+
)) {
35+
console.log(`${file} ${colors.green(size)}`);
36+
}
3537
}
3638

3739
run();

src/index.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const set = (obj: NestedMapping, key: string, value: any, type?: string) => {
3737

3838
const type = (
3939
key: string,
40-
opts: Record<"boolean" | "string" | "array", string[]>
40+
opts: Record<"boolean" | "string" | "array", string[]>,
4141
): "boolean" | "string" | "array" | undefined => {
4242
if (opts.array && opts.array.length > 0 && opts.array.includes(key))
4343
return "array";
@@ -56,12 +56,13 @@ const defaultValue = (type?: "boolean" | "string" | "array") => {
5656

5757
const coerce = (value?: string, type?: "string" | "boolean" | "array") => {
5858
if (type === "string") return value;
59-
if (type === "boolean") return value === undefined ? true : value === 'true';
59+
if (type === "boolean") return value === undefined ? true : value === "true";
6060

6161
if (!value) return value;
6262
if (value.length > 3 && BOOL_RE.test(value)) return value === "true";
6363
if (value.length > 2 && QUOTED_RE.test(value)) return value.slice(1, -1);
64-
if (value[0] === '.' && /\d/.test(value[1]) || /\d/.test(value[0])) return Number(value);
64+
if ((value[0] === "." && /\d/.test(value[1])) || /\d/.test(value[0]))
65+
return Number(value);
6566
return value;
6667
};
6768

@@ -80,14 +81,14 @@ export function parse<
8081
TDefaults extends Record<string, unknown> | undefined = undefined,
8182
TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined,
8283
TAliasArgNames extends string = string,
83-
TAliasNames extends string = string
84+
TAliasNames extends string = string,
8485
>(
8586
argv: string[],
8687
{
8788
default: defaults,
8889
alias: aliases,
8990
...types
90-
}: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases> = {}
91+
}: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases> = {},
9192
): Args<TArgs> {
9293
const obj = { ...defaults, _: [] } as unknown as Args<TArgs>;
9394
if (argv.length === 0) return obj;
@@ -96,57 +97,60 @@ export function parse<
9697
const curr = argv[i];
9798
const next = argv[i + 1];
9899

99-
let t: 'string' | 'boolean' | 'array' | undefined;
100-
let key = '';
100+
let t: "string" | "boolean" | "array" | undefined;
101+
let key = "";
101102
let value: string | undefined;
102103

103104
if (curr.length > 1 && curr[0] === "-") {
104-
if (curr[1] !== "-" && curr.length > 2 && !curr.includes('=')) {
105-
if (curr.includes('.')) {
105+
if (curr[1] !== "-" && curr.length > 2 && !curr.includes("=")) {
106+
if (curr.includes(".")) {
106107
key = curr.slice(1, 2);
107108
value = curr.slice(2);
108109
} else {
109110
const keys = curr.slice(1, -1);
110111
for (let key of keys) {
111-
if (aliases && (aliases as Record<string, any>)[key] !== undefined) {
112+
if (
113+
aliases &&
114+
(aliases as Record<string, any>)[key] !== undefined
115+
) {
112116
key = aliases[key as keyof typeof aliases] as string;
113117
}
114-
set(obj, key, defaultValue(t), t)
118+
set(obj, key, defaultValue(t), t);
115119
}
116-
key = curr.slice(-1)
117-
if (next && next[0] !== '-') {
120+
key = curr.slice(-1);
121+
if (next && next[0] !== "-") {
118122
value = next;
119123
i++;
120124
}
121125
}
122126
} else if (!curr.includes("=") && next && next[0] !== "-") {
123-
key = curr.replace(/^-{1,2}/, '');
127+
key = curr.replace(/^-{1,2}/, "");
124128
t = type(key, types as any);
125129
// treat boolean as flag without parameter
126-
if (t === 'boolean') {
127-
value = 'true';
130+
if (t === "boolean") {
131+
value = "true";
128132
} else {
129133
value = next;
130134
i++;
131135
}
132136
} else {
133-
const eq = curr.indexOf('=');
137+
const eq = curr.indexOf("=");
134138
if (eq === -1) {
135-
key = curr.replace(/^-{1,2}/, '');
139+
key = curr.replace(/^-{1,2}/, "");
136140
} else {
137-
key = curr.slice(0, eq).replace(/^-{1,2}/, '');
141+
key = curr.slice(0, eq).replace(/^-{1,2}/, "");
138142
value = curr.slice(eq + 1);
139143
}
140144
t = type(key, types as any);
141145
}
142146

143-
if ((!t || t === "boolean") && key.length > 3 && key.startsWith('no-')) {
144-
set(obj, key.slice(3), false)
147+
if ((!t || t === "boolean") && key.length > 3 && key.startsWith("no-")) {
148+
set(obj, key.slice(3), false);
145149
} else {
146150
if (aliases && (aliases as Record<string, any>)[key] !== undefined) {
147151
key = aliases[key as keyof typeof aliases] as string;
148152
}
149-
set(obj, key, coerce(value, t) ?? defaultValue(t), t)
153+
set(obj, key, coerce(value, t) ?? defaultValue(t), t);
150154
}
151155
} else if (curr) {
152156
(obj as any)._.push(coerce(curr));

0 commit comments

Comments
 (0)