Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions .scripts/gen-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function* iterModules(path: string): AsyncIterable<string> {
}
}

async function generateModTs(
async function generateExportsTs(
namespace: string,
): Promise<void> {
const path = fromFileUrl(import.meta.resolve(`../${namespace}/`));
Expand All @@ -45,39 +45,49 @@ async function generateModTs(
const lines = [
"// NOTE: This file is generated by gen-mod.ts",
...exports.map((x) => {
return `import { ${x.name} } from "./${x.path}";`;
return `export { ${x.name} as ${
x.name.replace(namespace, "")
} } from "./${x.path}";`;
}),
"",
];
await Deno.writeTextFile(
join(path, `${namespace}.ts`),
lines.join("\n") + "\n",
);
}

async function generateModTs(
namespace: string,
): Promise<void> {
const path = fromFileUrl(import.meta.resolve(`../${namespace}/`));
const exports = (await Array.fromAsync(
flatMap(iterModules(path), (x) => doc(toFileUrl(x).href)),
))
.filter((x) => !!x.jsDoc?.doc)
.filter((x) => x.kind === "function")
.filter((x) => x.declarationKind === "export")
.filter((x) => x.name.startsWith(namespace))
.map((x) => ({
path: relative(path, fromFileUrl(x.location.filename)),
name: x.name,
doc: x.jsDoc!.doc!,
}))
.toSorted((a, b) => a.name.localeCompare(b.name));
const lines = [
"// NOTE: This file is generated by gen-mod.ts",
...map((new Set(exports.map((x) => x.path))).values(), (x) => {
return `export * from "./${x}";`;
}),
"",
"/**",
` * An object containing all the functions in ${namespace} module.`,
" */",
`export const ${namespace}: {`,
...exports.flatMap((x) => {
return [
" /**",
...x.doc.split("\n").map((line) => ` * ${line}`.trimEnd()),
" */",
` ${x.name.replace(namespace, "")}: typeof ${x.name};`.trimEnd(),
];
}),
"} = {",
...exports.flatMap((x) => {
return [
` ${x.name.replace(namespace, "")}: ${x.name},`.trimEnd(),
];
}),
"};",
`export * as ${namespace} from "./${namespace}.ts";`,
];
await Deno.writeTextFile(join(path, "mod.ts"), lines.join("\n") + "\n");
}

async function main(): Promise<void> {
await generateModTs("is");
await generateModTs("as");
await generateExportsTs("is");
await generateExportsTs("as");
}

if (import.meta.main) {
Expand Down
5 changes: 5 additions & 0 deletions as/as.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// NOTE: This file is generated by gen-mod.ts
export { asOptional as Optional } from "./optional.ts";
export { asReadonly as Readonly } from "./readonly.ts";
export { asUnoptional as Unoptional } from "./optional.ts";
export { asUnreadonly as Unreadonly } from "./readonly.ts";
109 changes: 1 addition & 108 deletions as/mod.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,4 @@
// NOTE: This file is generated by gen-mod.ts
import { asOptional } from "./optional.ts";
import { asReadonly } from "./readonly.ts";
import { asUnoptional } from "./optional.ts";
import { asUnreadonly } from "./readonly.ts";

export * from "./optional.ts";
export * from "./readonly.ts";

/**
* An object containing all the functions in as module.
*/
export const as: {
/**
* Annotate the given predicate function as optional.
*
* Use this function to annotate a predicate function of `predObj` in {@linkcode [is/object-of].isObjectOf|isObjectOf}.
*
* Note that the annotated predicate function will return `true` if the type of `x` is `T` or `undefined`, indicating that
* this function is not just for annotation but it also changes the behavior of the predicate function.
*
* Use {@linkcode asUnoptional} to remove the annotation.
* Use {@linkcode hasOptional} to check if a predicate function has annotated with this function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Optional(is.String),
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {foo?: string | undefined} = a;
* }
* ```
*/
Optional: typeof asOptional;
/**
* Annotate the given predicate function as readonly.
*
* Use this function to annotate a predicate function of `predObj` in {@linkcode [is/object-of].isObjectOf|isObjectOf}.
*
* Use {@linkcode asUnreadonly} to remove the annotation.
* Use {@linkcode hasReadonly} to check if a predicate function has annotated with this function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Readonly(is.String),
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {readonly foo: string} = a;
* }
* ```
*/
Readonly: typeof asReadonly;
/**
* Unannotate the annotated predicate function with {@linkcode asOptional}.
*
* Use this function to unannotate a predicate function of `predObj` in {@linkcode [is/object-of].isObjectOf|isObjectOf}.
*
* Note that the annotated predicate function will return `true` if the type of `x` is `T`, indicating that
* this function is not just for annotation but it also changes the behavior of the predicate function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Unoptional(as.Optional(is.String)),
* });
* const a: unknown = {foo: "a"};
* if (isMyType(a)) {
* const _: {foo: string} = a;
* }
* ```
*/
Unoptional: typeof asUnoptional;
/**
* Unannotate the annotated predicate function with {@linkcode asReadonly}.
*
* Use this function to unannotate a predicate function of `predObj` in {@linkcode [is/object-of].isObjectOf|isObjectOf}.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Unreadonly(as.Readonly(is.String)),
* });
* const a: unknown = {foo: "a"};
* if (isMyType(a)) {
* const _: {foo: string} = a;
* }
* ```
*/
Unreadonly: typeof asUnreadonly;
} = {
Optional: asOptional,
Readonly: asReadonly,
Unoptional: asUnoptional,
Unreadonly: asUnreadonly,
};
export * as as from "./as.ts";
2 changes: 2 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"exports": {
".": "./mod.ts",
"./as": "./as/mod.ts",
"./as/as": "./as/as.ts",
"./as/optional": "./as/optional.ts",
"./as/readonly": "./as/readonly.ts",
"./assert": "./assert.ts",
"./ensure": "./ensure.ts",
"./is": "./is/mod.ts",
"./is/is": "./is/is.ts",
"./is/any": "./is/any.ts",
"./is/array": "./is/array.ts",
"./is/array-of": "./is/array_of.ts",
Expand Down
42 changes: 42 additions & 0 deletions is/is.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// NOTE: This file is generated by gen-mod.ts
export { isAny as Any } from "./any.ts";
export { isArray as Array } from "./array.ts";
export { isArrayOf as ArrayOf } from "./array_of.ts";
export { isAsyncFunction as AsyncFunction } from "./async_function.ts";
export { isBigint as Bigint } from "./bigint.ts";
export { isBoolean as Boolean } from "./boolean.ts";
export { isCustomJsonable as CustomJsonable } from "./custom_jsonable.ts";
export { isFunction as Function } from "./function.ts";
export { isInstanceOf as InstanceOf } from "./instance_of.ts";
export { isIntersectionOf as IntersectionOf } from "./intersection_of.ts";
export { isJsonable as Jsonable } from "./jsonable.ts";
export { isLiteralOf as LiteralOf } from "./literal_of.ts";
export { isLiteralOneOf as LiteralOneOf } from "./literal_one_of.ts";
export { isMap as Map } from "./map.ts";
export { isMapOf as MapOf } from "./map_of.ts";
export { isNull as Null } from "./null.ts";
export { isNullish as Nullish } from "./nullish.ts";
export { isNumber as Number } from "./number.ts";
export { isObjectOf as ObjectOf } from "./object_of.ts";
export { isOmitOf as OmitOf } from "./omit_of.ts";
export { isParametersOf as ParametersOf } from "./parameters_of.ts";
export { isPartialOf as PartialOf } from "./partial_of.ts";
export { isPickOf as PickOf } from "./pick_of.ts";
export { isPrimitive as Primitive } from "./primitive.ts";
export { isReadonlyOf as ReadonlyOf } from "./readonly_of.ts";
export { isRecord as Record } from "./record.ts";
export { isRecordObject as RecordObject } from "./record_object.ts";
export { isRecordObjectOf as RecordObjectOf } from "./record_object_of.ts";
export { isRecordOf as RecordOf } from "./record_of.ts";
export { isRequiredOf as RequiredOf } from "./required_of.ts";
export { isSet as Set } from "./set.ts";
export { isSetOf as SetOf } from "./set_of.ts";
export { isStrictOf as StrictOf } from "./strict_of.ts";
export { isString as String } from "./string.ts";
export { isSymbol as Symbol } from "./symbol.ts";
export { isSyncFunction as SyncFunction } from "./sync_function.ts";
export { isTupleOf as TupleOf } from "./tuple_of.ts";
export { isUndefined as Undefined } from "./undefined.ts";
export { isUniformTupleOf as UniformTupleOf } from "./uniform_tuple_of.ts";
export { isUnionOf as UnionOf } from "./union_of.ts";
export { isUnknown as Unknown } from "./unknown.ts";
Loading