|
1 | | -/** |
2 | | - * A utility pack for handling `unknown` type. |
3 | | - * |
4 | | - * ## Usage |
5 | | - * |
6 | | - * It provides `is` module for type predicate functions and `assert`, `ensure`, and |
7 | | - * `maybe` helper functions. |
8 | | - * |
9 | | - * ### is\* and as\* |
10 | | - * |
11 | | - * Type predicate function is a function which returns `true` if a given value is |
12 | | - * expected type. For example, `isString` (or `is.String`) returns `true` if a |
13 | | - * given value is `string`. |
14 | | - * |
15 | | - * ```typescript |
16 | | - * import { is } from "@core/unknownutil"; |
17 | | - * |
18 | | - * const a: unknown = "Hello"; |
19 | | - * if (is.String(a)) { |
20 | | - * // 'a' is 'string' in this block |
21 | | - * } |
22 | | - * ``` |
23 | | - * |
24 | | - * For more complex types, you can use `is*Of` (or `is.*Of`) functions and `as*` |
25 | | - * (or `as.*`) functions like: |
26 | | - * |
27 | | - * ```typescript |
28 | | - * import { |
29 | | - * as, |
30 | | - * is, |
31 | | - * PredicateType, |
32 | | - * } from "@core/unknownutil"; |
33 | | - * |
34 | | - * const isArticle = is.ObjectOf({ |
35 | | - * title: is.String, |
36 | | - * body: is.String, |
37 | | - * refs: is.ArrayOf( |
38 | | - * is.UnionOf([ |
39 | | - * is.String, |
40 | | - * is.ObjectOf({ |
41 | | - * name: is.String, |
42 | | - * url: is.String, |
43 | | - * }), |
44 | | - * ]), |
45 | | - * ), |
46 | | - * createTime: as.Optional(is.InstanceOf(Date)), |
47 | | - * updateTime: as.Optional(is.InstanceOf(Date)), |
48 | | - * }); |
49 | | - * |
50 | | - * // Infer the type of `Article` from the definition of `isArticle` |
51 | | - * type Article = PredicateType<typeof isArticle>; |
52 | | - * |
53 | | - * const a: unknown = { |
54 | | - * title: "Awesome article", |
55 | | - * body: "This is an awesome article", |
56 | | - * refs: [{ name: "Deno", url: "https://deno.land/" }, "https://github.com"], |
57 | | - * }; |
58 | | - * if (isArticle(a)) { |
59 | | - * // a is narrowed to the type of `isArticle` |
60 | | - * console.log(a.title); |
61 | | - * console.log(a.body); |
62 | | - * for (const ref of a.refs) { |
63 | | - * if (is.String(ref)) { |
64 | | - * console.log(ref); |
65 | | - * } else { |
66 | | - * console.log(ref.name); |
67 | | - * console.log(ref.url); |
68 | | - * } |
69 | | - * } |
70 | | - * } |
71 | | - * ``` |
72 | | - * |
73 | | - * Additionally, you can manipulate the predicate function returned from |
74 | | - * `isObjectOf` with `isPickOf`, `isOmitOf`, `isPartialOf`, and `isRequiredOf` |
75 | | - * similar to TypeScript's `Pick`, `Omit`, `Partial`, `Required` utility types. |
76 | | - * |
77 | | - * ```typescript |
78 | | - * import { as, is } from "@core/unknownutil"; |
79 | | - * |
80 | | - * const isArticle = is.ObjectOf({ |
81 | | - * title: is.String, |
82 | | - * body: is.String, |
83 | | - * refs: is.ArrayOf( |
84 | | - * is.UnionOf([ |
85 | | - * is.String, |
86 | | - * is.ObjectOf({ |
87 | | - * name: is.String, |
88 | | - * url: is.String, |
89 | | - * }), |
90 | | - * ]), |
91 | | - * ), |
92 | | - * createTime: as.Optional(is.InstanceOf(Date)), |
93 | | - * updateTime: as.Optional(is.InstanceOf(Date)), |
94 | | - * }); |
95 | | - * |
96 | | - * const isArticleCreateParams = is.PickOf(isArticle, ["title", "body", "refs"]); |
97 | | - * // is equivalent to |
98 | | - * //const isArticleCreateParams = is.ObjectOf({ |
99 | | - * // title: is.String, |
100 | | - * // body: is.String, |
101 | | - * // refs: is.ArrayOf( |
102 | | - * // is.UnionOf([ |
103 | | - * // is.String, |
104 | | - * // is.ObjectOf({ |
105 | | - * // name: is.String, |
106 | | - * // url: is.String, |
107 | | - * // }), |
108 | | - * // ]), |
109 | | - * // ), |
110 | | - * //}); |
111 | | - * |
112 | | - * const isArticleUpdateParams = is.OmitOf(isArticleCreateParams, ["title"]); |
113 | | - * // is equivalent to |
114 | | - * //const isArticleUpdateParams = is.ObjectOf({ |
115 | | - * // body: is.String, |
116 | | - * // refs: is.ArrayOf( |
117 | | - * // is.UnionOf([ |
118 | | - * // is.String, |
119 | | - * // is.ObjectOf({ |
120 | | - * // name: is.String, |
121 | | - * // url: is.String, |
122 | | - * // }), |
123 | | - * // ]), |
124 | | - * // ), |
125 | | - * //}); |
126 | | - * |
127 | | - * const isArticlePatchParams = is.PartialOf(isArticleUpdateParams); |
128 | | - * // is equivalent to |
129 | | - * //const isArticlePatchParams = is.ObjectOf({ |
130 | | - * // body: as.Optional(is.String), |
131 | | - * // refs: as.Optional(is.ArrayOf( |
132 | | - * // is.UnionOf([ |
133 | | - * // is.String, |
134 | | - * // is.ObjectOf({ |
135 | | - * // name: is.String, |
136 | | - * // url: is.String, |
137 | | - * // }), |
138 | | - * // ]), |
139 | | - * // )), |
140 | | - * //}); |
141 | | - * |
142 | | - * const isArticleAvailableParams = is.RequiredOf(isArticle); |
143 | | - * // is equivalent to |
144 | | - * //const isArticlePutParams = is.ObjectOf({ |
145 | | - * // body: is.String, |
146 | | - * // refs: is.ArrayOf( |
147 | | - * // is.UnionOf([ |
148 | | - * // is.String, |
149 | | - * // is.ObjectOf({ |
150 | | - * // name: is.String, |
151 | | - * // url: is.String, |
152 | | - * // }), |
153 | | - * // ]), |
154 | | - * // ), |
155 | | - * // createTime: is.InstanceOf(Date), |
156 | | - * // updateTime: is.InstanceOf(Date), |
157 | | - * //}); |
158 | | - * ``` |
159 | | - * |
160 | | - * If you need an union type or an intersection type, use `isUnionOf` and `isIntersectionOf` |
161 | | - * like: |
162 | | - * |
163 | | - * ```typescript |
164 | | - * import { is } from "@core/unknownutil"; |
165 | | - * |
166 | | - * const isFoo = is.ObjectOf({ |
167 | | - * foo: is.String, |
168 | | - * }); |
169 | | - * |
170 | | - * const isBar = is.ObjectOf({ |
171 | | - * bar: is.String, |
172 | | - * }); |
173 | | - * |
174 | | - * const isFooOrBar = is.UnionOf([isFoo, isBar]); |
175 | | - * // { foo: string } | { bar: string } |
176 | | - * |
177 | | - * const isFooAndBar = is.IntersectionOf([isFoo, isBar]); |
178 | | - * // { foo: string } & { bar: string } |
179 | | - * ``` |
180 | | - * |
181 | | - * ### assert |
182 | | - * |
183 | | - * The `assert` function does nothing if a given value is expected type. Otherwise, |
184 | | - * it throws an `AssertError` exception like: |
185 | | - * |
186 | | - * ```typescript |
187 | | - * import { |
188 | | - * assert, |
189 | | - * is, |
190 | | - * } from "@core/unknownutil"; |
191 | | - * |
192 | | - * const a: unknown = "Hello"; |
193 | | - * |
194 | | - * // `assert` does nothing or throws an `AssertError` |
195 | | - * assert(a, is.String); |
196 | | - * // a is now narrowed to string |
197 | | - * |
198 | | - * // With custom message |
199 | | - * assert(a, is.String, { message: "a must be a string" }); |
200 | | - * ``` |
201 | | - * |
202 | | - * ### ensure |
203 | | - * |
204 | | - * The `ensure` function return the value as-is if a given value is expected type. |
205 | | - * Otherwise, it throws an `AssertError` exception like: |
206 | | - * |
207 | | - * ```typescript |
208 | | - * import { |
209 | | - * ensure, |
210 | | - * is, |
211 | | - * } from "@core/unknownutil"; |
212 | | - * |
213 | | - * const a: unknown = "Hello"; |
214 | | - * |
215 | | - * // `ensure` returns `string` or throws an `AssertError` |
216 | | - * const _: string = ensure(a, is.String); |
217 | | - * |
218 | | - * // With custom message |
219 | | - * const __: string = ensure(a, is.String, { message: "a must be a string" }); |
220 | | - * ``` |
221 | | - * |
222 | | - * ### maybe |
223 | | - * |
224 | | - * The `maybe` function return the value as-is if a given value is expected type. |
225 | | - * Otherwise, it returns `undefined` that suites with |
226 | | - * [nullish coalescing operator (`??`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) |
227 | | - * like: |
228 | | - * |
229 | | - * ```typescript |
230 | | - * import { |
231 | | - * is, |
232 | | - * maybe, |
233 | | - * } from "@core/unknownutil"; |
234 | | - * |
235 | | - * const a: unknown = "Hello"; |
236 | | - * |
237 | | - * // `maybe` returns `string | undefined` so it suites with `??` |
238 | | - * const _: string = maybe(a, is.String) ?? "default value"; |
239 | | - * ``` |
240 | | - * |
241 | | - * @module |
242 | | - */ |
243 | | - |
244 | 1 | export type * from "./type.ts"; |
245 | 2 |
|
246 | 3 | export * from "./as/mod.ts"; |
|
0 commit comments