Skip to content

Commit e4d587c

Browse files
authored
Merge pull request #87 from jsr-core/next
🎉 unknownutil v4
2 parents cbe8323 + ea8956e commit e4d587c

File tree

129 files changed

+4654
-4622
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+4654
-4622
lines changed

.github/workflows/npm.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,6 @@ jobs:
5959
deno bench
6060
timeout-minutes: 5
6161

62-
build-npm:
63-
runs-on: ubuntu-latest
64-
steps:
65-
- uses: actions/checkout@v4
66-
- uses: denoland/setup-deno@v1
67-
with:
68-
deno-version: ${{ env.DENO_VERSION }}
69-
- uses: actions/setup-node@v4
70-
with:
71-
node-version: ${{ env.NODE_VERSION }}
72-
registry-url: "https://registry.npmjs.org"
73-
- name: Build
74-
run: deno task build-npm
75-
7662
jsr-publish:
7763
runs-on: ubuntu-latest
7864
steps:
File renamed without changes.

is_bench.ts renamed to .scripts/bench.ts

Lines changed: 9 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { is } from "./is.ts";
1+
import { as } from "../as/mod.ts";
2+
import { is } from "../is/mod.ts";
23

34
const cs: unknown[] = [
45
"Hello world",
@@ -60,10 +61,10 @@ Deno.bench({
6061
});
6162

6263
Deno.bench({
63-
name: "is.BigInt",
64+
name: "is.Bigint",
6465
fn: () => {
6566
for (const c of cs) {
66-
is.BigInt(c);
67+
is.Bigint(c);
6768
}
6869
},
6970
});
@@ -176,46 +177,6 @@ Deno.bench({
176177
},
177178
});
178179

