Skip to content

Commit 5c2fc0a

Browse files
authored
Merge pull request #11 from lambdalisue/require
Add `require` that returns the value as-is
2 parents 63d4425 + db6c09a commit 5c2fc0a

File tree

4 files changed

+519
-7
lines changed

4 files changed

+519
-7
lines changed

README.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ The `unknownutil` provides the following ensure functions which will raise
9595
`EnsureError` when a given `x` is not expected type.
9696

9797
- `ensureString(x: unknown): assert x is string`
98-
- `ensureNumber(x: unknown): assert x is string`
98+
- `ensureNumber(x: unknown): assert x is number`
9999
- `ensureBoolean(x: unknown): assert x is boolean`
100100
- `ensureArray<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is T[]`
101-
- `ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x ensure Record<string, T>`
102-
- `ensureFunction(x: unknown): x ensure (...args: unknown[]) => unknown`
103-
- `ensureNull(x: unknown): x ensure null`
104-
- `ensureUndefined(x: unknown): x ensure undefined`
105-
- `ensureNone(x: unknown): x ensure null | undefined`
101+
- `ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x is Record<string, T>`
102+
- `ensureFunction(x: unknown): x is (...args: unknown[]) => unknown`
103+
- `ensureNull(x: unknown): x is null`
104+
- `ensureUndefined(x: unknown): x is undefined`
105+
- `ensureNone(x: unknown): x is null | undefined`
106106

107107
For example:
108108

