Skip to content

Commit 49eb62d

Browse files
committed
💪 Use unknownutil
1 parent 6e14e74 commit 49eb62d

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

denops_std/helper/expr_string.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
Dispatcher,
55
Meta,
66
} from "https://deno.land/x/denops_core@v5.0.0/mod.ts";
7+
import is from "https://deno.land/x/unknownutil@v3.10.0/is.ts";
78
import { execute } from "./execute.ts";
89
import { generateUniqueString } from "../util.ts";
910

@@ -29,7 +30,7 @@ type TemplateSubstitutions = any[];
2930
const cacheKey = "denops_std/helper/exprStr@1";
3031

3132
async function ensurePrerequisites(denops: Denops): Promise<string> {
32-
if (typeof denops.context[cacheKey] === "string") {
33+
if (is.String(denops.context[cacheKey])) {
3334
return denops.context[cacheKey];
3435
}
3536
const suffix = generateUniqueString();
@@ -69,6 +70,10 @@ export function exprQuote(
6970
});
7071
}
7172

73+
const isInstanceOfBoolean = is.InstanceOf(Boolean);
74+
const isInstanceOfNumber = is.InstanceOf(Number);
75+
const isInstanceOfString = is.InstanceOf(String);
76+
7277
/**
7378
* Returns `true` if the value is a string marked as Vim's string constant format.
7479
*
@@ -80,11 +85,17 @@ export function exprQuote(
8085
* ```
8186
*/
8287
export function isExprString(x: unknown): x is ExprString {
83-
return x instanceof String && (x as ExprString)[EXPR_STRING_MARK] === 1;
88+
return is.ObjectOf({
89+
[EXPR_STRING_MARK]: is.LiteralOf(1),
90+
})(x);
8491
}
8592

8693
function isJsonable(x: unknown): x is Jsonable {
87-
return x != null && typeof (x as Jsonable).toJSON === "function";
94+
return x != null && is.Function((x as Jsonable).toJSON);
95+
}
96+
97+
function isIgnoreRecordValue(x: unknown): boolean {
98+
return is.Undefined(x) || is.Function(x) || is.Symbol(x);
8899
}
89100

90101
/**
@@ -95,30 +106,33 @@ export function vimStringify(value: unknown, key?: string | number): string {
95106
return vimStringify(JSON.parse(value.toJSON(key)));
96107
}
97108
if (isExprString(value)) {
109+
// Return Vim's expr-string
98110
return `"${value.replaceAll('"', '\\"')}"`;
99111
}
100-
if (value == null || ["function", "symbol"].includes(typeof value)) {
112+
if ((is.Nullish(value) || is.Function(value) || is.Symbol(value))) {
101113
return "v:null";
102114
}
103-
if (typeof value === "boolean" || value instanceof Boolean) {
104-
return value == true ? "v:true" : "v:false";
115+
if (is.Boolean(value) || isInstanceOfBoolean(value)) {
116+
// Return v:true or v:false
117+
return `v:${value}`;
105118
}
106-
if (typeof value === "number" || value instanceof Number) {
119+
if (is.Number(value) || isInstanceOfNumber(value)) {
107120
// Replace `5e-10` to `5.0e-10`
108121
return `${value}`.replace(/^(\d+)e/, "$1.0e");
109122
}
110-
if (typeof value === "string" || value instanceof String) {
123+
if (is.String(value) || isInstanceOfString(value)) {
124+
// Returns Vim's literal-string
111125
return `'${value.replaceAll("'", "''")}'`;
112126
}
113-
if (Array.isArray(value)) {
127+
if (is.Array(value)) {
128+
// Returns Vim's list
114129
return `[${value.map(vimStringify).join(",")}]`;
115130
}
116-
if (typeof value === "object") {
131+
if (is.Record(value)) {
132+
// Returns Vim's dict
117133
return `{${
118134
Object.entries(value)
119-
.filter(([, value]) =>
120-
!["undefined", "function", "symbol"].includes(typeof value)
121-
)
135+
.filter(([, value]) => !isIgnoreRecordValue(value))
122136
.map(([key, value]) =>
123137
`'${key.replaceAll("'", "''")}':${vimStringify(value, key)}`
124138
)
@@ -130,7 +144,7 @@ export function vimStringify(value: unknown, key?: string | number): string {
130144
}
131145

132146
function trimEndOfArgs(args: unknown[]): unknown[] {
133-
const last = args.findIndex((v) => v === undefined);
147+
const last = args.findIndex(is.Undefined);
134148
return last < 0 ? args : args.slice(0, last);
135149
}
136150

0 commit comments

Comments
 (0)