Skip to content

Commit ef32620

Browse files
committed
📦 Upgrade unknownutil to v3
1 parent fe34d13 commit ef32620

File tree

6 files changed

+44
-56
lines changed

6 files changed

+44
-56
lines changed

denops_std/buffer/fileformat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as unknownutil from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
1+
import { is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
22

33
export type FileFormat = "unix" | "dos" | "mac";
44

@@ -9,7 +9,7 @@ const fileFormatDelimiters = {
99
};
1010

1111
export function isFileFormat(v: unknown): v is FileFormat {
12-
return unknownutil.isString(v) && v in fileFormatDelimiters;
12+
return is.String(v) && v in fileFormatDelimiters;
1313
}
1414

1515
export function assertFileFormat(v: unknown): asserts v is FileFormat {

denops_std/function/buffer_test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { assertEquals } from "https://deno.land/std@0.192.0/testing/asserts.ts";
2-
import {
3-
assertArray,
4-
isObject,
5-
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
2+
import { assert, is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
63
import { test } from "https://deno.land/x/denops_test@v1.4.0/mod.ts";
74
import { BufInfo } from "./types.ts";
85
import * as buffer from "./buffer.ts";
@@ -23,7 +20,7 @@ test({
2320
await denops.cmd("bunload!");
2421

2522
const actual = await buffer.getbufinfo(denops);
26-
assertArray(actual, (x): x is BufInfo => isObject(x));
23+
assert(actual, is.ArrayOf((x): x is BufInfo => is.Record(x)));
2724
assertEquals(actual.length, 4);
2825
assertEquals(
2926
actual.map(({ changed, hidden, listed, loaded }) => (

denops_std/function/cursor_test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "https://deno.land/std@0.192.0/testing/asserts.ts";
2-
import { assertNumber } from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
2+
import { assert, is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
33
import { test } from "https://deno.land/x/denops_test@v1.4.0/mod.ts";
44
import * as cursor from "./cursor.ts";
55
import { assertPosition, assertScreenPos, isScreenPos } from "./types.ts";
@@ -15,7 +15,7 @@ test({
1515
await denops.call("setline", 1, "abcdef");
1616
await denops.cmd("norm gg4|mx6|mY2|");
1717

18-
assertNumber(await cursor.col(denops, "."));
18+
assert(await cursor.col(denops, "."), is.Number);
1919
assertEquals(await cursor.col(denops, "."), 2);
2020
assertEquals(await cursor.col(denops, [1, 2]), 2);
2121
assertEquals(await cursor.col(denops, [1, "$"]), 7);
@@ -28,7 +28,7 @@ test({
2828
await denops.cmd("enew!");
2929
await denops.call("setline", 1, "\tab");
3030

31-
assertNumber(await cursor.virtcol(denops, "."));
31+
assert(await cursor.virtcol(denops, "."), is.Number);
3232
assertEquals(await cursor.virtcol(denops, "."), 8);
3333
assertEquals(await cursor.virtcol(denops, [1, 1]), 8);
3434
assertEquals(await cursor.virtcol(denops, [1, "$"]), 11);
@@ -45,10 +45,10 @@ test({
4545
const winid = await denops.call("bufwinid", "%") as number;
4646
await denops.call("setline", 1, ["a", "b", "c"]);
4747

48-
assertNumber(await cursor.line(denops, "."));
48+
assert(await cursor.line(denops, "."), is.Number);
4949
assertEquals(await cursor.line(denops, "."), 1);
5050
assertEquals(await cursor.line(denops, "$"), 3);
51-
assertNumber(await cursor.line(denops, ".", winid));
51+
assert(await cursor.line(denops, ".", winid), is.Number);
5252
assertEquals(await cursor.line(denops, ".", winid), 1);
5353
},
5454
});
@@ -57,26 +57,26 @@ test({
5757
name: "wincol()",
5858
fn: async () => {
5959
await denops.cmd("enew!");
60-
assertNumber(await cursor.wincol(denops));
60+
assert(await cursor.wincol(denops), is.Number);
6161
},
6262
});
6363

6464
await t.step({
6565
name: "winline()",
6666
fn: async () => {
6767
await denops.cmd("enew!");
68-
assertNumber(await cursor.winline(denops));
68+
assert(await cursor.winline(denops), is.Number);
6969
},
7070
});
7171

7272
await t.step({
7373
name: "cursor()",
7474
fn: async () => {
7575
await denops.cmd("enew!");
76-
assertNumber(await cursor.cursor(denops, 1, 2));
77-
assertNumber(await cursor.cursor(denops, 1, 2, 3));
78-
assertNumber(await cursor.cursor(denops, [1, 2, 3]));
79-
assertNumber(await cursor.cursor(denops, [1, 2, 3, 4]));
76+
assert(await cursor.cursor(denops, 1, 2), is.Number);
77+
assert(await cursor.cursor(denops, 1, 2, 3), is.Number);
78+
assert(await cursor.cursor(denops, [1, 2, 3]), is.Number);
79+
assert(await cursor.cursor(denops, [1, 2, 3, 4]), is.Number);
8080
},
8181
});
8282

@@ -123,11 +123,13 @@ test({
123123
name: "setpos",
124124
fn: async () => {
125125
await denops.cmd("enew!");
126-
assertNumber(
126+
assert(
127127
await cursor.setpos(denops, "'b", await cursor.getpos(denops, "'a")),
128+
is.Number,
128129
);
129-
assertNumber(
130+
assert(
130131
await cursor.setpos(denops, "'b", await cursor.getcurpos(denops)),
132+
is.Number,
131133
);
132134
},
133135
});
@@ -136,24 +138,24 @@ test({
136138
name: "byte2line",
137139
fn: async () => {
138140
await denops.cmd("enew!");
139-
assertNumber(await cursor.byte2line(denops, 1));
141+
assert(await cursor.byte2line(denops, 1), is.Number);
140142
},
141143
});
142144

143145
await t.step({
144146
name: "line2byte",
145147
fn: async () => {
146148
await denops.cmd("enew!");
147-
assertNumber(await cursor.line2byte(denops, 1));
149+
assert(await cursor.line2byte(denops, 1), is.Number);
148150
},
149151
});
150152

151153
await t.step({
152154
name: "diff_filler()",
153155
fn: async () => {
154156
await denops.cmd("enew!");
155-
assertNumber(await cursor.diff_filler(denops, 1));
156-
assertNumber(await cursor.diff_filler(denops, "."));
157+
assert(await cursor.diff_filler(denops, 1), is.Number);
158+
assert(await cursor.diff_filler(denops, "."), is.Number);
157159
},
158160
});
159161
},

denops_std/function/types.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
AssertError,
3-
isLike,
4-
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
1+
import { assert, is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
52

63
/**
74
* Type of `screenpos()` result.
@@ -13,30 +10,24 @@ export type ScreenPos = {
1310
curscol: number;
1411
};
1512

16-
/**
17-
* Reference ScreenPos struct for isLike.
18-
*/
19-
const refScreenPos: ScreenPos = {
20-
row: 0,
21-
col: 0,
22-
endcol: 0,
23-
curscol: 0,
24-
};
25-
2613
/**
2714
* Return true if the value is ScreenPos.
2815
*/
2916
export function isScreenPos(x: unknown): x is ScreenPos {
30-
return isLike(refScreenPos, x);
17+
const predObj = {
18+
row: is.Number,
19+
col: is.Number,
20+
endcol: is.Number,
21+
curscol: is.Number,
22+
};
23+
return is.ObjectOf(predObj)(x);
3124
}
3225

3326
/**
3427
* Assert if `x` is ScreenPos by raising an `AssertError` when it's not.
3528
*/
3629
export function assertScreenPos(x: unknown): asserts x is ScreenPos {
37-
if (!isScreenPos(x)) {
38-
throw new AssertError("The value must be ScreenPos");
39-
}
30+
assert(x, isScreenPos, { message: "The value must be ScreenPos" });
4031
}
4132

4233
/**
@@ -54,17 +45,18 @@ export type Position = [
5445
* Return true if the value is Position.
5546
*/
5647
export function isPosition(x: unknown): x is Position {
57-
return Array.isArray(x) && (x.length === 4 || x.length === 5) &&
58-
x.every((x) => typeof x === "number");
48+
const pred = is.OneOf([
49+
is.TupleOf([is.Number, is.Number, is.Number, is.Number]),
50+
is.TupleOf([is.Number, is.Number, is.Number, is.Number, is.Number]),
51+
]);
52+
return pred(x);
5953
}
6054

6155
/**
6256
* Assert if `x` is Position by raising an `AssertError` when it's not.
6357
*/
6458
export function assertPosition(x: unknown): asserts x is Position {
65-
if (!isPosition(x)) {
66-
throw new AssertError("The value must be Position");
67-
}
59+
assert(x, isPosition, { message: "The value must be Position" });
6860
}
6961

7062
const validBuiltinCompletions = [

denops_std/helper/input.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v5.0.0/mod.ts";
2-
import {
3-
assertNumber,
4-
assertString,
5-
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
2+
import { assert, is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
63
import * as fn from "../function/mod.ts";
74
import * as lambda from "../lambda/mod.ts";
85
import { execute } from "./execute.ts";
@@ -214,9 +211,9 @@ export async function input(
214211
const completion = options.completion ?? null;
215212
if (completion && typeof completion !== "string") {
216213
const id = lambda.register(denops, async (arglead, cmdline, cursorpos) => {
217-
assertString(arglead);
218-
assertString(cmdline);
219-
assertNumber(cursorpos);
214+
assert(arglead, is.String);
215+
assert(cmdline, is.String);
216+
assert(cursorpos, is.Number);
220217
return await completion(arglead, cmdline, cursorpos);
221218
});
222219
try {

denops_std/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
* import * as vars from "./variable/mod.ts";
1313
* import * as helper from "./helper/mod.ts";
1414
*
15-
* import * as unknownutil from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
15+
* import { assert, is } from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^";
1616
*
1717
* export async function main(denops: Denops): Promise<void> {
1818
* denops.dispatcher = {
1919
* async say(where: unknown): Promise<void> {
20-
* unknownutil.assertString(where);
20+
* assert(where, is.String);
2121
*
2222
* const name = await fn.input(denops, "Your name: ");
2323
* const progname = await vars.v.get(denops, "progname");

0 commit comments

Comments
 (0)