Skip to content

Commit fae5555

Browse files
authored
Merge pull request #45 from lambdalisue/fix-docs
📝 Improve documentation
2 parents 6cdf1e3 + 5ef006f commit fae5555

File tree

8 files changed

+216
-44
lines changed

8 files changed

+216
-44
lines changed

.github/workflows/npm.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ jobs:
1313
publish:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v4
1717
with:
1818
fetch-depth: 0
1919
- uses: denoland/setup-deno@v1
2020
with:
2121
deno-version: ${{ env.DENO_VERSION }}
22-
- uses: actions/setup-node@v3
22+
- uses: actions/setup-node@v4
2323
with:
2424
node-version: ${{ env.NODE_VERSION }}
2525
registry-url: "https://registry.npmjs.org"

.github/workflows/test.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
check:
1717
runs-on: ubuntu-latest
1818
steps:
19-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
2020
- uses: denoland/setup-deno@v1
2121
with:
2222
deno-version: ${{ env.DENO_VERSION }}
@@ -31,19 +31,25 @@ jobs:
3131
test:
3232
runs-on: ubuntu-latest
3333
steps:
34-
- uses: actions/checkout@v3
34+
- uses: actions/checkout@v4
3535
- uses: denoland/setup-deno@v1
3636
with:
3737
deno-version: ${{ env.DENO_VERSION }}
3838
- name: Test
3939
run: |
40-
deno task test
40+
deno task test:coverage
4141
timeout-minutes: 5
42+
- run: |
43+
deno task coverage --lcov > coverage.lcov
44+
- uses: codecov/codecov-action@v3
45+
with:
46+
os: ${{ runner.os }}
47+
files: ./coverage.lcov
4248

4349
bench:
4450
runs-on: ubuntu-latest
4551
steps:
46-
- uses: actions/checkout@v3
52+
- uses: actions/checkout@v4
4753
- uses: denoland/setup-deno@v1
4854
with:
4955
deno-version: ${{ env.DENO_VERSION }}
@@ -55,11 +61,11 @@ jobs:
5561
build-npm:
5662
runs-on: ubuntu-latest
5763
steps:
58-
- uses: actions/checkout@v3
64+
- uses: actions/checkout@v4
5965
- uses: denoland/setup-deno@v1
6066
with:
6167
deno-version: ${{ env.DENO_VERSION }}
62-
- uses: actions/setup-node@v3
68+
- uses: actions/setup-node@v4
6369
with:
6470
node-version: ${{ env.NODE_VERSION }}
6571
registry-url: "https://registry.npmjs.org"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/npm
2+
/docs
23
deno.lock
4+
.coverage

README.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/unknownutil/mod.ts)
66
[![Test](https://github.com/lambdalisue/deno-unknownutil/workflows/Test/badge.svg)](https://github.com/lambdalisue/deno-unknownutil/actions?query=workflow%3ATest)
77
[![npm version](https://badge.fury.io/js/unknownutil.svg)](https://badge.fury.io/js/unknownutil)
8+
[![codecov](https://codecov.io/github/lambdalisue/deno-unknownutil/graph/badge.svg?token=pfbLRGU5AM)](https://codecov.io/github/lambdalisue/deno-unknownutil)
89

910
A utility pack for handling `unknown` type.
1011

@@ -15,14 +16,14 @@ A utility pack for handling `unknown` type.
1516
It provides `is` module for type predicate functions and `assert`, `ensure`, and
1617
`maybe` helper functions.
1718

18-
### is*
19+
### is\*
1920

2021
Type predicate function is a function which returns `true` if a given value is
2122
expected type. For example, `isString` (or `is.String`) returns `true` if a
2223
given value is `string`.
2324

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

2728
const a: unknown = "Hello";
2829
if (is.String(a)) {
@@ -34,29 +35,31 @@ Additionally, `is*Of` (or `is.*Of`) functions return type predicate functions to
3435
predicate types of `x` more precisely like:
3536

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

3943
const isArticle = is.ObjectOf({
4044
title: is.String,
4145
body: is.String,
42-
refs: is.ArrayOf(is.OneOf([
43-
is.String,
44-
is.ObjectOf({
45-
name: is.String,
46-
url: is.String,
47-
}),
48-
])),
46+
refs: is.ArrayOf(
47+
is.OneOf([
48+
is.String,
49+
is.ObjectOf({
50+
name: is.String,
51+
url: is.String,
52+
}),
53+
]),
54+
),
4955
});
5056

5157
type Article = PredicateType<typeof isArticle>;
5258

5359
const a: unknown = {
5460
title: "Awesome article",
5561
body: "This is an awesome article",
56-
refs: [
57-
{ name: "Deno", url: "https://deno.land/" },
58-
"https://github.com",
59-
],
62+
refs: [{ name: "Deno", url: "https://deno.land/" }, "https://github.com"],
6063
};
6164
if (isArticle(a)) {
6265
// a is narrowed to the type of `isArticle`
@@ -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,14 +125,35 @@ 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

123135
// `maybe` returns `string | undefined` so it suites with `??`
124136
const _: string = maybe(a, is.String) ?? "default value";
125137
```
126138

139+
## Node.js (npm)
140+
141+
To use `unknownutil` in [Node.js][nodejs], install `unknownutil` like
142+
143+
```console
144+
npm i unknownutil
145+
```
146+
147+
Then import `is`, `assert`, `ensure`, and `maybe` like:
148+
149+
```typescript, ignore
150+
import { assert, ensure, is, maybe } from "unknownutil";
151+
152+
// ...
153+
```
154+
155+
[nodejs]: https://nodejs.org/
156+
127157
## Migration
128158

129159
See [GitHub Wiki](https://github.com/lambdalisue/deno-unknownutil/wiki) for

deno.jsonc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{
22
"lock": false,
3+
"imports": {
4+
"https://deno.land/x/unknownutil@$MODULE_VERSION/": "./"
5+
},
36
"tasks": {
47
"build-npm": "deno run -A scripts/build_npm.ts $(git describe --tags --always --dirty)",
5-
"test": "deno test --unstable -A --parallel --doc",
6-
"check": "deno check --unstable $(find . -name '*.ts')",
7-
"upgrade": "deno run -A https://deno.land/x/udd/main.ts $(find . -name '*.ts' -not -path '*/npm/*')"
8+
"test": "deno test -A --parallel --shuffle --doc",
9+
"test:coverage": "deno task test --coverage=.coverage",
10+
"check": "deno check ./**/*.ts",
11+
"coverage": "deno coverage .coverage --exclude=is_bench.ts",
12+
"upgrade": "deno run -q -A https://deno.land/x/[email protected]/cli.ts ./**/*.ts",
13+
"upgrade:commit": "deno task -q upgrade --commit --prefix :package: --pre-commit=fmt"
814
}
915
}

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)) {

0 commit comments

Comments
 (0)