Skip to content

Commit 63bf53c

Browse files
committed
Rename require to assume
Because `require` is registered word in node-js
1 parent 1797484 commit 63bf53c

File tree

5 files changed

+384
-377
lines changed

5 files changed

+384
-377
lines changed

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,60 +145,60 @@ 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
148+
### assumeXXXXX
149149

150-
The `unknownutil` provides the following require functions which returns a given
150+
The `unknownutil` provides the following assume functions which returns a given
151151
`x` as is or raise `EnsureError` when that is not expected type.
152152

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`
153+
- `assumeString(x: unknown): string`
154+
- `assumeNumber(x: unknown): number`
155+
- `assumeBoolean(x: unknown): boolean`
156+
- `assumeArray<T extends unknown>(x: unknown, pred?: Predicate<T>): T[]`
157+
- `assumeObject<T extends unknown>(x: unknown, pred?: Predicate<T>): Record<string, T>`
158+
- `assumeFunction(x: unknown): (...args: unknown[]) => unknown`
159+
- `assumeNull(x: unknown): null`
160+
- `assumeUndefined(x: unknown): undefined`
161+
- `assumeNone(x: unknown): null | undefined`
162162

163163
For example:
164164

165165
```typescript
166-
import { requireString } from "https://deno.land/x/unknownutil/mod.ts";
166+
import { assumeString } from "https://deno.land/x/unknownutil/mod.ts";
167167

168168
const a: unknown = "Hello";
169-
const a1 = requireString(a); // Now 'a' and 'a1' is 'string'
169+
const a1 = assumeString(a); // Now 'a' and 'a1' is 'string'
170170

171171
const b: unknown = 0;
172-
const b1 = requireString(b); // Raise EnsureError on above while 'b' is not string
172+
const b1 = assumeString(b); // Raise EnsureError on above while 'b' is not string
173173
```
174174

175-
Additionally, `requireArray` and `requireObject` supports an inner predicate
175+
Additionally, `assumeArray` and `assumeObject` supports an inner predicate
176176
function to predicate `x` more precisely like:
177177

178178
```typescript
179-
import { isString, requireArray } from "https://deno.land/x/unknownutil/mod.ts";
179+
import { assumeArray, isString } from "https://deno.land/x/unknownutil/mod.ts";
180180

181181
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[]'
182+
const a1 = assumeArray(a); // Now 'a' and 'a1' is 'unknown[]'
183+
const a2 = assumeArray(a, isString); // Now 'a' and 'a2' is 'string[]'
184184

185185
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
186+
const b1 = assumeArray(b); // Now 'b' and 'b1' is 'unknown[]'
187+
const b2 = assumeArray(b, isString); // Raise EnsureError on above while 'b' is not string array
188188
```
189189

190-
Use `requireLike` if you need some complicated types like tuple or struct like:
190+
Use `assumeLike` if you need some complicated types like tuple or struct like:
191191

192192
```typescript
193-
import { requireLike } from "https://deno.land/x/unknownutil/mod.ts";
193+
import { assumeLike } from "https://deno.land/x/unknownutil/mod.ts";
194194

195195
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]'
196+
const a1 = assumeLike([], a); // Now 'a' and 'a1' is 'unknown[]'
197+
const a2 = assumeLike(["", "", ""], a); // Now 'a' and 'a2' is '[string, string, string]'
198198

199199
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}'
200+
const b1 = assumeLike({}, b); // Now 'b' and 'b1' is 'Record<string, unknown>'
201+
const b2 = assumeLike({ foo: "", bar: 0 }, b); // Now 'b' and 'b2' is '{foo: string, bar: number}'
202202
```
203203

204204
## Development

require.ts renamed to assume.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
/**
1717
* Return `x` as-is if `x` is expected type or raise an `EnsureError` when it's not.
1818
*/
19-
export function require<T>(
19+
export function assume<T>(
2020
x: unknown,
2121
pred: Predicate<T>,
2222
message = "The value is not expected type",
@@ -28,31 +28,31 @@ export function require<T>(
2828
/**
2929
* Return `x` as-is if `x` is string or raise an `EnsureError` when it's not.
3030
*/
31-
export function requireString(x: unknown): string {
31+
export function assumeString(x: unknown): string {
3232
ensureString(x);
3333
return x;
3434
}
3535

3636
/**
3737
* Return `x` as-is if `x` is number or raise an `EnsureError` when it's not.
3838
*/
39-
export function requireNumber(x: unknown): number {
39+
export function assumeNumber(x: unknown): number {
4040
ensureNumber(x);
4141
return x;
4242
}
4343

4444
/**
4545
* Return `x` as-is if `x` is boolean or raise an `EnsureError` when it's not.
4646
*/
47-
export function requireBoolean(x: unknown): boolean {
47+
export function assumeBoolean(x: unknown): boolean {
4848
ensureBoolean(x);
4949
return x;
5050
}
5151

5252
/**
5353
* Return `x` as-is if `x` is array or raise an `EnsureError` when it's not.
5454
*/
55-
export function requireArray<T extends unknown>(
55+
export function assumeArray<T extends unknown>(
5656
x: unknown,
5757
ipred?: Predicate<T>,
5858
): T[] {
@@ -63,7 +63,7 @@ export function requireArray<T extends unknown>(
6363
/**
6464
* Return `x` as-is if `x` is object or raise an `EnsureError` when it's not.
6565
*/
66-
export function requireObject<T>(
66+
export function assumeObject<T>(
6767
x: unknown,
6868
ipred?: Predicate<T>,
6969
): Record<string, T> {
@@ -74,41 +74,39 @@ export function requireObject<T>(
7474
/**
7575
* Return `x` as-is if `x` is function or raise an `EnsureError` when it's not.
7676
*/
77-
export function requireFunction(
78-
x: unknown,
79-
): (...args: unknown[]) => unknown {
77+
export function assumeFunction(x: unknown): (...args: unknown[]) => unknown {
8078
ensureFunction(x);
8179
return x;
8280
}
8381

8482
/**
8583
* Return `x` as-is if `x` is null or raise an `EnsureError` when it's not.
8684
*/
87-
export function requireNull(x: unknown): null {
85+
export function assumeNull(x: unknown): null {
8886
ensureNull(x);
8987
return x;
9088
}
9189

9290
/**
9391
* Return `x` as-is if `x` is undefined or raise an `EnsureError` when it's not.
9492
*/
95-
export function requireUndefined(x: unknown): undefined {
93+
export function assumeUndefined(x: unknown): undefined {
9694
ensureUndefined(x);
9795
return x;
9896
}
9997

10098
/**
10199
* Return `x` as-is if `x` is null or undefined or raise an `EnsureError` when it's not.
102100
*/
103-
export function requireNone(x: unknown): null | undefined {
101+
export function assumeNone(x: unknown): null | undefined {
104102
ensureNone(x);
105103
return x;
106104
}
107105

108106
/**
109107
* Return `x` as-is if `x` follows the reference or raise an `EnsureError` when it doesn't.
110108
*/
111-
export function requireLike<R, T extends unknown>(
109+
export function assumeLike<R, T extends unknown>(
112110
ref: R,
113111
x: unknown,
114112
ipred?: Predicate<T>,

0 commit comments

Comments
 (0)