Skip to content

Commit 9b6876f

Browse files
committed
📝 Use $MODULE_VERSION in docs
1 parent b6a24eb commit 9b6876f

File tree

4 files changed

+162
-22
lines changed

4 files changed

+162
-22
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ expected type. For example, `isString` (or `is.String`) returns `true` if a
2323
given value is `string`.
2424

2525
```typescript
26-
import { is } from "./mod.ts";
26+
import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
2727

2828
const a: unknown = "Hello";
2929
if (is.String(a)) {
@@ -35,7 +35,10 @@ Additionally, `is*Of` (or `is.*Of`) functions return type predicate functions to
3535
predicate types of `x` more precisely like:
3636

3737
```typescript
38-
import { is, PredicateType } from "./mod.ts";
38+
import {
39+
is,
40+
PredicateType,
41+
} from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
3942

4043
const isArticle = is.ObjectOf({
4144
title: is.String,
@@ -47,7 +50,7 @@ const isArticle = is.ObjectOf({
4750
name: is.String,
4851
url: is.String,
4952
}),
50-
])
53+
]),
5154
),
5255
});
5356

@@ -79,7 +82,10 @@ The `assert` function does nothing if a given value is expected type. Otherwise,
7982
it throws an `AssertError` exception like:
8083

8184
```typescript
82-
import { assert, is } from "./mod.ts";
85+
import {
86+
assert,
87+
is,
88+
} from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
8389

8490
const a: unknown = "Hello";
8591

@@ -97,7 +103,10 @@ The `ensure` function return the value as-is if a given value is expected type.
97103
Otherwise, it throws an `AssertError` exception like:
98104

99105
```typescript
100-
import { ensure, is } from "./mod.ts";
106+
import {
107+
ensure,
108+
is,
109+
} from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
101110

102111
const a: unknown = "Hello";
103112

@@ -116,7 +125,10 @@ Otherwise, it returns `undefined` that suites with
116125
like:
117126

