Skip to content

Commit 59c6d5c

Browse files
committed
Revert "Format Code, and Fix Spacing and Name in bench.js"
This reverts commit 4f460e5.
1 parent 4f460e5 commit 59c6d5c

File tree

6 files changed

+204
-257
lines changed

6 files changed

+204
-257
lines changed

scripts/bench.js

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
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 arg } from "../dist/index.js";
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";
77

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

1911
bench
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)))
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)))
3718
.run();

scripts/build.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
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]) =>
33-
a.localeCompare(b),
34-
)) {
35-
console.log(`${file} ${colors.green(size)}`);
36-
}
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+
}
3735
}
3836

3937
run();

src/index.ts

Lines changed: 22 additions & 26 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,13 +56,12 @@ 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]))
65-
return Number(value);
64+
if (value[0] === '.' && /\d/.test(value[1]) || /\d/.test(value[0])) return Number(value);
6665
return value;
6766
};
6867

@@ -81,14 +80,14 @@ export function parse<
8180
TDefaults extends Record<string, unknown> | undefined = undefined,
8281
TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined,
8382
TAliasArgNames extends string = string,
84-
TAliasNames extends string = string,
83+
TAliasNames extends string = string
8584
>(
8685
argv: string[],
8786
{
8887
default: defaults,
8988
alias: aliases,
9089
...types
91-
}: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases> = {},
90+
}: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases> = {}
9291
): Args<TArgs> {
9392
const obj = { ...defaults, _: [] } as unknown as Args<TArgs>;
9493
if (argv.length === 0) return obj;
@@ -97,60 +96,57 @@ export function parse<
9796
const curr = argv[i];
9897
const next = argv[i + 1];
9998

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

104103
if (curr.length > 1 && curr[0] === "-") {
105-
if (curr[1] !== "-" && curr.length > 2 && !curr.includes("=")) {
106-
if (curr.includes(".")) {
104+
if (curr[1] !== "-" && curr.length > 2 && !curr.includes('=')) {
105+
if (curr.includes('.')) {
107106
key = curr.slice(1, 2);
108107
value = curr.slice(2);
109108
} else {
110109
const keys = curr.slice(1, -1);
111110
for (let key of keys) {
112-
if (
113-
aliases &&
114-
(aliases as Record<string, any>)[key] !== undefined
115-
) {
111+
if (aliases && (aliases as Record<string, any>)[key] !== undefined) {
116112
key = aliases[key as keyof typeof aliases] as string;
117113
}
118-
set(obj, key, defaultValue(t), t);
114+
set(obj, key, defaultValue(t), t)
119115
}
120-
key = curr.slice(-1);
121-
if (next && next[0] !== "-") {
116+
key = curr.slice(-1)
117+
if (next && next[0] !== '-') {
122118
value = next;
123119
i++;
124120
}
125121
}
126122
} else if (!curr.includes("=") && next && next[0] !== "-") {
127-
key = curr.replace(/^-{1,2}/, "");
123+
key = curr.replace(/^-{1,2}/, '');
128124
t = type(key, types as any);
129125
// treat boolean as flag without parameter
130-
if (t === "boolean") {
131-
value = "true";
126+
if (t === 'boolean') {
127+
value = 'true';
132128
} else {
133129
value = next;
134130
i++;
135131
}
136132
} else {
137-
const eq = curr.indexOf("=");
133+
const eq = curr.indexOf('=');
138134
if (eq === -1) {
139-
key = curr.replace(/^-{1,2}/, "");
135+
key = curr.replace(/^-{1,2}/, '');
140136
} else {
141-
key = curr.slice(0, eq).replace(/^-{1,2}/, "");
137+
key = curr.slice(0, eq).replace(/^-{1,2}/, '');
142138
value = curr.slice(eq + 1);
143139
}
144140
t = type(key, types as any);
145141
}
146142

147-
if ((!t || t === "boolean") && key.length > 3 && key.startsWith("no-")) {
148-
set(obj, key.slice(3), false);
143+
if ((!t || t === "boolean") && key.length > 3 && key.startsWith('no-')) {
144+
set(obj, key.slice(3), false)
149145
} else {
150146
if (aliases && (aliases as Record<string, any>)[key] !== undefined) {
151147
key = aliases[key as keyof typeof aliases] as string;
152148
}
153-
set(obj, key, coerce(value, t) ?? defaultValue(t), t);
149+
set(obj, key, coerce(value, t) ?? defaultValue(t), t)
154150
}
155151
} else if (curr) {
156152
(obj as any)._.push(coerce(curr));

0 commit comments

Comments
 (0)