diff --git a/.scripts/gen-mod.ts b/.scripts/gen-mod.ts index 620a036..92ce828 100644 --- a/.scripts/gen-mod.ts +++ b/.scripts/gen-mod.ts @@ -25,7 +25,7 @@ async function* iterModules(path: string): AsyncIterable { } } -async function generateModTs( +async function generateExportsTs( namespace: string, ): Promise { const path = fromFileUrl(import.meta.resolve(`../${namespace}/`)); @@ -45,32 +45,40 @@ 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 { + 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"); } @@ -78,6 +86,8 @@ async function generateModTs( async function main(): Promise { await generateModTs("is"); await generateModTs("as"); + await generateExportsTs("is"); + await generateExportsTs("as"); } if (import.meta.main) { diff --git a/as/as.ts b/as/as.ts new file mode 100644 index 0000000..aede8d9 --- /dev/null +++ b/as/as.ts @@ -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"; diff --git a/as/mod.ts b/as/mod.ts index 6b946e8..ab52faa 100644 --- a/as/mod.ts +++ b/as/mod.ts @@ -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"; diff --git a/deno.jsonc b/deno.jsonc index 2c2538d..4d7e63c 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -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", diff --git a/is/is.ts b/is/is.ts new file mode 100644 index 0000000..d943519 --- /dev/null +++ b/is/is.ts @@ -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"; diff --git a/is/mod.ts b/is/mod.ts index 0fd6be4..015bffa 100644 --- a/is/mod.ts +++ b/is/mod.ts @@ -1,46 +1,4 @@ // NOTE: This file is generated by gen-mod.ts -import { isAny } from "./any.ts"; -import { isArray } from "./array.ts"; -import { isArrayOf } from "./array_of.ts"; -import { isAsyncFunction } from "./async_function.ts"; -import { isBigint } from "./bigint.ts"; -import { isBoolean } from "./boolean.ts"; -import { isCustomJsonable } from "./custom_jsonable.ts"; -import { isFunction } from "./function.ts"; -import { isInstanceOf } from "./instance_of.ts"; -import { isIntersectionOf } from "./intersection_of.ts"; -import { isJsonable } from "./jsonable.ts"; -import { isLiteralOf } from "./literal_of.ts"; -import { isLiteralOneOf } from "./literal_one_of.ts"; -import { isMap } from "./map.ts"; -import { isMapOf } from "./map_of.ts"; -import { isNull } from "./null.ts"; -import { isNullish } from "./nullish.ts"; -import { isNumber } from "./number.ts"; -import { isObjectOf } from "./object_of.ts"; -import { isOmitOf } from "./omit_of.ts"; -import { isParametersOf } from "./parameters_of.ts"; -import { isPartialOf } from "./partial_of.ts"; -import { isPickOf } from "./pick_of.ts"; -import { isPrimitive } from "./primitive.ts"; -import { isReadonlyOf } from "./readonly_of.ts"; -import { isRecord } from "./record.ts"; -import { isRecordObject } from "./record_object.ts"; -import { isRecordObjectOf } from "./record_object_of.ts"; -import { isRecordOf } from "./record_of.ts"; -import { isRequiredOf } from "./required_of.ts"; -import { isSet } from "./set.ts"; -import { isSetOf } from "./set_of.ts"; -import { isStrictOf } from "./strict_of.ts"; -import { isString } from "./string.ts"; -import { isSymbol } from "./symbol.ts"; -import { isSyncFunction } from "./sync_function.ts"; -import { isTupleOf } from "./tuple_of.ts"; -import { isUndefined } from "./undefined.ts"; -import { isUniformTupleOf } from "./uniform_tuple_of.ts"; -import { isUnionOf } from "./union_of.ts"; -import { isUnknown } from "./unknown.ts"; - export * from "./any.ts"; export * from "./array.ts"; export * from "./array_of.ts"; @@ -82,1002 +40,4 @@ export * from "./undefined.ts"; export * from "./uniform_tuple_of.ts"; export * from "./union_of.ts"; export * from "./unknown.ts"; - -/** - * An object containing all the functions in is module. - */ -export const is: { - /** - * Assume `x is `any` and always return `true` regardless of the type of `x`. - * - * Use {@linkcode [is/unknown].isUnknown|isUnknown} to assume that a value is `unknown`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a = "a"; - * if (is.Any(a)) { - * const _: any = a; - * } - * ``` - */ - Any: typeof isAny; - /** - * Return `true` if the type of `x` is `unknown[]`. - * - * Use {@linkcode [is/arrayt-of].isArrayOf|isArrayOf} to check if the type of `x` is an array of `T`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = [0, 1, 2]; - * if (is.Array(a)) { - * const _: unknown[] = a; - * } - * ``` - */ - Array: typeof isArray; - /** - * Return a type predicate function that returns `true` if the type of `x` is `T[]`. - * - * Use {@linkcode [is/array].isArray|isArray} to check if the type of `x` is an array of `unknown`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.ArrayOf(is.String); - * const a: unknown = ["a", "b", "c"]; - * if (isMyType(a)) { - * const _: string[] = a; - * } - * ``` - */ - ArrayOf: typeof isArrayOf; - /** - * Return `true` if the type of `x` is `function` (async function). - * - * Use {@linkcode [is/function].isFunction|isFunction} to check if the type of `x` is a function. - * Use {@linkcode [is/sync-function].isSyncFunction|isSyncFunction} to check if the type of `x` is a synchronous function. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = async () => {}; - * if (is.AsyncFunction(a)) { - * const _: ((...args: unknown[]) => Promise) = a; - * } - * ``` - */ - AsyncFunction: typeof isAsyncFunction; - /** - * Return `true` if the type of `x` is `bigint`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = 0n; - * if (is.Bigint(a)) { - * const _: bigint = a; - * } - * ``` - */ - Bigint: typeof isBigint; - /** - * Return `true` if the type of `x` is `boolean`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = true; - * if (is.Boolean(a)) { - * const _: boolean = a; - * } - * ``` - */ - Boolean: typeof isBoolean; - /** - * Returns true if `x` is {@linkcode CustomJsonable}, false otherwise. - * - * Use {@linkcode [is/jsonable].isJsonable|isJsonable} to check if the type of `x` is a JSON-serializable. - * - * ```ts - * import { is, CustomJsonable } from "@core/unknownutil"; - * - * const a: unknown = Object.assign(42n, { - * toJSON() { - * return `${this}n`; - * } - * }); - * if (is.CustomJsonable(a)) { - * const _: CustomJsonable = a; - * } - * ``` - */ - CustomJsonable: typeof isCustomJsonable; - /** - * Return `true` if the type of `x` is `function`. - * - * Use {@linkcode [is/sync-function].isSyncFunction|isSyncFunction} to check if the type of `x` is a synchronous function. - * Use {@linkcode [is/async-function].isAsyncFunction|isAsyncFunction} to check if the type of `x` is an asynchronous function. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = () => {}; - * if (is.Function(a)) { - * const _: ((...args: unknown[]) => unknown) = a; - * } - * ``` - */ - Function: typeof isFunction; - /** - * Return `true` if the type of `x` is instance of `ctor`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.InstanceOf(Date); - * const a: unknown = new Date(); - * if (isMyType(a)) { - * const _: Date = a; - * } - * ``` - */ - InstanceOf: typeof isInstanceOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `IntersectionOf`. - * - * Use {@linkcode [is/union-of].isUnionOf|isUnionOf} to check if the type of `x` is a union of `T`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.IntersectionOf([ - * is.ObjectOf({ a: is.Number }), - * is.ObjectOf({ b: is.String }), - * ]); - * const a: unknown = { a: 0, b: "a" }; - * if (isMyType(a)) { - * const _: { a: number } & { b: string } = a; - * } - * ``` - * - * Depending on the version of TypeScript and how values are provided, it may be necessary to add `as const` to the array - * used as `preds`. If a type error occurs, try adding `as const` as follows: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const preds = [ - * is.ObjectOf({ a: is.Number }), - * is.ObjectOf({ b: is.String }), - * ] as const - * const isMyType = is.IntersectionOf(preds); - * const a: unknown = { a: 0, b: "a" }; - * if (isMyType(a)) { - * const _: { a: number } & { b: string } = a; - * } - * ``` - */ - IntersectionOf: typeof isIntersectionOf; - /** - * Returns true if `x` is a JSON-serializable value, false otherwise. - * - * It does not check array or object properties recursively. - * - * Use {@linkcode [is/custom_jsonable].isCustomJsonable|isCustomJsonable} to check if the type of `x` has a custom `toJSON` method. - * - * ```ts - * import { is, Jsonable } from "@core/unknownutil"; - * - * const a: unknown = "Hello, world!"; - * if (is.Jsonable(a)) { - * const _: Jsonable = a; - * } - * ``` - */ - Jsonable: typeof isJsonable; - /** - * Return a type predicate function that returns `true` if the type of `x` is a literal type of `pred`. - * - * Use {@linkcode [is/literal].isLiteral|isLiteral} to check if the type of `x` is a literal type. - * Use {@linkcode [is/literal-one-of].isLiteralOneOf|isLiteralOneOf} to check if the type of `x` is one of the literal type of {@linkcode [type].Primitive|Primitive[]}. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.LiteralOf("hello"); - * const a: unknown = "hello"; - * if (isMyType(a)) { - * const _: "hello" = a; - * } - * ``` - */ - LiteralOf: typeof isLiteralOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is one of literal type in `preds`. - * - * Use {@linkcode [is/literal].isLiteral|isLiteral} to check if the type of `x` is a literal type. - * Use {@linkcode [is/literal-of].isLiteralOf|isLiteralOf} to check if the type of `x` is a literal type of {@linkcode [type].Primitive|Primitive}. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.LiteralOneOf(["hello", "world"] as const); - * const a: unknown = "hello"; - * if (isMyType(a)) { - * const _: "hello" | "world" = a; - * } - * ``` - */ - LiteralOneOf: typeof isLiteralOneOf; - /** - * Return `true` if the type of `x` is `Map`. - * - * Use {@linkcode [is/map-of].isMapOf|isMapOf} to check if the type of `x` is a map of `T`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = new Map([["a", 0], ["b", 1]]); - * if (is.Map(a)) { - * const _: Map = a; - * } - * ``` - */ - Map: typeof isMap; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Map`. - * - * Use {@linkcode [is/map].isMap|isMap} to check if the type of `x` is a map of `unknown`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.MapOf(is.Number); - * const a: unknown = new Map([["a", 0], ["b", 1]]); - * if (isMyType(a)) { - * const _: Map = a; - * } - * ``` - * - * With predicate function for keys: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.MapOf(is.Number, is.String); - * const a: unknown = new Map([["a", 0], ["b", 1]]); - * if (isMyType(a)) { - * const _: Map = a; - * } - * ``` - */ - MapOf: typeof isMapOf; - /** - * Return `true` if the type of `x` is `null`. - * - * Use {@linkcode [is/undefined].isUndefined|isUndefined} to check if the type of `x` is `undefined`. - * Use {@linkcode [is/nullish].isNullish|isNullish} to check if the type of `x` is `null` or `undefined`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = null; - * if (is.Null(a)) { - * const _: null = a; - * } - * ``` - */ - Null: typeof isNull; - /** - * Return `true` if the type of `x` is `null` or `undefined`. - * - * Use {@linkcode [is/null].isNull|isNull} to check if the type of `x` is `null`. - * Use {@linkcode [is/undefined].isUndefined|isUndefined} to check if the type of `x` is `undefined`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = null; - * if (is.Nullish(a)) { - * const _: (null | undefined) = a; - * } - * ``` - */ - Nullish: typeof isNullish; - /** - * Return `true` if the type of `x` is `number`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = 0; - * if (is.Number(a)) { - * const _: number = a; - * } - * ``` - */ - Number: typeof isNumber; - /** - * Return a type predicate function that returns `true` if the type of `x` is `ObjectOf`. - * - * Use {@linkcode [is/record-of].isRecordOf|isRecordOf} if you want to check if the type of `x` is a record of `T`. - * - * If {@linkcode [as/optional].asOptional|asOptional} is specified in the predicate function in `predObj`, the property becomes optional. - * If {@linkcode [as/readonly].asReadonly|asReadonly} is specified in the predicate function in `predObj`, the property becomes readonly. - * - * The number of keys of `x` must be greater than or equal to the number of keys of `predObj`. - * Use {@linkcode [is/strict-of].isStrictOf|isStrictOf} if you want to check the exact number of keys. - * - * 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({ - * a: is.Number, - * b: is.String, - * c: as.Optional(is.Boolean), - * d: as.Readonly(is.String), - * }); - * const a: unknown = { a: 0, b: "a", d: "d" }; - * if (isMyType(a)) { - * const _: { a: number; b: string; c?: boolean | undefined, readonly d: string } = a; - * } - * ``` - */ - ObjectOf: typeof isObjectOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Omit, K>`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```typescript - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.OmitOf(is.ObjectOf({ - * a: is.Number, - * b: is.String, - * c: as.Optional(is.Boolean), - * }), ["a", "c"]); - * const a: unknown = { a: 0, b: "a" }; - * if (isMyType(a)) { - * const _: { b: string } = a; - * } - * ``` - */ - OmitOf: typeof isOmitOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `ParametersOf` or `ParametersOf`. - * - * This is similar to {@linkcode [is/tuple].isTupleOf|isTupleOf}, but if {@linkcode [as/optional].asOptional|asOptional} - * is specified at the trailing, the trailing elements becomes optional and makes variable-length tuple. - * - * 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.ParametersOf([ - * is.Number, - * as.Optional(is.String), - * is.Boolean, - * as.Optional(is.Number), - * as.Optional(is.String), - * as.Optional(is.Boolean), - * ] as const); - * const a: unknown = [0, undefined, "a"]; - * if (isMyType(a)) { - * const _: [number, string | undefined, boolean, (number | undefined)?, (string | undefined)?, (boolean | undefined)?] = a; - * } - * ``` - * - * With `predRest` to represent rest parameters: - * - * ```ts - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.ParametersOf( - * [ - * is.Number, - * as.Optional(is.String), - * as.Optional(is.Boolean), - * ] as const, - * is.ArrayOf(is.Number), - * ); - * const a: unknown = [0, "a", true, 0, 1, 2]; - * if (isMyType(a)) { - * const _: [number, (string | undefined)?, (boolean | undefined)?, ...number[]] = a; - * } - * ``` - * - * Depending on the version of TypeScript and how values are provided, it may be necessary to add `as const` to the array - * used as `predTup`. If a type error occurs, try adding `as const` as follows: - * - * ```ts - * import { as, is } from "@core/unknownutil"; - * - * const predTup = [is.Number, is.String, as.Optional(is.Boolean)] as const; - * const isMyType = is.ParametersOf(predTup); - * const a: unknown = [0, "a"]; - * if (isMyType(a)) { - * const _: [number, string, (boolean | undefined)?] = a; - * } - * ``` - */ - ParametersOf: typeof isParametersOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Partial>`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```typescript - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.PartialOf(is.ObjectOf({ - * a: is.Number, - * b: is.UnionOf([is.String, is.Undefined]), - * c: as.Optional(is.Boolean), - * })); - * const a: unknown = { a: undefined, other: "other" }; - * if (isMyType(a)) { - * const _: { a?: number | undefined; b?: string | undefined; c?: boolean | undefined } = a; - * } - * ``` - */ - PartialOf: typeof isPartialOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Pick, K>`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```typescript - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.PickOf(is.ObjectOf({ - * a: is.Number, - * b: is.String, - * c: as.Optional(is.Boolean), - * }), ["a", "c"]); - * const a: unknown = { a: 0, b: "a" }; - * if (isMyType(a)) { - * const _: { a: number; c?: boolean | undefined } = a; - * } - * ``` - */ - PickOf: typeof isPickOf; - /** - * Return `true` if the type of `x` is `Primitive`. - * - * ```ts - * import { is, type Primitive } from "@core/unknownutil"; - * - * const a: unknown = 0; - * if (is.Primitive(a)) { - * const _: Primitive = a; - * } - * ``` - */ - Primitive: typeof isPrimitive; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Readonly>`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```typescript - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.ReadonlyOf(is.ObjectOf({ - * a: is.Number, - * b: is.UnionOf([is.String, is.Undefined]), - * c: as.Readonly(is.Boolean), - * })); - * const a: unknown = { a: 0, b: "b", c: true }; - * if (isMyType(a)) { - * const _: { readonly a: number; readonly b: string | undefined; readonly c: boolean } = a; - * } - * ``` - */ - ReadonlyOf: typeof isReadonlyOf; - /** - * Return `true` if the type of `x` satisfies `Record`. - * - * Note that this function returns `true` for ambiguous instances like `Set`, `Map`, `Date`, `Promise`, etc. - * Use {@linkcode [is/record-object].isRecordObject|isRecordObject} instead if you want to check if `x` is an instance of `Object`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = {"a": 0, "b": 1}; - * if (is.Record(a)) { - * const _: Record = a; - * } - * - * const b: unknown = new Set(); - * if (is.Record(b)) { - * const _: Record = b; - * } - * ``` - */ - Record: typeof isRecord; - /** - * Return `true` if the type of `x` is an object instance that satisfies `Record`. - * - * Note that this function check if the `x` is an instance of `Object`. - * Use {@linkcode [is/record].isRecord|isRecord} instead if you want to check if the `x` satisfies the `Record` type. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = {"a": 0, "b": 1}; - * if (is.RecordObject(a)) { - * const _: Record = a; - * } - * - * const b: unknown = new Set(); - * if (is.RecordObject(b)) { - * // b is not a raw object, so it is not narrowed - * } - * ``` - */ - RecordObject: typeof isRecordObject; - /** - * Return a type predicate function that returns `true` if the type of `x` is an Object instance that satisfies `Record`. - * - * Note that this function check if the `x` is an instance of `Object`. - * Use {@linkcode [is/record-of].isRecordOf|isRecordOf} instead if you want to check if the `x` satisfies the `Record` type. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.RecordObjectOf(is.Number); - * const a: unknown = {"a": 0, "b": 1}; - * if (isMyType(a)) { - * const _: Record = a; - * } - * ``` - * - * With predicate function for keys: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.RecordObjectOf(is.Number, is.String); - * const a: unknown = {"a": 0, "b": 1}; - * if (isMyType(a)) { - * const _: Record = a; - * } - * ``` - */ - RecordObjectOf: typeof isRecordObjectOf; - /** - * Return a type predicate function that returns `true` if the type of `x` satisfies `Record`. - * - * Note that this function only check if the `x` satisfies the `Record` type. - * Use {@linkcode [is/record-object-of].isRecordObjectOf|isRecordObjectOf} instead if you want to check if the `x` is an instance of `Object`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.RecordOf(is.Number); - * const a: unknown = {"a": 0, "b": 1}; - * if (isMyType(a)) { - * const _: Record = a; - * } - * ``` - * - * With predicate function for keys: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.RecordOf(is.Number, is.String); - * const a: unknown = {"a": 0, "b": 1}; - * if (isMyType(a)) { - * const _: Record = a; - * } - * ``` - */ - RecordOf: typeof isRecordOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Required>`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```typescript - * import { as, is } from "@core/unknownutil"; - * - * const isMyType = is.RequiredOf(is.ObjectOf({ - * a: is.Number, - * b: is.UnionOf([is.String, is.Undefined]), - * c: as.Optional(is.Boolean), - * })); - * const a: unknown = { a: 0, b: "b", c: true, other: "other" }; - * if (isMyType(a)) { - * const _: { a: number; b: string | undefined; c: boolean | undefined } = a; - * } - * ``` - */ - RequiredOf: typeof isRequiredOf; - /** - * Return `true` if the type of `x` is `Set`. - * - * Use {@linkcode [is/set-of].isSetOf|isSetOf} to check if the type of `x` is a set of `T`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = new Set([0, 1, 2]); - * if (is.Set(a)) { - * const _: Set = a; - * } - * ``` - */ - Set: typeof isSet; - /** - * Return a type predicate function that returns `true` if the type of `x` is `Set`. - * - * Use {@linkcode [is/set].isSet|isSet} to check if the type of `x` is a set of `unknown`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.SetOf(is.String); - * const a: unknown = new Set(["a", "b", "c"]); - * if (isMyType(a)) { - * const _: Set = a; - * } - * ``` - */ - SetOf: typeof isSetOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is strictly follow the `ObjectOf`. - * - * It only supports modifing a predicate function annotated with `IsPredObj`, usually returned by the followings - * - * - {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} - * - {@linkcode [is/object-of].isObjectOf|isObjectOf} - * - {@linkcode [is/omit-of].isOmitOf|isOmitOf} - * - {@linkcode [is/partial-of].isPartialOf|isPartialOf} - * - {@linkcode [is/pick-of].isPickOf|isPickOf} - * - {@linkcode [is/readonly-of].isReadonlyOf|isReadonlyOf} - * - {@linkcode [is/required-of].isRequiredOf|isRequiredOf} - * - {@linkcode [is/strict-of].isStrictOf|isStrictOf} - * - * 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.StrictOf(is.ObjectOf({ - * a: is.Number, - * b: is.String, - * c: as.Optional(is.Boolean), - * })); - * const a: unknown = { a: 0, b: "a", other: "other" }; - * if (isMyType(a)) { - * // This block will not be executed because of "other" key in `a`. - * } - * ``` - */ - StrictOf: typeof isStrictOf; - /** - * Return `true` if the type of `x` is `string`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = "a"; - * if (is.String(a)) { - * const _: string = a; - * } - * ``` - */ - String: typeof isString; - /** - * Return `true` if the type of `x` is `symbol`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = Symbol("symbol"); - * if (is.Symbol(a)) { - * const _: symbol = a; - * } - * ``` - */ - Symbol: typeof isSymbol; - /** - * Return `true` if the type of `x` is `function` (non async function). - * - * Use {@linkcode [is/function].isFunction|isFunction} to check if the type of `x` is a function. - * Use {@linkcode [is/async-function].isAsyncFunction|isAsyncFunction} to check if the type of `x` is an asynchronous function. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = () => {}; - * if (is.SyncFunction(a)) { - * const _: ((...args: unknown[]) => unknown) = a; - * } - * ``` - */ - SyncFunction: typeof isSyncFunction; - /** - * Return a type predicate function that returns `true` if the type of `x` is `TupleOf`. - * - * Use {@linkcode [is/uniform-tuple-of].isUniformTupleOf|isUniformTupleOf} to check if the type of `x` is a tuple of uniform types. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.TupleOf([is.Number, is.String, is.Boolean]); - * const a: unknown = [0, "a", true]; - * if (isMyType(a)) { - * const _: [number, string, boolean] = a; - * } - * ``` - * - * With `predRest` to represent rest elements or leading rest elements: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.TupleOf( - * [is.Number, is.String, is.Boolean], - * is.ArrayOf(is.Number), - * ); - * const a: unknown = [0, "a", true, 0, 1, 2]; - * if (isMyType(a)) { - * const _: [number, string, boolean, ...number[]] = a; - * } - * - * const isMyTypeLeadingRest = is.TupleOf( - * is.ArrayOf(is.Number), - * [is.Number, is.String, is.Boolean], - * ); - * if (isMyTypeLeadingRest(a)) { - * const _: [...number[], number, string, boolean] = a; - * } - * ``` - * - * With `predRest` and `predTrail` to represent middle rest elements: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.TupleOf( - * [is.Number, is.String, is.Boolean], - * is.ArrayOf(is.Number), - * [is.Number, is.String, is.Boolean], - * ); - * const a: unknown = [0, "a", true, 0, 1, 2, 0, "a", true]; - * if (isMyType(a)) { - * const _: [number, string, boolean, ...number[], number, string, boolean] = a; - * } - * ``` - * - * Depending on the version of TypeScript and how values are provided, it may be necessary to add `as const` to the array - * used as `predTup`. If a type error occurs, try adding `as const` as follows: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const predTup = [is.Number, is.String, is.Boolean] as const; - * const isMyType = is.TupleOf(predTup); - * const a: unknown = [0, "a", true]; - * if (isMyType(a)) { - * const _: [number, string, boolean] = a; - * } - * ``` - */ - TupleOf: typeof isTupleOf; - /** - * Return `true` if the type of `x` is `undefined`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a: unknown = undefined; - * if (is.Undefined(a)) { - * const _: undefined = a; - * } - * ``` - */ - Undefined: typeof isUndefined; - /** - * Return a type predicate function that returns `true` if the type of `x` is `UniformTupleOf`. - * - * Use {@linkcode [is/tuple-of].isTupleOf|isTupleOf} to check if the type of `x` is a tuple of `T`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.UniformTupleOf(5); - * const a: unknown = [0, 1, 2, 3, 4]; - * if (isMyType(a)) { - * const _: [unknown, unknown, unknown, unknown, unknown] = a; - * } - * ``` - * - * With predicate function: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.UniformTupleOf(5, is.Number); - * const a: unknown = [0, 1, 2, 3, 4]; - * if (isMyType(a)) { - * const _: [number, number, number, number, number] = a; - * } - * ``` - */ - UniformTupleOf: typeof isUniformTupleOf; - /** - * Return a type predicate function that returns `true` if the type of `x` is `UnionOf`. - * - * Use {@linkcode [is/intersection-of].isIntersectionOf|isIntersectionOf} to check if the type of `x` is an intersection of `T`. - * - * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const isMyType = is.UnionOf([is.Number, is.String, is.Boolean]); - * const a: unknown = 0; - * if (isMyType(a)) { - * const _: number | string | boolean = a; - * } - * ``` - * - * Depending on the version of TypeScript and how values are provided, it may be necessary to add `as const` to the array - * used as `preds`. If a type error occurs, try adding `as const` as follows: - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const preds = [is.Number, is.String, is.Boolean] as const; - * const isMyType = is.UnionOf(preds); - * const a: unknown = 0; - * if (isMyType(a)) { - * const _: number | string | boolean = a; - * } - * ``` - */ - UnionOf: typeof isUnionOf; - /** - * Assume `x` is `unknown` and always return `true` regardless of the type of `x`. - * - * Use {@linkcode [is/any].isAny|isAny} to assume that the type of `x` is `any`. - * - * ```ts - * import { is } from "@core/unknownutil"; - * - * const a = "a"; - * if (is.Unknown(a)) { - * const _: unknown = a; - * } - * ``` - */ - Unknown: typeof isUnknown; -} = { - Any: isAny, - Array: isArray, - ArrayOf: isArrayOf, - AsyncFunction: isAsyncFunction, - Bigint: isBigint, - Boolean: isBoolean, - CustomJsonable: isCustomJsonable, - Function: isFunction, - InstanceOf: isInstanceOf, - IntersectionOf: isIntersectionOf, - Jsonable: isJsonable, - LiteralOf: isLiteralOf, - LiteralOneOf: isLiteralOneOf, - Map: isMap, - MapOf: isMapOf, - Null: isNull, - Nullish: isNullish, - Number: isNumber, - ObjectOf: isObjectOf, - OmitOf: isOmitOf, - ParametersOf: isParametersOf, - PartialOf: isPartialOf, - PickOf: isPickOf, - Primitive: isPrimitive, - ReadonlyOf: isReadonlyOf, - Record: isRecord, - RecordObject: isRecordObject, - RecordObjectOf: isRecordObjectOf, - RecordOf: isRecordOf, - RequiredOf: isRequiredOf, - Set: isSet, - SetOf: isSetOf, - StrictOf: isStrictOf, - String: isString, - Symbol: isSymbol, - SyncFunction: isSyncFunction, - TupleOf: isTupleOf, - Undefined: isUndefined, - UniformTupleOf: isUniformTupleOf, - UnionOf: isUnionOf, - Unknown: isUnknown, -}; +export * as is from "./is.ts"; diff --git a/mod.ts b/mod.ts index 2a11ea4..3d71c0f 100644 --- a/mod.ts +++ b/mod.ts @@ -1,6 +1,7 @@ export type * from "./type.ts"; export * from "./as/mod.ts"; + export * from "./is/mod.ts"; export * from "./assert.ts";