@@ -145,6 +145,62 @@ ensureLike({}, b); // Now 'b' is 'Record<string, unknown>'
145145
ensureLike({ foo: "", bar: 0 }, b); // Now 'b' is '{foo: string, bar: number}'
146146
```
147147

148+
### requireXXXXX
149+
150+
The `unknownutil` provides the following require functions which returns a given
151+
`x` as is or raise `EnsureError` when that is not expected type.
152+
153+
- `requireString(x: unknown): string`
154+
- `requireNumber(x: unknown): number`
155+
- `requireBoolean(x: unknown): boolean`
156+
- `requireArray<T extends unknown>(x: unknown, pred?: Predicate<T>): T[]`
157+
- `requireObject<T extends unknown>(x: unknown, pred?: Predicate<T>): Record<string, T>`
158+
- `requireFunction(x: unknown): (...args: unknown[]) => unknown`
159+
- `requireNull(x: unknown): null`
160+
- `requireUndefined(x: unknown): undefined`
161+
- `requireNone(x: unknown): null | undefined`
162+
163+
For example:
164+
165+
```typescript
166+
import { requireString } from "https://deno.land/x/unknownutil/mod.ts";
167+
168+
const a: unknown = "Hello";
169+
const a1 = requireString(a); // Now 'a' and 'a1' is 'string'
170+
171+
const b: unknown = 0;
172+
const b1 = requireString(b); // Raise EnsureError on above while 'b' is not string
173+
```
174+
175+
Additionally, `requireArray` and `requireObject` supports an inner predicate
176+
function to predicate `x` more precisely like:
177+
178+
```typescript
179+
import { isString, requireArray } from "https://deno.land/x/unknownutil/mod.ts";
180+
181+
const a: unknown = ["a", "b", "c"];
182+
const a1 = requireArray(a); // Now 'a' and 'a1' is 'unknown[]'
183+
const a2 = requireArray(a, isString); // Now 'a' and 'a2' is 'string[]'
184+
185+
const b: unknown = [0, 1, 2];
186+
const b1 = requireArray(b); // Now 'b' and 'b1' is 'unknown[]'
187+
const b2 = requireArray(b, isString); // Raise EnsureError on above while 'b' is not string array
188+
```
189+
190+
Use `requireLike` if you need some complicated types like tuple or struct like:
191+
192+
```typescript
193+
import { requireLike } from "https://deno.land/x/unknownutil/mod.ts";
194+
195+
const a: unknown = ["a", "b", "c"];
196+
const a1 = requireLike([], a); // Now 'a' and 'a1' is 'unknown[]'
197+
const a2 = requireLike(["", "", ""], a); // Now 'a' and 'a2' is '[string, string, string]'
198+
199+
const b: unknown = { foo: "foo", bar: 0 };
200+
const b1 = requireLike({}, b); // Now 'b' and 'b1' is 'Record<string, unknown>'
201+
const b2 = requireLike({ foo: "", bar: 0 }, b); // Now 'b' and 'b2' is '{foo: string, bar: number}'
202+
```
203+
148204
## Development
149205

150206
Lint code like:

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from "./is.ts";
21
export * from "./ensure.ts";
2+
export * from "./is.ts";
3+
export * from "./require.ts";

require.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { Predicate } from "./is.ts";
2+
import {
3+
ensure,
4+
ensureArray,
5+
ensureBoolean,
6+
ensureFunction,
7+
ensureLike,
8+
ensureNone,
9+
ensureNull,
10+
ensureNumber,
11+
ensureObject,
12+
ensureString,
13+
ensureUndefined,
14+
} from "./ensure.ts";
15+
16+
/**
17+
* Return `x` as-is if `x` is expected type or raise an `EnsureError` when it's not.
18+
*/
19+
export function require<T>(
20+
x: unknown,
21+
pred: Predicate<T>,
22+
message = "The value is not expected type",
23+
): T {
24+
ensure(x, pred, message);
25+
return x;
26+
}
27+
28+
/**
29+
* Return `x` as-is if `x` is string or raise an `EnsureError` when it's not.
30+
*/
31+
export function requireString(x: unknown): string {
32+
ensureString(x);
33+
return x;
34+
}
35+
36+
/**
37+
* Return `x` as-is if `x` is number or raise an `EnsureError` when it's not.
38+
*/
39+
export function requireNumber(x: unknown): number {
40+
ensureNumber(x);
41+
return x;
42+
}
43+
44+
/**
45+
* Return `x` as-is if `x` is boolean or raise an `EnsureError` when it's not.
46+
*/
47+
export function requireBoolean(x: unknown): boolean {
48+
ensureBoolean(x);
49+
return x;
50+
}
51+
52+
/**
53+
* Return `x` as-is if `x` is array or raise an `EnsureError` when it's not.
54+
*/
55+
export function requireArray<T extends unknown>(
56+
x: unknown,
57+
ipred?: Predicate<T>,
58+
): T[] {
59+
ensureArray(x, ipred);
60+
return x;
61+
}
62+
63+
/**
64+
* Return `x` as-is if `x` is object or raise an `EnsureError` when it's not.
65+
*/
66+
export function requireObject<T>(
67+
x: unknown,
68+
ipred?: Predicate<T>,
69+
): Record<string, T> {
70+
ensureObject(x, ipred);
71+
return x;
72+
}
73+
74+
/**
75+
* Return `x` as-is if `x` is function or raise an `EnsureError` when it's not.
76+
*/
77+
export function requireFunction(
78+
x: unknown,
79+
): (...args: unknown[]) => unknown {
80+
ensureFunction(x);
81+
return x;
82+
}
83+
84+
/**
85+
* Return `x` as-is if `x` is null or raise an `EnsureError` when it's not.
86+
*/
87+
export function requireNull(x: unknown): null {
88+
ensureNull(x);
89+
return x;
90+
}
91+
92+
/**
93+
* Return `x` as-is if `x` is undefined or raise an `EnsureError` when it's not.
94+
*/
95+
export function requireUndefined(x: unknown): undefined {
96+
ensureUndefined(x);
97+
return x;
98+
}
99+
100+
/**
101+
* Return `x` as-is if `x` is null or undefined or raise an `EnsureError` when it's not.
102+
*/
103+
export function requireNone(x: unknown): null | undefined {
104+
ensureNone(x);
105+
return x;
106+
}
107+
108+
/**
109+
* Return `x` as-is if `x` follows the reference or raise an `EnsureError` when it doesn't.
110+
*/
111+
export function requireLike<R, T extends unknown>(
112+
ref: R,
113+
x: unknown,
114+
ipred?: Predicate<T>,
115+
): R {
116+
ensureLike(ref, x, ipred);
117+
return x;
118+
}

0 commit comments

Comments
 (0)