Skip to content

πŸŽ‰ unknownutil v4 #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c98025e
:boom: Remove deprecated features
lambdalisue Jul 30, 2024
8e0659d
:boom: Remove unstable features
lambdalisue Jul 31, 2024
a2886a0
:+1: Add `as.Optional` and `as.Unoptional`
lambdalisue Jul 31, 2024
6654077
:boom: Replace `OptionalOf/UnwrapOptinalOf` to `Optional/Unoptional`
lambdalisue Jul 31, 2024
0d2dbb3
:boom: Remove `metadata` module and use `_annotation` instead
lambdalisue Jul 31, 2024
d990e4d
:boom: Individually define predicate functions
lambdalisue Aug 1, 2024
f0b96b6
:coffee: Move bench/script files
lambdalisue Aug 1, 2024
7025a4f
:muscle: Individually define utility functions
lambdalisue Aug 1, 2024
69d7d45
:muscle: Rename `inspect.ts` to `_inspect.ts`
lambdalisue Aug 1, 2024
67ef8f9
:herb: Fix tests for `as/optional.ts`
lambdalisue Aug 1, 2024
f0392ca
:+1: Add `as.Readonly` and `as.Unreadonly`
lambdalisue Aug 1, 2024
9b1aa43
:+1: Add `isReadonlyOf`
lambdalisue Aug 1, 2024
161a015
:bug: Fix `asOptional` and `asReadonly` for type
lambdalisue Aug 1, 2024
26d9523
:herb: Improve tests of `isPartialOf` and `isRequiredOf`
lambdalisue Aug 1, 2024
82ebdb4
:memo: Update document comments
lambdalisue Aug 1, 2024
90bba5a
:coffee: Export individual modules for JSR
lambdalisue Aug 1, 2024
3153d27
:coffee: Rename `udd.yml` to `update.yml`
lambdalisue Aug 1, 2024
2223257
:coffee: Stop publishing to npm
lambdalisue Aug 1, 2024
68be2f1
:memo: Remove npm/deno.land badges
lambdalisue Aug 1, 2024
34770c0
:muscle: Improve docs/tests/types of `as` module
lambdalisue Aug 2, 2024
e902b0a
:muscle: Improve tests/docs/types of `is` module
lambdalisue Aug 2, 2024
ea8956e
:memo: Update usage
lambdalisue Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading