Skip to content

Commit 127dca5

Browse files
authored
Merge pull request #258 from vim-denops/cache-predcates
2 parents b2e9943 + ae8f712 commit 127dca5

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

function/types.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { Predicate } from "@core/unknownutil/type";
12
import { assert } from "@core/unknownutil/assert";
3+
import { asOptional } from "@core/unknownutil/as/optional";
24
import { isNumber } from "@core/unknownutil/is/number";
35
import { isObjectOf } from "@core/unknownutil/is/object-of";
4-
import { isTupleOf } from "@core/unknownutil/is/tuple-of";
5-
import { isUnionOf } from "@core/unknownutil/is/union-of";
6+
import { isParametersOf } from "@core/unknownutil/is/parameters-of";
67

78
/**
89
* Type of `screenpos()` result.
@@ -17,15 +18,12 @@ export type ScreenPos = {
1718
/**
1819
* Return true if the value is ScreenPos.
1920
*/
20-
export function isScreenPos(x: unknown): x is ScreenPos {
21-
const predObj = {
22-
row: isNumber,
23-
col: isNumber,
24-
endcol: isNumber,
25-
curscol: isNumber,
26-
};
27-
return isObjectOf(predObj)(x);
28-
}
21+
export const isScreenPos: Predicate<ScreenPos> = isObjectOf({
22+
row: isNumber,
23+
col: isNumber,
24+
endcol: isNumber,
25+
curscol: isNumber,
26+
});
2927

3028
/**
3129
* Assert if `x` is ScreenPos by raising an `AssertError` when it's not.
@@ -48,13 +46,9 @@ export type Position = [
4846
/**
4947
* Return true if the value is Position.
5048
*/
51-
export function isPosition(x: unknown): x is Position {
52-
const pred = isUnionOf([
53-
isTupleOf([isNumber, isNumber, isNumber, isNumber]),
54-
isTupleOf([isNumber, isNumber, isNumber, isNumber, isNumber]),
55-
]);
56-
return pred(x);
57-
}
49+
export const isPosition: Predicate<Position> = isParametersOf(
50+
[isNumber, isNumber, isNumber, isNumber, asOptional(isNumber)] as const,
51+
);
5852

5953
/**
6054
* Assert if `x` is Position by raising an `AssertError` when it's not.

helper/expr_string.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @module
2626
*/
2727
import type { Context, Denops, Dispatcher, Meta } from "@denops/core";
28+
import type { Predicate } from "@core/unknownutil/type";
2829
import { isArray } from "@core/unknownutil/is/array";
2930
import { isBoolean } from "@core/unknownutil/is/boolean";
3031
import { isFunction } from "@core/unknownutil/is/function";
@@ -120,11 +121,10 @@ const isInstanceOfString = isInstanceOf(String);
120121
* console.log(isExprString("foo")); // outputs: false
121122
* ```
122123
*/
123-
export function isExprString(x: unknown): x is ExprString {
124-
return isObjectOf({
125-
[EXPR_STRING_MARK]: isLiteralOf(1),
126-
})(x);
127-
}
124+
export const isExprString: Predicate<ExprString> = isObjectOf({
125+
// NOTE: `ExprString` has a different type in definition (primitive `string`) and implementation (`String`). Only checks `EXPR_STRING_MARK` existence.
126+
[EXPR_STRING_MARK]: isLiteralOf(1),
127+
}) as unknown as Predicate<ExprString>;
128128

129129
function isJsonable(x: unknown): x is Jsonable {
130130
return x != null && isFunction((x as Jsonable).toJSON);

0 commit comments

Comments
 (0)