118127
```typescript
119-
import { is, maybe } from "./mod.ts";
128+
import {
129+
is,
130+
maybe,
131+
} from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
120132

121133
const a: unknown = "Hello";
122134

is.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export type TupleOf<T extends readonly Predicate<unknown>[]> = {
8686
* Return a type predicate function that returns `true` if the type of `x` is `TupleOf<T>`.
8787
*
8888
* ```ts
89-
* import is from "./is.ts";
89+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
9090
*
9191
* const predTup = [is.Number, is.String, is.Boolean] as const;
9292
* const a: unknown = [0, "a", true];
@@ -129,7 +129,7 @@ export type UniformTupleOf<
129129
* Return a type predicate function that returns `true` if the type of `x` is `UniformTupleOf<T>`.
130130
*
131131
* ```ts
132-
* import is from "./is.ts";
132+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
133133
*
134134
* const a: unknown = [0, 1, 2, 3, 4];
135135
* if (is.UniformTupleOf(5)(a)) {
@@ -223,7 +223,7 @@ export type ObjectOf<T extends RecordOf<Predicate<unknown>>> = FlatType<
223223
* Otherwise, the number of keys of `x` must be greater than or equal to the number of keys of `predObj`.
224224
*
225225
* ```ts
226-
* import is from "./is.ts";
226+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
227227
*
228228
* const predObj = {
229229
* a: is.Number,
@@ -310,7 +310,7 @@ export function isAsyncFunction(
310310
* Return `true` if the type of `x` is instance of `ctor`.
311311
*
312312
* ```ts
313-
* import is from "./is.ts";
313+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
314314
*
315315
* const a: unknown = new Date();
316316
* if (is.InstanceOf(Date)(a)) {
@@ -396,7 +396,8 @@ export function isLiteralOf<T extends Primitive>(literal: T): Predicate<T> {
396396
* Return a type predicate function that returns `true` if the type of `x` is one of literal type in `preds`.
397397
*
398398
* ```ts
399-
* import is from "./is.ts";
399+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
400+
*
400401
* const a: unknown = "hello";
401402
* if (is.LiteralOneOf(["hello", "world"] as const)(a)) {
402403
* // a is narrowed to "hello" | "world"
@@ -424,7 +425,7 @@ export type OneOf<T> = T extends Predicate<infer U>[] ? U : never;
424425
* Return a type predicate function that returns `true` if the type of `x` is `OneOf<T>`.
425426
*
426427
* ```ts
427-
* import is from "./is.ts";
428+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
428429
*
429430
* const preds = [is.Number, is.String, is.Boolean];
430431
* const a: unknown = 0;
@@ -457,7 +458,7 @@ export type AllOf<T> = UnionToIntersection<OneOf<T>>;
457458
* Return a type predicate function that returns `true` if the type of `x` is `AllOf<T>`.
458459
*
459460
* ```ts
460-
* import is from "./is.ts";
461+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
461462
*
462463
* const preds = [is.ObjectOf({ a: is.Number }), is.ObjectOf({ b: is.String })];
463464
* const a: unknown = { a: 0, b: "a" };
@@ -488,7 +489,7 @@ export type OptionalPredicate<T> = Predicate<T | undefined> & {
488489
* Return a type predicate function that returns `true` if the type of `x` is `T` or `undefined`.
489490
*
490491
* ```ts
491-
* import is from "./is.ts";
492+
* import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
492493
*
493494
* const a: unknown = "a";
494495
* if (is.OptionalOf(is.String)(a)) {

mod.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,134 @@
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\*
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 "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
17+
*
18+
* const a: unknown = "Hello";
19+
* if (is.String(a)) {
20+
* // 'a' is 'string' in this block
21+
* }
22+
* ```
23+
*
24+
* Additionally, `is*Of` (or `is.*Of`) functions return type predicate functions to
25+
* predicate types of `x` more precisely like:
26+
*
27+
* ```typescript
28+
* import {
29+
* is,
30+
* PredicateType,
31+
* } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
32+
*
33+
* const isArticle = is.ObjectOf({
34+
* title: is.String,
35+
* body: is.String,
36+
* refs: is.ArrayOf(
37+
* is.OneOf([
38+
* is.String,
39+
* is.ObjectOf({
40+
* name: is.String,
41+
* url: is.String,
42+
* }),
43+
* ]),
44+
* ),
45+
* });
46+
*
47+
* type Article = PredicateType<typeof isArticle>;
48+
*
49+
* const a: unknown = {
50+
* title: "Awesome article",
51+
* body: "This is an awesome article",
52+
* refs: [{ name: "Deno", url: "https://deno.land/" }, "https://github.com"],
53+
* };
54+
* if (isArticle(a)) {
55+
* // a is narrowed to the type of `isArticle`
56+
* console.log(a.title);
57+
* console.log(a.body);
58+
* for (const ref of a.refs) {
59+
* if (is.String(ref)) {
60+
* console.log(ref);
61+
* } else {
62+
* console.log(ref.name);
63+
* console.log(ref.url);
64+
* }
65+
* }
66+
* }
67+
* ```
68+
*
69+
* ### assert
70+
*
71+
* The `assert` function does nothing if a given value is expected type. Otherwise,
72+
* it throws an `AssertError` exception like:
73+
*
74+
* ```typescript
75+
* import {
76+
* assert,
77+
* is,
78+
* } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
79+
*
80+
* const a: unknown = "Hello";
81+
*
82+
* // `assert` does nothing or throws an `AssertError`
83+
* assert(a, is.String);
84+
* // a is now narrowed to string
85+
*
86+
* // With custom message
87+
* assert(a, is.String, { message: "a must be a string" });
88+
* ```
89+
*
90+
* ### ensure
91+
*
92+
* The `ensure` function return the value as-is if a given value is expected type.
93+
* Otherwise, it throws an `AssertError` exception like:
94+
*
95+
* ```typescript
96+
* import {
97+
* ensure,
98+
* is,
99+
* } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
100+
*
101+
* const a: unknown = "Hello";
102+
*
103+
* // `ensure` returns `string` or throws an `AssertError`
104+
* const _: string = ensure(a, is.String);
105+
*
106+
* // With custom message
107+
* const __: string = ensure(a, is.String, { message: "a must be a string" });
108+
* ```
109+
*
110+
* ### maybe
111+
*
112+
* The `maybe` function return the value as-is if a given value is expected type.
113+
* Otherwise, it returns `undefined` that suites with
114+
* [nullish coalescing operator (`??`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)
115+
* like:
116+
*
117+
* ```typescript
118+
* import {
119+
* is,
120+
* maybe,
121+
* } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
122+
*
123+
* const a: unknown = "Hello";
124+
*
125+
* // `maybe` returns `string | undefined` so it suites with `??`
126+
* const _: string = maybe(a, is.String) ?? "default value";
127+
* ```
128+
*
129+
* @module
130+
*/
131+
1132
export * from "./is.ts";
2133
export * from "./util.ts";
3134

util.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ export class AssertError extends Error {
4545
* @param factory The factory function.
4646
* @example
4747
* ```ts
48-
* import { setAssertMessageFactory } from "./util.ts";
49-
* import is from "./is.ts";
48+
* import { is, setAssertMessageFactory } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
5049
*
5150
* setAssertMessageFactory((x, pred) => {
5251
* if (pred === is.String) {
@@ -69,8 +68,7 @@ export function setAssertMessageFactory(factory: AssertMessageFactory): void {
6968
* Asserts that the given value satisfies the provided predicate.
7069
*
7170
* ```ts
72-
* import { assert } from "./util.ts";
73-
* import is from "./is.ts";
71+
* import { assert, is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
7472
*
7573
* const a: unknown = "hello";
7674
* assert(a, is.String);
@@ -99,8 +97,7 @@ export function assert<T>(
9997
* Ensures that the given value satisfies the provided predicate.
10098
*
10199
* ```ts
102-
* import { ensure } from "./util.ts";
103-
* import is from "./is.ts";
100+
* import { ensure, is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
104101
*
105102
* const a: unknown = "hello";
106103
* const _: string = ensure(a, is.String);
@@ -125,8 +122,7 @@ export function ensure<T>(
125122
* Returns the input value if it satisfies the provided predicate, or `undefined` otherwise.
126123
*
127124
* ```ts
128-
* import { maybe } from "./util.ts";
129-
* import is from "./is.ts";
125+
* import { is, maybe } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
130126
*
131127
* const a: unknown = "hello";
132128
* const _: string = maybe(a, is.String) ?? "default value";

0 commit comments

Comments
 (0)