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
910A utility pack for handling ` unknown ` type.
1011
@@ -15,14 +16,14 @@ A utility pack for handling `unknown` type.
1516It provides ` is ` module for type predicate functions and ` assert ` , ` ensure ` , and
1617` maybe ` helper functions.
1718
18- ### is*
19+ ### is\ *
1920
2021Type predicate function is a function which returns ` true ` if a given value is
2122expected type. For example, ` isString ` (or ` is.String ` ) returns ` true ` if a
2223given 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
2728const a: unknown = " Hello" ;
2829if (is .String (a )) {
@@ -34,29 +35,31 @@ Additionally, `is*Of` (or `is.*Of`) functions return type predicate functions to
3435predicate 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
3943const 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
5157type Article = PredicateType <typeof isArticle >;
5258
5359const 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};
6164if (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,
7982it 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
8490const a: unknown = " Hello" ;
8591
@@ -97,7 +103,10 @@ The `ensure` function return the value as-is if a given value is expected type.
97103Otherwise, 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
102111const a: unknown = " Hello" ;
103112
@@ -116,14 +125,35 @@ Otherwise, it returns `undefined` that suites with
116125like:
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
121133const a: unknown = " Hello" ;
122134
123135// `maybe` returns `string | undefined` so it suites with `??`
124136const _: 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
129159See [ GitHub Wiki] ( https://github.com/lambdalisue/deno-unknownutil/wiki ) for
0 commit comments