Skip to content

Commit

Permalink
refactor: enforce uniform naming convention of internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 19, 2024
1 parent 1fbca7b commit 56992b2
Show file tree
Hide file tree
Showing 186 changed files with 2,650 additions and 1,920 deletions.
7 changes: 4 additions & 3 deletions packages/api/src/mixins/igrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const IGrid2DMixin = mixin(<IGrid2D<any, any>>{
*/
export const IGrid3DMixin = mixin(<IGrid3D<any, any>>{
order() {
return strideOrder(this.stride);
return __strideOrder(this.stride);
},

includes(x: number, y: number, z: number) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export const IGrid3DMixin = mixin(<IGrid3D<any, any>>{
*/
export const IGrid4DMixin = mixin(<IGrid4D<any, any>>{
order() {
return strideOrder(this.stride);
return __strideOrder(this.stride);
},

includes(x: number, y: number, z: number, w: number) {
Expand Down Expand Up @@ -202,7 +202,8 @@ export const IGrid4DMixin = mixin(<IGrid4D<any, any>>{
},
});

const strideOrder = (strides: NumericArray) =>
/** @internal */
const __strideOrder = (strides: NumericArray) =>
[...strides]
.map((x, i) => [x, i])
.sort((a, b) => Math.abs(b[0]) - Math.abs(a[0]))
Expand Down
33 changes: 21 additions & 12 deletions packages/args/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const parse = <T extends IObjectOf<any>>(
): Maybe<ParseResult<T>> => {
opts = { start: 2, showUsage: true, help: ["--help", "-h"], ...opts };
try {
return parseOpts(specs, argv, opts);
return __parseOpts(specs, argv, opts);
} catch (e) {
if (opts.showUsage) {
console.log(
Expand All @@ -26,12 +26,13 @@ export const parse = <T extends IObjectOf<any>>(
}
};

const parseOpts = <T extends IObjectOf<any>>(
/** @internal */
const __parseOpts = <T extends IObjectOf<any>>(
specs: Args<T>,
argv: string[],
opts: Partial<ParseOpts>
): Maybe<ParseResult<T>> => {
const aliases = aliasIndex<T>(specs);
const aliases = __aliasIndex<T>(specs);
const acc: any = {};
let id: Nullable<string>;
let spec: Nullable<ArgSpecExt>;
Expand All @@ -43,27 +44,28 @@ const parseOpts = <T extends IObjectOf<any>>(
console.log(usage(specs, opts.usageOpts));
return;
}
const state = parseKey(specs, aliases, acc, a);
const state = __parseKey(specs, aliases, acc, a);
id = state.id;
spec = state.spec;
i = i + ~~(state.state < 2);
if (state.state) break;
} else {
if (parseValue(spec!, acc, id, a)) break;
if (__parseValue(spec!, acc, id, a)) break;
id = null;
i++;
}
}
id && illegalArgs(`missing value for: --${id}`);
return {
result: processResults(specs, acc),
result: __processResults(specs, acc),
index: i,
rest: argv.slice(i),
done: i >= argv.length,
};
};

const aliasIndex = <T extends IObjectOf<any>>(specs: Args<T>) =>
/** @internal */
const __aliasIndex = <T extends IObjectOf<any>>(specs: Args<T>) =>
Object.entries(specs).reduce(
(acc, [k, v]) => (v.alias ? ((acc[v.alias] = k), acc) : acc),
<IObjectOf<string>>{}
Expand All @@ -75,7 +77,8 @@ interface ParseKeyResult {
spec?: ArgSpecExt;
}

const parseKey = <T extends IObjectOf<any>>(
/** @internal */
const __parseKey = <T extends IObjectOf<any>>(
specs: Args<T>,
aliases: IObjectOf<string>,
acc: any,
Expand Down Expand Up @@ -105,7 +108,8 @@ const parseKey = <T extends IObjectOf<any>>(
return { state: 2 };
};

const parseValue = (spec: ArgSpecExt, acc: any, id: string, a: string) => {
/** @internal */
const __parseValue = (spec: ArgSpecExt, acc: any, id: string, a: string) => {
/^-[a-z]/i.test(a) && illegalArgs(`missing value for: --${id}`);
if (spec!.multi) {
isArray(acc[id!]) ? acc[id!].push(a) : (acc[id!] = [a]);
Expand All @@ -115,7 +119,11 @@ const parseValue = (spec: ArgSpecExt, acc: any, id: string, a: string) => {
return spec!.fn && !spec!.fn(a);
};

const processResults = <T extends IObjectOf<any>>(specs: Args<T>, acc: any) => {
/** @internal */
const __processResults = <T extends IObjectOf<any>>(
specs: Args<T>,
acc: any
) => {
let spec: Nullable<ArgSpecExt>;
for (let id in specs) {
spec = specs[id];
Expand All @@ -126,13 +134,14 @@ const processResults = <T extends IObjectOf<any>>(specs: Args<T>, acc: any) => {
illegalArgs(`missing arg: --${id}`);
}
} else if (spec.coerce) {
coerceValue(spec, acc, id);
__coerceValue(spec, acc, id);
}
}
return acc;
};

const coerceValue = (spec: ArgSpecExt, acc: any, id: string) => {
/** @internal */
const __coerceValue = (spec: ArgSpecExt, acc: any, id: string) => {
try {
if (spec.multi && spec.delim) {
acc[id] = (<string[]>acc[id]).reduce(
Expand Down
47 changes: 27 additions & 20 deletions packages/args/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const usage = <T extends IObjectOf<any>>(
: <ColorTheme>{};
const indent = repeat(" ", opts.paramWidth!);
const format = (ids: string[]) =>
ids.map((id) => argUsage(id, specs[id], opts, theme, indent));
ids.map((id) => __argUsage(id, specs[id], opts, theme, indent));
const sortedIDs = Object.keys(specs).sort();
const groups: Pair<string, string[]>[] = opts.groups
? opts.groups
Expand All @@ -47,70 +47,75 @@ export const usage = <T extends IObjectOf<any>>(
.filter((g) => !!g[1].length)
: [["options", sortedIDs]];
return [
...wrap(opts.prefix, opts.lineWidth!),
...__wrap(opts.prefix, opts.lineWidth!),
...groups.map(([gid, ids]) =>
[
...(opts.showGroupNames ? [`${capitalize(gid)}:\n`] : []),
...format(ids),
"",
].join("\n")
),
...wrap(opts.suffix, opts.lineWidth!),
...__wrap(opts.suffix, opts.lineWidth!),
].join("\n");
};

const argUsage = (
/** @internal */
const __argUsage = (
id: string,
spec: ArgSpecExt,
opts: Partial<UsageOpts>,
theme: ColorTheme,
indent: string
) => {
const hint = argHint(spec, theme);
const alias = argAlias(spec, theme, hint);
const name = ansi(`--${kebab(id)}`, theme.param!);
const hint = __argHint(spec, theme);
const alias = __argAlias(spec, theme, hint);
const name = __ansi(`--${kebab(id)}`, theme.param!);
const params = `${alias}${name}${hint}`;
const isRequired = spec.optional === false && spec.default === undefined;
const prefixes: string[] = [];
isRequired && prefixes.push("required");
spec.multi && prefixes.push("multiple");
const body =
argPrefix(prefixes, theme, isRequired) +
__argPrefix(prefixes, theme, isRequired) +
(spec.desc || "") +
argDefault(spec, opts, theme);
__argDefault(spec, opts, theme);
return (
padRight(opts.paramWidth!)(params, lengthAnsi(params)) +
wrap(body, opts.lineWidth! - opts.paramWidth!)
__wrap(body, opts.lineWidth! - opts.paramWidth!)
.map((l, i) => (i > 0 ? indent + l : l))
.join("\n")
);
};

const argHint = (spec: ArgSpecExt, theme: ColorTheme) =>
spec.hint ? ansi(" " + spec.hint, theme.hint!) : "";
/** @internal */
const __argHint = (spec: ArgSpecExt, theme: ColorTheme) =>
spec.hint ? __ansi(" " + spec.hint, theme.hint!) : "";

const argAlias = (spec: ArgSpecExt, theme: ColorTheme, hint: string) =>
spec.alias ? `${ansi("-" + spec.alias, theme.param!)}${hint}, ` : "";
/** @internal */
const __argAlias = (spec: ArgSpecExt, theme: ColorTheme, hint: string) =>
spec.alias ? `${__ansi("-" + spec.alias, theme.param!)}${hint}, ` : "";

const argPrefix = (
/** @internal */
const __argPrefix = (
prefixes: string[],
theme: ColorTheme,
isRequired: boolean
) =>
prefixes.length
? ansi(
? __ansi(
`[${prefixes.join(", ")}] `,
isRequired ? theme.required! : theme.multi!
)
: "";

const argDefault = (
/** @internal */
const __argDefault = (
spec: ArgSpecExt,
opts: Partial<UsageOpts>,
theme: ColorTheme
) =>
opts.showDefaults && spec.default != null && spec.default !== false
? ansi(
? __ansi(
` (default: ${stringify(true)(
spec.defaultHint != undefined
? spec.defaultHint
Expand All @@ -120,10 +125,12 @@ const argDefault = (
)
: "";

const ansi = (x: string, col: number) =>
/** @internal */
const __ansi = (x: string, col: number) =>
col != null ? `\x1b[${col}m${x}\x1b[0m` : x;

const wrap = (str: Maybe<string>, width: number) =>
/** @internal */
const __wrap = (str: Maybe<string>, width: number) =>
str
? wordWrapLines(str, {
width,
Expand Down
7 changes: 4 additions & 3 deletions packages/arrays/src/levenshtein.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Predicate2 } from "@thi.ng/api";

const eqStrict = (a: any, b: any) => a === b;
/** @internal */
const __eqStrict = (a: any, b: any) => a === b;

/**
* Computes Levenshtein distance w/ optionally given `maxDist` (for
Expand Down Expand Up @@ -46,7 +47,7 @@ export const levenshtein = <T>(
a: ArrayLike<T>,
b: ArrayLike<T>,
maxDist = Infinity,
equiv: Predicate2<T> = eqStrict
equiv: Predicate2<T> = __eqStrict
): number => {
if (a === b) {
return 0;
Expand Down Expand Up @@ -161,7 +162,7 @@ export const normalizedLevenshtein = <T>(
a: ArrayLike<T>,
b: ArrayLike<T>,
maxDist = Infinity,
equiv = eqStrict
equiv = __eqStrict
): number => {
const n = Math.max(a.length, b.length);
return n > 0 ? levenshtein(a, b, maxDist, equiv) / n : 0;
Expand Down
2 changes: 2 additions & 0 deletions packages/associative/src/hash-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ interface HashMapState<K, V> {
size: number;
}

/** @internal */
const __private = new WeakMap<HashMap<any, any>, HashMapState<any, any>>();

/** @internal */
const __iterator = <K, V>(map: HashMap<K, V>, id: 0 | 1) =>
function* () {
for (let p of __private.get(map)!.bins) {
Expand Down
10 changes: 6 additions & 4 deletions packages/associative/src/internal/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ isNode() &&
inspect = m.inspect;
});

const inspectSet = (coll: Set<any>, opts: any) =>
/** @internal */
const __inspectSet = (coll: Set<any>, opts: any) =>
[...map((x) => inspect!(x, opts), coll)].join(", ");

const inspectMap = (coll: Map<any, any>, opts: any) =>
/** @internal */
const __inspectMap = (coll: Map<any, any>, opts: any) =>
[
...map(
([k, v]) => `${inspect!(k, opts)} => ${inspect!(v, opts)}`,
Expand Down Expand Up @@ -48,9 +50,9 @@ export const __inspectable = mixin({
`${name}(${this.size || 0}) {`,
inspect
? this instanceof Set
? inspectSet(this, childOpts)
? __inspectSet(this, childOpts)
: this instanceof Map
? inspectMap(this, childOpts)
? __inspectMap(this, childOpts)
: ""
: "",
"}",
Expand Down
10 changes: 6 additions & 4 deletions packages/associative/src/sparse-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ interface SparseSetProps {
n: number;
}

/** @internal */
const __private = new WeakMap<ASparseSet<any>, SparseSetProps>();

const fail = () => illegalArgs(`dense & sparse arrays must be of same size`);
/** @internal */
const __fail = () => illegalArgs(`dense & sparse arrays must be of same size`);

/**
* After "An Efficient Representation for Sparse Sets" Preston Briggs and Linda
Expand Down Expand Up @@ -165,7 +167,7 @@ export class SparseSet8
? super(new Uint8Array(n), new Uint8Array(n))
: n.length === sparse!.length
? super(n, sparse!)
: fail();
: __fail();
}

get [Symbol.species]() {
Expand Down Expand Up @@ -196,7 +198,7 @@ export class SparseSet16
? super(new Uint16Array(n), new Uint16Array(n))
: n.length === sparse!.length
? super(n, sparse!)
: fail();
: __fail();
}

get [Symbol.species]() {
Expand Down Expand Up @@ -227,7 +229,7 @@ export class SparseSet32
? super(new Uint32Array(n), new Uint32Array(n))
: n.length === sparse!.length
? super(n, sparse!)
: fail();
: __fail();
}

get [Symbol.species]() {
Expand Down
Loading

0 comments on commit 56992b2

Please sign in to comment.