@@ -4,6 +4,7 @@ import type {
4
4
Dispatcher ,
5
5
Meta ,
6
6
} 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" ;
7
8
import { execute } from "./execute.ts" ;
8
9
import { generateUniqueString } from "../util.ts" ;
9
10
@@ -29,7 +30,7 @@ type TemplateSubstitutions = any[];
29
30
const cacheKey = "denops_std/helper/exprStr@1" ;
30
31
31
32
async function ensurePrerequisites ( denops : Denops ) : Promise < string > {
32
- if ( typeof denops . context [ cacheKey ] === "string" ) {
33
+ if ( is . String ( denops . context [ cacheKey ] ) ) {
33
34
return denops . context [ cacheKey ] ;
34
35
}
35
36
const suffix = generateUniqueString ( ) ;
@@ -69,6 +70,10 @@ export function exprQuote(
69
70
} ) ;
70
71
}
71
72
73
+ const isInstanceOfBoolean = is . InstanceOf ( Boolean ) ;
74
+ const isInstanceOfNumber = is . InstanceOf ( Number ) ;
75
+ const isInstanceOfString = is . InstanceOf ( String ) ;
76
+
72
77
/**
73
78
* Returns `true` if the value is a string marked as Vim's string constant format.
74
79
*
@@ -80,11 +85,17 @@ export function exprQuote(
80
85
* ```
81
86
*/
82
87
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 ) ;
84
91
}
85
92
86
93
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 ) ;
88
99
}
89
100
90
101
/**
@@ -95,30 +106,33 @@ export function vimStringify(value: unknown, key?: string | number): string {
95
106
return vimStringify ( JSON . parse ( value . toJSON ( key ) ) ) ;
96
107
}
97
108
if ( isExprString ( value ) ) {
109
+ // Return Vim's expr-string
98
110
return `"${ value . replaceAll ( '"' , '\\"' ) } "` ;
99
111
}
100
- if ( value == null || [ "function" , "symbol" ] . includes ( typeof value ) ) {
112
+ if ( ( is . Nullish ( value ) || is . Function ( value ) || is . Symbol ( value ) ) ) {
101
113
return "v:null" ;
102
114
}
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 } ` ;
105
118
}
106
- if ( typeof value === "number" || value instanceof Number ) {
119
+ if ( is . Number ( value ) || isInstanceOfNumber ( value ) ) {
107
120
// Replace `5e-10` to `5.0e-10`
108
121
return `${ value } ` . replace ( / ^ ( \d + ) e / , "$1.0e" ) ;
109
122
}
110
- if ( typeof value === "string" || value instanceof String ) {
123
+ if ( is . String ( value ) || isInstanceOfString ( value ) ) {
124
+ // Returns Vim's literal-string
111
125
return `'${ value . replaceAll ( "'" , "''" ) } '` ;
112
126
}
113
- if ( Array . isArray ( value ) ) {
127
+ if ( is . Array ( value ) ) {
128
+ // Returns Vim's list
114
129
return `[${ value . map ( vimStringify ) . join ( "," ) } ]` ;
115
130
}
116
- if ( typeof value === "object" ) {
131
+ if ( is . Record ( value ) ) {
132
+ // Returns Vim's dict
117
133
return `{${
118
134
Object . entries ( value )
119
- . filter ( ( [ , value ] ) =>
120
- ! [ "undefined" , "function" , "symbol" ] . includes ( typeof value )
121
- )
135
+ . filter ( ( [ , value ] ) => ! isIgnoreRecordValue ( value ) )
122
136
. map ( ( [ key , value ] ) =>
123
137
`'${ key . replaceAll ( "'" , "''" ) } ':${ vimStringify ( value , key ) } `
124
138
)
@@ -130,7 +144,7 @@ export function vimStringify(value: unknown, key?: string | number): string {
130
144
}
131
145
132
146
function trimEndOfArgs ( args : unknown [ ] ) : unknown [ ] {
133
- const last = args . findIndex ( ( v ) => v === undefined ) ;
147
+ const last = args . findIndex ( is . Undefined ) ;
134
148
return last < 0 ? args : args . slice ( 0 , last ) ;
135
149
}
136
150
0 commit comments