Skip to content

Commit

Permalink
Merge pull request #87 from jsr-core/next
Browse files Browse the repository at this point in the history
🎉 unknownutil v4
  • Loading branch information
lambdalisue authored Aug 2, 2024
2 parents cbe8323 + ea8956e commit e4d587c
Show file tree
Hide file tree
Showing 129 changed files with 4,654 additions and 4,622 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/npm.yml

This file was deleted.

14 changes: 0 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ jobs:
deno bench
timeout-minutes: 5

build-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: ${{ env.DENO_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: "https://registry.npmjs.org"
- name: Build
run: deno task build-npm

jsr-publish:
runs-on: ubuntu-latest
steps:
Expand Down
File renamed without changes.
141 changes: 9 additions & 132 deletions is_bench.ts → .scripts/bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { is } from "./is.ts";
import { as } from "../as/mod.ts";
import { is } from "../is/mod.ts";

const cs: unknown[] = [
"Hello world",
Expand Down Expand Up @@ -60,10 +61,10 @@ Deno.bench({
});

Deno.bench({
name: "is.BigInt",
name: "is.Bigint",
fn: () => {
for (const c of cs) {
is.BigInt(c);
is.Bigint(c);
}
},
});
Expand Down Expand Up @@ -176,46 +177,6 @@ Deno.bench({
},
});

Deno.bench({
name: "is.ReadonlyTupleOf<T>",
fn: () => {
const pred = is.ReadonlyTupleOf(predTup);
for (const c of cs) {
pred(c);
}
},
});

const isReadonlyTupleOfPred = is.ReadonlyTupleOf(predTup);
Deno.bench({
name: "is.ReadonlyTupleOf<T> (pre)",
fn: () => {
for (const c of cs) {
isReadonlyTupleOfPred(c);
}
},
});

Deno.bench({
name: "is.ReadonlyTupleOf<T, E>",
fn: () => {
const pred = is.ReadonlyTupleOf(predTup, is.Array);
for (const c of cs) {
pred(c);
}
},
});

const isReadonlyTupleOfElsePred = is.ReadonlyTupleOf(predTup, is.Array);
Deno.bench({
name: "is.ReadonlyTupleOf<T, E> (pre)",
fn: () => {
for (const c of cs) {
isReadonlyTupleOfElsePred(c);
}
},
});

Deno.bench({
name: "is.UniformTupleOf<N, T>",
fn: () => {
Expand All @@ -236,26 +197,6 @@ Deno.bench({
},
});

Deno.bench({
name: "is.ReadonlyUniformTupleOf<N, T>",
fn: () => {
const pred = is.ReadonlyUniformTupleOf(3, is.String);
for (const c of cs) {
pred(c);
}
},
});

const isReadonlyUniformTupleOfPred = is.ReadonlyUniformTupleOf(3, is.String);
Deno.bench({
name: "is.ReadonlyUniformTupleOf<N, T> (pre)",
fn: () => {
for (const c of cs) {
isReadonlyUniformTupleOfPred(c);
}
},
});

Deno.bench({
name: "is.Record",
fn: () => {
Expand Down Expand Up @@ -328,15 +269,6 @@ Deno.bench({
}
},
});
Deno.bench({
name: "is.ObjectOf<T> (strict)",
fn: () => {
const pred = is.ObjectOf(predObj, { strict: true });
for (const c of cs) {
pred(c);
}
},
});

const isObjectOfPred = is.ObjectOf(predObj);
Deno.bench({
Expand All @@ -348,16 +280,6 @@ Deno.bench({
},
});

const isObjectOfStrictPred = is.ObjectOf(predObj, { strict: true });
Deno.bench({
name: "is.ObjectOf<T> (pre, strict)",
fn: () => {
for (const c of cs) {
isObjectOfStrictPred(c);
}
},
});

Deno.bench({
name: "is.Function",
fn: () => {
Expand Down Expand Up @@ -492,67 +414,22 @@ Deno.bench({
},
});

const predsOne = [is.String, is.Number, is.Boolean] as const;
Deno.bench({
name: "is.OneOf<T>",
fn: () => {
const pred = is.OneOf(predsOne);
for (const c of cs) {
pred(c);
}
},
});

const isOneOfPred = is.OneOf(predsOne);
Deno.bench({
name: "is.OneOf<T> (pre)",
fn: () => {
for (const c of cs) {
isOneOfPred(c);
}
},
});

const predsAll = [
is.ObjectOf({ a: is.Number }),
is.ObjectOf({ b: is.String }),
] as const;
Deno.bench({
name: "is.AllOf<T>",
fn: () => {
const pred = is.AllOf(predsAll);
for (const c of cs) {
pred(c);
}
},
});

const isAllOfPred = is.AllOf(predsAll);
Deno.bench({
name: "is.AllOf<T> (pre)",
fn: () => {
for (const c of cs) {
isAllOfPred(c);
}
},
});

Deno.bench({
name: "is.OptionalOf<T>",
name: "as.Optional<T>",
fn: () => {
const pred = is.OptionalOf(is.String);
const pred = as.Optional(is.String);
for (const c of cs) {
pred(c);
}
},
});

const isOptionalOfPred = is.OptionalOf(is.String);
const asOptionalPred = as.Optional(is.String);
Deno.bench({
name: "is.OptionalOf<T> (pre)",
name: "as.Optional<T> (pre)",
fn: () => {
for (const c of cs) {
isOptionalOfPred(c);
asOptionalPred(c);
}
},
});
File renamed without changes.
47 changes: 13 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
# unknownutil

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

A utility pack for handling `unknown` type.

[deno]: https://deno.land/

> [!WARNING]
>
> The package on [deno.land] and [npm] is deprecated. Use the package on
> [jsr.io] instead.
>
> ```
> deno add @core/unknownutil
> npx jsr add @core/unknownutil
> ```
[deno.land]: https://deno.land/x/unknownutil
[npm]: https://www.npmjs.com/package/unknownutil
[jsr.io]: https://jsr.io/@core/unknownutil

## Usage

It provides `is` module for type predicate functions and `assert`, `ensure`, and
`maybe` helper functions.
It provides `is` and `as` module for type predicate functions and `assert`,
`ensure`, and `maybe` helper functions.

### is\*
### is\* and as\*

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

For more complex types, you can use `is*Of` (or `is.*Of`) functions like:
For more complex types, you can use `is*Of` (or `is.*Of`) functions and `as*`
(or `as.*`) like:

```typescript
import { is, PredicateType } from "@core/unknownutil";
import { as, is, PredicateType } from "@core/unknownutil";

const isArticle = is.ObjectOf({
title: is.String,
Expand All @@ -62,8 +46,8 @@ const isArticle = is.ObjectOf({
}),
]),
),
createTime: is.OptionalOf(is.InstanceOf(Date)),
updateTime: is.OptionalOf(is.InstanceOf(Date)),
createTime: as.Optional(is.InstanceOf(Date)),
updateTime: as.Optional(is.InstanceOf(Date)),
});

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

```typescript
import { is } from "@core/unknownutil";
import { as, is } from "@core/unknownutil";

const isArticle = is.ObjectOf({
title: is.String,
Expand All @@ -108,8 +92,8 @@ const isArticle = is.ObjectOf({
}),
]),
),
createTime: is.OptionalOf(is.InstanceOf(Date)),
updateTime: is.OptionalOf(is.InstanceOf(Date)),
createTime: as.Optional(is.InstanceOf(Date)),
updateTime: as.Optional(is.InstanceOf(Date)),
});

const isArticleCreateParams = is.PickOf(isArticle, ["title", "body", "refs"]);
Expand Down Expand Up @@ -146,8 +130,8 @@ const isArticleUpdateParams = is.OmitOf(isArticleCreateParams, ["title"]);
const isArticlePatchParams = is.PartialOf(isArticleUpdateParams);
// is equivalent to
//const isArticlePatchParams = is.ObjectOf({
// body: is.OptionalOf(is.String),
// refs: is.OptionalOf(is.ArrayOf(
// body: as.Optional(is.String),
// refs: as.Optional(is.ArrayOf(
// is.UnionOf([
// is.String,
// is.ObjectOf({
Expand Down Expand Up @@ -248,11 +232,6 @@ const a: unknown = "Hello";
const _: string = maybe(a, is.String) ?? "default value";
```

## Migration

See [GitHub Wiki](https://github.com/jsr-core/unknownutil/wiki) for migration to
v3 from v2 or v2 from v1.

## License

The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
Expand Down
File renamed without changes.
Loading

0 comments on commit e4d587c

Please sign in to comment.