179-
Deno.bench({
180-
name: "is.ReadonlyTupleOf<T>",
181-
fn: () => {
182-
const pred = is.ReadonlyTupleOf(predTup);
183-
for (const c of cs) {
184-
pred(c);
185-
}
186-
},
187-
});
188-
189-
const isReadonlyTupleOfPred = is.ReadonlyTupleOf(predTup);
190-
Deno.bench({
191-
name: "is.ReadonlyTupleOf<T> (pre)",
192-
fn: () => {
193-
for (const c of cs) {
194-
isReadonlyTupleOfPred(c);
195-
}
196-
},
197-
});
198-
199-
Deno.bench({
200-
name: "is.ReadonlyTupleOf<T, E>",
201-
fn: () => {
202-
const pred = is.ReadonlyTupleOf(predTup, is.Array);
203-
for (const c of cs) {
204-
pred(c);
205-
}
206-
},
207-
});
208-
209-
const isReadonlyTupleOfElsePred = is.ReadonlyTupleOf(predTup, is.Array);
210-
Deno.bench({
211-
name: "is.ReadonlyTupleOf<T, E> (pre)",
212-
fn: () => {
213-
for (const c of cs) {
214-
isReadonlyTupleOfElsePred(c);
215-
}
216-
},
217-
});
218-
219180
Deno.bench({
220181
name: "is.UniformTupleOf<N, T>",
221182
fn: () => {
@@ -236,26 +197,6 @@ Deno.bench({
236197
},
237198
});
238199

239-
Deno.bench({
240-
name: "is.ReadonlyUniformTupleOf<N, T>",
241-
fn: () => {
242-
const pred = is.ReadonlyUniformTupleOf(3, is.String);
243-
for (const c of cs) {
244-
pred(c);
245-
}
246-
},
247-
});
248-
249-
const isReadonlyUniformTupleOfPred = is.ReadonlyUniformTupleOf(3, is.String);
250-
Deno.bench({
251-
name: "is.ReadonlyUniformTupleOf<N, T> (pre)",
252-
fn: () => {
253-
for (const c of cs) {
254-
isReadonlyUniformTupleOfPred(c);
255-
}
256-
},
257-
});
258-
259200
Deno.bench({
260201
name: "is.Record",
261202
fn: () => {
@@ -328,15 +269,6 @@ Deno.bench({
328269
}
329270
},
330271
});
331-
Deno.bench({
332-
name: "is.ObjectOf<T> (strict)",
333-
fn: () => {
334-
const pred = is.ObjectOf(predObj, { strict: true });
335-
for (const c of cs) {
336-
pred(c);
337-
}
338-
},
339-
});
340272

341273
const isObjectOfPred = is.ObjectOf(predObj);
342274
Deno.bench({
@@ -348,16 +280,6 @@ Deno.bench({
348280
},
349281
});
350282

351-
const isObjectOfStrictPred = is.ObjectOf(predObj, { strict: true });
352-
Deno.bench({
353-
name: "is.ObjectOf<T> (pre, strict)",
354-
fn: () => {
355-
for (const c of cs) {
356-
isObjectOfStrictPred(c);
357-
}
358-
},
359-
});
360-
361283
Deno.bench({
362284
name: "is.Function",
363285
fn: () => {
@@ -492,67 +414,22 @@ Deno.bench({
492414
},
493415
});
494416

495-
const predsOne = [is.String, is.Number, is.Boolean] as const;
496-
Deno.bench({
497-
name: "is.OneOf<T>",
498-
fn: () => {
499-
const pred = is.OneOf(predsOne);
500-
for (const c of cs) {
501-
pred(c);
502-
}
503-
},
504-
});
505-
506-
const isOneOfPred = is.OneOf(predsOne);
507-
Deno.bench({
508-
name: "is.OneOf<T> (pre)",
509-
fn: () => {
510-
for (const c of cs) {
511-
isOneOfPred(c);
512-
}
513-
},
514-
});
515-
516-
const predsAll = [
517-
is.ObjectOf({ a: is.Number }),
518-
is.ObjectOf({ b: is.String }),
519-
] as const;
520-
Deno.bench({
521-
name: "is.AllOf<T>",
522-
fn: () => {
523-
const pred = is.AllOf(predsAll);
524-
for (const c of cs) {
525-
pred(c);
526-
}
527-
},
528-
});
529-
530-
const isAllOfPred = is.AllOf(predsAll);
531-
Deno.bench({
532-
name: "is.AllOf<T> (pre)",
533-
fn: () => {
534-
for (const c of cs) {
535-
isAllOfPred(c);
536-
}
537-
},
538-
});
539-
540417
Deno.bench({
541-
name: "is.OptionalOf<T>",
418+
name: "as.Optional<T>",
542419
fn: () => {
543-
const pred = is.OptionalOf(is.String);
420+
const pred = as.Optional(is.String);
544421
for (const c of cs) {
545422
pred(c);
546423
}
547424
},
548425
});
549426

550-
const isOptionalOfPred = is.OptionalOf(is.String);
427+
const asOptionalPred = as.Optional(is.String);
551428
Deno.bench({
552-
name: "is.OptionalOf<T> (pre)",
429+
name: "as.Optional<T> (pre)",
553430
fn: () => {
554431
for (const c of cs) {
555-
isOptionalOfPred(c);
432+
asOptionalPred(c);
556433
}
557434
},
558435
});
File renamed without changes.

README.md

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
# unknownutil
22

33
[![jsr](https://jsr.io/badges/@core/unknownutil)](https://jsr.io/@core/unknownutil)
4-
[![npm](https://img.shields.io/npm/v/unknownutil?logo=npm&logoColor=white)](https://www.npmjs.com/package/unknownutil)
5-
[![denoland](https://img.shields.io/github/v/release/jsr-core/unknownutil?logo=deno&label=denoland)](https://deno.land/x/unknownutil)
6-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/unknownutil/mod.ts)
74
[![test](https://github.com/jsr-core/unknownutil/workflows/Test/badge.svg)](https://github.com/jsr-core/unknownutil/actions?query=workflow%3ATest)
85
[![codecov](https://codecov.io/github/jsr-core/unknownutil/graph/badge.svg?token=pfbLRGU5AM)](https://codecov.io/github/jsr-core/unknownutil)
96

107
A utility pack for handling `unknown` type.
118

12-
[deno]: https://deno.land/
13-
14-
> [!WARNING]
15-
>
16-
> The package on [deno.land] and [npm] is deprecated. Use the package on
17-
> [jsr.io] instead.
18-
>
19-
> ```
20-
> deno add @core/unknownutil
21-
> npx jsr add @core/unknownutil
22-
> ```
23-
24-
[deno.land]: https://deno.land/x/unknownutil
25-
[npm]: https://www.npmjs.com/package/unknownutil
269
[jsr.io]: https://jsr.io/@core/unknownutil
2710

2811
## Usage
2912

30-
It provides `is` module for type predicate functions and `assert`, `ensure`, and
31-
`maybe` helper functions.
13+
It provides `is` and `as` module for type predicate functions and `assert`,
14+
`ensure`, and `maybe` helper functions.
3215

33-
### is\*
16+
### is\* and as\*
3417

3518
Type predicate function is a function which returns `true` if a given value is
3619
expected type. For example, `isString` (or `is.String`) returns `true` if a
@@ -45,10 +28,11 @@ if (is.String(a)) {
4528
}
4629
```
4730

48-
For more complex types, you can use `is*Of` (or `is.*Of`) functions like:
31+
For more complex types, you can use `is*Of` (or `is.*Of`) functions and `as*`
32+
(or `as.*`) like:
4933

5034
```typescript
51-
import { is, PredicateType } from "@core/unknownutil";
35+
import { as, is, PredicateType } from "@core/unknownutil";
5236

5337
const isArticle = is.ObjectOf({
5438
title: is.String,
@@ -62,8 +46,8 @@ const isArticle = is.ObjectOf({
6246
}),
6347
]),
6448
),
65-
createTime: is.OptionalOf(is.InstanceOf(Date)),
66-
updateTime: is.OptionalOf(is.InstanceOf(Date)),
49+
createTime: as.Optional(is.InstanceOf(Date)),
50+
updateTime: as.Optional(is.InstanceOf(Date)),
6751
});
6852

6953
// Infer the type of `Article` from the definition of `isArticle`
@@ -94,7 +78,7 @@ Additionally, you can manipulate the predicate function returned from
9478
similar to TypeScript's `Pick`, `Omit`, `Partial`, `Required` utility types.
9579

9680
```typescript
97-
import { is } from "@core/unknownutil";
81+
import { as, is } from "@core/unknownutil";
9882

9983
const isArticle = is.ObjectOf({
10084
title: is.String,
@@ -108,8 +92,8 @@ const isArticle = is.ObjectOf({
10892
}),
10993
]),
11094
),
111-
createTime: is.OptionalOf(is.InstanceOf(Date)),
112-
updateTime: is.OptionalOf(is.InstanceOf(Date)),
95+
createTime: as.Optional(is.InstanceOf(Date)),
96+
updateTime: as.Optional(is.InstanceOf(Date)),
11397
});
11498

11599
const isArticleCreateParams = is.PickOf(isArticle, ["title", "body", "refs"]);
@@ -146,8 +130,8 @@ const isArticleUpdateParams = is.OmitOf(isArticleCreateParams, ["title"]);
146130
const isArticlePatchParams = is.PartialOf(isArticleUpdateParams);
147131
// is equivalent to
148132
//const isArticlePatchParams = is.ObjectOf({
149-
// body: is.OptionalOf(is.String),
150-
// refs: is.OptionalOf(is.ArrayOf(
133+
// body: as.Optional(is.String),
134+
// refs: as.Optional(is.ArrayOf(
151135
// is.UnionOf([
152136
// is.String,
153137
// is.ObjectOf({
@@ -248,11 +232,6 @@ const a: unknown = "Hello";
248232
const _: string = maybe(a, is.String) ?? "default value";
249233
```
250234

251-
## Migration
252-
253-
See [GitHub Wiki](https://github.com/jsr-core/unknownutil/wiki) for migration to
254-
v3 from v2 or v2 from v1.
255-
256235
## License
257236

258237
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need

0 commit comments

Comments
 (0)