xshell 0.0.12 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chalk.browser.ts +41 -0
- package/file.d.ts +15 -0
- package/file.js +8 -1
- package/file.js.map +1 -1
- package/myfont.sass +11 -0
- package/net.browser.ts +141 -0
- package/package.json +6 -2
- package/prototype.browser.ts +728 -0
- package/repl.js +4 -0
- package/repl.js.map +1 -1
- package/scroll-bar.sass +35 -0
- package/server.d.ts +16 -3
- package/server.js +122 -2
- package/server.js.map +1 -1
- package/toaster.browser.ts +50 -0
- package/toaster.d.ts +9 -0
- package/toaster.sass +22 -0
- package/tsconfig.json +5 -0
- package/ufs.d.ts +21 -0
- package/ufs.js +153 -0
- package/ufs.js.map +1 -0
- package/utils.browser.ts +25 -0
- package/utils.d.ts +1 -1
|
@@ -0,0 +1,728 @@
|
|
|
1
|
+
/** 在浏览器端修改 prototype,需要更加小心 */
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface String {
|
|
5
|
+
readonly width: number
|
|
6
|
+
|
|
7
|
+
// --- 工具方法
|
|
8
|
+
/** 截取字符串不超过 width 显示宽度的部分,并保留颜色
|
|
9
|
+
找到并记录能容纳 字符串 + … 的最后一个字符的位置 i_fitted
|
|
10
|
+
若完整的字符串长度超过 width,返回 slice(0, i_fitted + 1) + …
|
|
11
|
+
否则 返回 this
|
|
12
|
+
*/
|
|
13
|
+
truncate (this: string, width: number): string
|
|
14
|
+
|
|
15
|
+
pad (this: string, width: number, { character, position }?: { character?: string, position?: 'left' | 'right'}): string
|
|
16
|
+
|
|
17
|
+
limit (this: string, width: number, { character, position }?: { character?: string, position?: 'left' | 'right'}): string
|
|
18
|
+
|
|
19
|
+
to_regx (this: string, preservations?: string, flags?: string): RegExp
|
|
20
|
+
|
|
21
|
+
/** ```ts
|
|
22
|
+
'g:/acgn/海贼王/[Skytree][海贼王][One_Piece][893][GB_BIG5_JP][X264_AAC][1080P][CRRIP][天空树双语字幕组].mkv'.refmt(
|
|
23
|
+
'{dirp}/[Skytree][海贼王][{ en_name: \\w+ }][{ episode: \\d+ }][GB_BIG5_JP][{encoding}_AAC][1080P][CRRIP][天空树双语字幕组].{format}',
|
|
24
|
+
'g:/acgn/海贼王/{episode} {encoding}.{format}',
|
|
25
|
+
'\\+',
|
|
26
|
+
'i',
|
|
27
|
+
(name, value) => name === 'episode' ? String(+value + 1) : value.toLowerCase()
|
|
28
|
+
)
|
|
29
|
+
```
|
|
30
|
+
*/
|
|
31
|
+
refmt ( this: string,
|
|
32
|
+
pattern: string,
|
|
33
|
+
|
|
34
|
+
pattern_: string,
|
|
35
|
+
|
|
36
|
+
/** `''` 保留的正则表达式字符 */
|
|
37
|
+
preservations?: string,
|
|
38
|
+
|
|
39
|
+
/** `''` 正则匹配选项 */
|
|
40
|
+
flags?: string,
|
|
41
|
+
|
|
42
|
+
/** `(name, matched) => matched || ''` placeholder transformer */
|
|
43
|
+
transformer?: (name: string, value: string, placeholders: { [name: string]: string }) => string,
|
|
44
|
+
|
|
45
|
+
/** `/\{.*?\}/g` */
|
|
46
|
+
pattern_placeholder?: RegExp
|
|
47
|
+
|
|
48
|
+
): string
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/** 字符串模式搜索
|
|
52
|
+
```ts
|
|
53
|
+
'git+https://github.com/tamino-martinius/node-ts-dedent-123.git'.find(
|
|
54
|
+
'^{protocol:[\\w+]+}://{hostname:[\\w\\.]+}/{username}/{project}-{index:\\d+}.{suffix}', '^', 'i'
|
|
55
|
+
)
|
|
56
|
+
{
|
|
57
|
+
protocol: 'git+https',
|
|
58
|
+
hostname: 'github.com',
|
|
59
|
+
...
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
- preservations?: `''` 保留的正则表达式字符
|
|
64
|
+
- flags?: `''` 正则匹配选项
|
|
65
|
+
- pattern_placeholder?: `/\{.*?\}/g`
|
|
66
|
+
*/
|
|
67
|
+
find (this: string,
|
|
68
|
+
|
|
69
|
+
pattern: string,
|
|
70
|
+
|
|
71
|
+
preservations?: string,
|
|
72
|
+
|
|
73
|
+
flags?: string,
|
|
74
|
+
|
|
75
|
+
pattern_placeholder?: RegExp
|
|
76
|
+
|
|
77
|
+
): { [name: string]: string }
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/** - type?: `'single'` */
|
|
81
|
+
quote (this: string, type?: keyof typeof quotes | 'psh'): string
|
|
82
|
+
|
|
83
|
+
/** - shape?: `'parenthesis'` */
|
|
84
|
+
bracket (this: string, shape?: keyof typeof brackets): string
|
|
85
|
+
|
|
86
|
+
surround (this: string, left: string, right?: string): string
|
|
87
|
+
|
|
88
|
+
surround_tag (this: string, tag_name: string): string
|
|
89
|
+
|
|
90
|
+
to_lf (this: string): string
|
|
91
|
+
|
|
92
|
+
to_crlf (this: string): string
|
|
93
|
+
|
|
94
|
+
/** 'xxx'.replace(/pattern/g, '')
|
|
95
|
+
如果 pattern 是 string 则在创建 RegExp 时自动加上 flags (默认 'g'), 否则忽略 flags
|
|
96
|
+
*/
|
|
97
|
+
rm (this: string, pattern: string | RegExp, flags?: string): string
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
// --- 文本处理
|
|
101
|
+
split_lines (this: string): string[]
|
|
102
|
+
|
|
103
|
+
trim_doc_comment (this: string): string
|
|
104
|
+
|
|
105
|
+
split_indent (this: string): { indent: number, text: string }
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
space (this: string): string
|
|
109
|
+
|
|
110
|
+
to_slash (this: string): string
|
|
111
|
+
|
|
112
|
+
to_backslash (this: string): string
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
interface Date {
|
|
117
|
+
to_str (this: Date): string
|
|
118
|
+
|
|
119
|
+
to_date_str (this: Date): string
|
|
120
|
+
|
|
121
|
+
to_time_str (this: Date): string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
interface Number {
|
|
126
|
+
to_bin_str (this: number): string
|
|
127
|
+
|
|
128
|
+
to_hex_str (this: number): string
|
|
129
|
+
|
|
130
|
+
to_oct_str (this: number): string
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
interface Array<T> {
|
|
135
|
+
indent (this: string[], width: number, c?: string): string[]
|
|
136
|
+
|
|
137
|
+
// --- 文本处理
|
|
138
|
+
/**
|
|
139
|
+
- trim_line?: `true`
|
|
140
|
+
- rm_empty_lines?: `true`
|
|
141
|
+
- rm_last_empty_lines?: `false`
|
|
142
|
+
*/
|
|
143
|
+
trim_lines (this: string[], { trim_line, rm_empty_lines, rm_last_empty_lines }?: { trim_line?: boolean, rm_empty_lines?: boolean, rm_last_empty_lines?: boolean }): string[]
|
|
144
|
+
|
|
145
|
+
join_lines (): string
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
import EmojiRegex from 'emoji-regex'
|
|
151
|
+
|
|
152
|
+
export const emoji_regex = EmojiRegex()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
export function to_method_property_descriptors (methods: { [name: string]: Function }): PropertyDescriptorMap {
|
|
156
|
+
return Object.fromEntries(
|
|
157
|
+
Object.entries(methods)
|
|
158
|
+
.map(([name, value]) => ([name, {
|
|
159
|
+
configurable: true,
|
|
160
|
+
writable: true,
|
|
161
|
+
enumerable: false,
|
|
162
|
+
value,
|
|
163
|
+
} as PropertyDescriptor])
|
|
164
|
+
))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
export function to_getter_property_descriptors (getters: { [name: string]: Function }): PropertyDescriptorMap {
|
|
169
|
+
return Object.fromEntries(
|
|
170
|
+
Object.entries(getters)
|
|
171
|
+
.map(([name, get]) => ([name, {
|
|
172
|
+
configurable: true,
|
|
173
|
+
enumerable: false,
|
|
174
|
+
get,
|
|
175
|
+
} as PropertyDescriptor])
|
|
176
|
+
))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
export const cjk = '([\u2e80-\u9fff\uf900-\ufaff])'
|
|
181
|
+
|
|
182
|
+
export const quotes = {
|
|
183
|
+
single: "'",
|
|
184
|
+
double: '"',
|
|
185
|
+
backtick: '`',
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export const brackets = {
|
|
189
|
+
round: ['(', ')'],
|
|
190
|
+
square: ['[', ']'],
|
|
191
|
+
curly: ['{', '}'],
|
|
192
|
+
pointy: ['<', '>'],
|
|
193
|
+
corner: ['「', '」'],
|
|
194
|
+
fat: ['【', '】'],
|
|
195
|
+
tortoise_shell: ['〔', '〕'],
|
|
196
|
+
} as const
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
// ------------------------------------ String.prototype
|
|
201
|
+
Object.defineProperties(String.prototype, {
|
|
202
|
+
... to_getter_property_descriptors({
|
|
203
|
+
width (this: string) {
|
|
204
|
+
const s = this.replace(emoji_regex, ' ')
|
|
205
|
+
let width = 0
|
|
206
|
+
for (let i = 0; i < s.length; i++) {
|
|
207
|
+
const code = s.codePointAt(i)
|
|
208
|
+
|
|
209
|
+
if (
|
|
210
|
+
(code <= 0x1f || (code >= 0x7f && code <= 0x9f)) || // ignore control characters
|
|
211
|
+
code >= 0x300 && code <= 0x36f // ignore combining characters
|
|
212
|
+
) continue
|
|
213
|
+
|
|
214
|
+
// surrogates
|
|
215
|
+
if (code > 0xFFFF)
|
|
216
|
+
i++
|
|
217
|
+
|
|
218
|
+
width += is_codepoint_fullwidth(code) ? 2 : 1
|
|
219
|
+
}
|
|
220
|
+
return width
|
|
221
|
+
}
|
|
222
|
+
}),
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
// ------------ 文本处理工具方法
|
|
227
|
+
... to_method_property_descriptors({
|
|
228
|
+
/** 截取字符串不超过 width 显示宽度的部分,并保留颜色
|
|
229
|
+
找到并记录能容纳 字符串 + … 的最后一个字符的位置 i_fitted
|
|
230
|
+
- 若完整的字符串长度超过 width,返回 slice(0, i_fitted + 1) + …
|
|
231
|
+
- 否则 返回 this
|
|
232
|
+
*/
|
|
233
|
+
truncate (this: string, width: number) {
|
|
234
|
+
const color_bak = this.startsWith('\u001b') ? this.slice(0, 5) : ''
|
|
235
|
+
if (width <= 2) return this.slice(0, width)
|
|
236
|
+
let i_fitted = 0
|
|
237
|
+
let fitted_width = 0
|
|
238
|
+
let cur_width = 0
|
|
239
|
+
for (let i = 0; i < this.length; i++) {
|
|
240
|
+
const code = this.codePointAt(i)
|
|
241
|
+
|
|
242
|
+
if (
|
|
243
|
+
(code <= 0x1F || (code >= 0x7F && code <= 0x9F)) || // Ignore control characters
|
|
244
|
+
code >= 0x300 && code <= 0x36F // Ignore combining characters
|
|
245
|
+
) continue
|
|
246
|
+
|
|
247
|
+
// surrogates (codepoint 需要用两个 utf-16 编码单位表示,因此这里跳过第二个编码单位,防止重复计算显示宽度)
|
|
248
|
+
if (code > 0xFFFF)
|
|
249
|
+
i++
|
|
250
|
+
|
|
251
|
+
const w = is_codepoint_fullwidth(code) ? 2 : 1
|
|
252
|
+
|
|
253
|
+
if (cur_width + w + 2 <= width) {
|
|
254
|
+
i_fitted = i
|
|
255
|
+
fitted_width += w
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
cur_width += w
|
|
259
|
+
|
|
260
|
+
if (cur_width > width) {
|
|
261
|
+
const i_fitted_next = i_fitted + 1
|
|
262
|
+
const t = this.slice(0, i_fitted_next) + ' '.repeat(width - 2 - fitted_width) + '…'
|
|
263
|
+
return color_bak ? color_bak + t + '\u001b[39m' : t
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return this
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
pad (this: string, width: number, { character = ' ', position = 'right' }: { character?: string, position?: 'left' | 'right'} = { }) {
|
|
271
|
+
const _width = this.width
|
|
272
|
+
if (_width >= width) return this
|
|
273
|
+
if (position === 'right') return this + character.repeat( (width - _width) / character.width )
|
|
274
|
+
return character.repeat(width - _width) + this
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
limit (this: string, width: number, { character = ' ', position = 'right' }: { character?: string, position?: 'left' | 'right'} = { }) {
|
|
279
|
+
return this.pad(width, { character, position }).truncate(width)
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
to_regx (this: string, preservations: string, flags = ''): RegExp {
|
|
284
|
+
const preserved_chars = new Set(preservations)
|
|
285
|
+
const replace_chars: string = Array.prototype.filter.call('|\\{}()[]^$+*?.-', (c: string) => !preserved_chars.has(c))
|
|
286
|
+
.map((c: string) =>
|
|
287
|
+
c === ']' ? '\\]' : c
|
|
288
|
+
).join('')
|
|
289
|
+
|
|
290
|
+
return new RegExp( this.replace(new RegExp(`[${replace_chars}]`, 'g'), '\\$&'), flags)
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
refmt (this: string,
|
|
295
|
+
pattern : string,
|
|
296
|
+
pattern_: string,
|
|
297
|
+
preservations: string = '',
|
|
298
|
+
flags = '',
|
|
299
|
+
transformer: (name: string, value: string, placeholders: { [name: string]: string }) => string = (name, value) => value || '',
|
|
300
|
+
pattern_placeholder = /\{.*?\}/g,
|
|
301
|
+
): string {
|
|
302
|
+
// --- 转换 pattern 为 pattern_regx
|
|
303
|
+
let last_end = 0
|
|
304
|
+
|
|
305
|
+
// placeholder matched group indexes
|
|
306
|
+
let $placeholders: Record<string, number> = { }
|
|
307
|
+
|
|
308
|
+
let regx_parts = [ ]
|
|
309
|
+
|
|
310
|
+
function add_part (left: number, right?: number) {
|
|
311
|
+
const part = pattern.slice(left, right)
|
|
312
|
+
if (part)
|
|
313
|
+
regx_parts.push(
|
|
314
|
+
part.to_regx(preservations).source.bracket()
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
pattern.replace(pattern_placeholder, ($0, offset) => {
|
|
319
|
+
add_part(last_end, offset)
|
|
320
|
+
last_end = offset + $0.length
|
|
321
|
+
|
|
322
|
+
const placeholder = $0.slice(1, -1)
|
|
323
|
+
let [placeholder_name, placeholder_pattern] = placeholder.split(':').map(s => s.trim())
|
|
324
|
+
let optional = false
|
|
325
|
+
if (placeholder_name.endsWith('?')) {
|
|
326
|
+
placeholder_name = placeholder_name.slice(0, -1)
|
|
327
|
+
optional = true
|
|
328
|
+
}
|
|
329
|
+
$placeholders[placeholder_name] = regx_parts.push(placeholder_pattern ?
|
|
330
|
+
`${placeholder_pattern.bracket()}${optional ? '?' : ''}`
|
|
331
|
+
:
|
|
332
|
+
'(.*?)'
|
|
333
|
+
)
|
|
334
|
+
return ''
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
add_part(last_end)
|
|
338
|
+
|
|
339
|
+
// 最后一个 (.*?) 改为贪心匹配,满足 .{suffix} 的需要
|
|
340
|
+
regx_parts = regx_parts.filter(part => part)
|
|
341
|
+
if (regx_parts.last === '(.*?)')
|
|
342
|
+
regx_parts[regx_parts.length - 1] = '(.*)'
|
|
343
|
+
|
|
344
|
+
const pattern_regx = new RegExp(regx_parts.join(''), flags)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
// --- 根据 pattern_regx 去匹配原有字符串,获取匹配结果,生成 placeholders 词典
|
|
348
|
+
const matches = pattern_regx.exec(this)
|
|
349
|
+
|
|
350
|
+
if (!matches) return this
|
|
351
|
+
|
|
352
|
+
const placeholders = Object.fromEntries(
|
|
353
|
+
Object.entries($placeholders)
|
|
354
|
+
.map(([name, $i]) => [
|
|
355
|
+
[name, matches[$i]],
|
|
356
|
+
[`${name}.before`, matches[$i - 1] || ''],
|
|
357
|
+
[`${name}.after`, matches[$i + 1] || ''],
|
|
358
|
+
])
|
|
359
|
+
.flat()
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
// --- 转换 pattern_ 为 replacement_str,如果有 transformer 则在遇到 placeholder 时应用
|
|
364
|
+
last_end = 0
|
|
365
|
+
let replacement_parts = [ ]
|
|
366
|
+
|
|
367
|
+
pattern_.replace(pattern_placeholder, ($0, offset) => {
|
|
368
|
+
replacement_parts.push(
|
|
369
|
+
pattern_.slice(last_end, offset)
|
|
370
|
+
)
|
|
371
|
+
last_end = offset + $0.length
|
|
372
|
+
|
|
373
|
+
const placeholder_name = $0.slice(1, -1)
|
|
374
|
+
|
|
375
|
+
replacement_parts.push(
|
|
376
|
+
transformer(placeholder_name, placeholders[placeholder_name], placeholders)
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
return ''
|
|
380
|
+
})
|
|
381
|
+
replacement_parts.push(
|
|
382
|
+
pattern_.slice(last_end)
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
return this.replace(pattern_regx, replacement_parts.join(''))
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
find (this: string,
|
|
390
|
+
pattern: string,
|
|
391
|
+
preservations: string = '',
|
|
392
|
+
flags = '',
|
|
393
|
+
pattern_placeholder = /\{.*?\}/g
|
|
394
|
+
): { [name: string]: string } {
|
|
395
|
+
// --- 转换 pattern 为 pattern_regx
|
|
396
|
+
let last_end = 0
|
|
397
|
+
|
|
398
|
+
// placeholder matched group index
|
|
399
|
+
let $placeholders: Record<string, number> = { }
|
|
400
|
+
|
|
401
|
+
let regx_parts = [ ]
|
|
402
|
+
|
|
403
|
+
function add_part (left: number, right?: number) {
|
|
404
|
+
const part = pattern.slice(left, right)
|
|
405
|
+
if (part)
|
|
406
|
+
regx_parts.push(
|
|
407
|
+
part.to_regx(preservations).source.bracket()
|
|
408
|
+
)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
pattern.replace(pattern_placeholder, ($0, offset) => {
|
|
412
|
+
add_part(last_end, offset)
|
|
413
|
+
last_end = offset + $0.length
|
|
414
|
+
|
|
415
|
+
const placeholder = $0.slice(1, -1)
|
|
416
|
+
let [placeholder_name, placeholder_pattern] = placeholder.split(':').map(s => s.trim())
|
|
417
|
+
let optional = false
|
|
418
|
+
if (placeholder_name.endsWith('?')) {
|
|
419
|
+
placeholder_name = placeholder_name.slice(0, -1)
|
|
420
|
+
optional = true
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
$placeholders[placeholder_name] = regx_parts.push(
|
|
424
|
+
placeholder_pattern ?
|
|
425
|
+
`${placeholder_pattern.bracket()}${optional ? '?' : ''}`
|
|
426
|
+
:
|
|
427
|
+
'(.*?)'
|
|
428
|
+
)
|
|
429
|
+
return ''
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
add_part(last_end)
|
|
433
|
+
|
|
434
|
+
// 最后一个 (.*?) 改为贪心匹配,满足 .{suffix} 的需要
|
|
435
|
+
regx_parts = regx_parts.filter(part => part)
|
|
436
|
+
if (regx_parts[ regx_parts.length - 1 ] === '(.*?)')
|
|
437
|
+
regx_parts[regx_parts.length - 1] = '(.*)'
|
|
438
|
+
|
|
439
|
+
const pattern_regx = new RegExp(regx_parts.join(''), flags)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
// --- 根据 pattern_regx 去匹配原有字符串,获取匹配结果,生成 placeholders 词典
|
|
443
|
+
const matches = pattern_regx.exec(this)
|
|
444
|
+
|
|
445
|
+
if (!matches) return { }
|
|
446
|
+
|
|
447
|
+
return Object.fromEntries(
|
|
448
|
+
Object.entries($placeholders)
|
|
449
|
+
.map(([name, $i]) =>
|
|
450
|
+
[name, matches[$i] || '']
|
|
451
|
+
)
|
|
452
|
+
)
|
|
453
|
+
},
|
|
454
|
+
|
|
455
|
+
quote (this: string, type: keyof typeof quotes | 'psh' = 'single') {
|
|
456
|
+
if (type === 'psh') return '& ' + this.quote()
|
|
457
|
+
return this.surround(quotes[type])
|
|
458
|
+
},
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
bracket (this: string, shape: keyof typeof brackets = 'round') {
|
|
462
|
+
return this.surround(...brackets[shape] as [string, string])
|
|
463
|
+
},
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
surround (this: string, left: string, right?: string) {
|
|
467
|
+
return left + this + (right || left)
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
surround_tag (this: string, tag_name: string): string {
|
|
471
|
+
return '<' + tag_name + '>' + this + '</' + tag_name + '>'
|
|
472
|
+
},
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
to_lf (this: string) {
|
|
476
|
+
return this.replace(/\r\n/g, '\n')
|
|
477
|
+
},
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
to_crlf (this: string) {
|
|
481
|
+
return this.replace(/\n/g, '\r\n')
|
|
482
|
+
},
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
rm (this: string, pattern: string | RegExp, flags: string = 'g') {
|
|
486
|
+
if (typeof pattern === 'string')
|
|
487
|
+
pattern = new RegExp(pattern, flags)
|
|
488
|
+
|
|
489
|
+
return this.replace(pattern, '')
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
split_lines (this: string, delimiter: string | RegExp = /\r?\n/) {
|
|
494
|
+
let lines = this.split(delimiter)
|
|
495
|
+
if (lines.last === '')
|
|
496
|
+
lines.pop()
|
|
497
|
+
return lines
|
|
498
|
+
},
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
split_indent (this: string): { indent: number, text: string } {
|
|
502
|
+
let i = 0
|
|
503
|
+
let indent = 0
|
|
504
|
+
for (; i < this.length; i++)
|
|
505
|
+
if (this[i] === ' ')
|
|
506
|
+
indent += 1
|
|
507
|
+
else if (this[i] === '\t')
|
|
508
|
+
indent += 4
|
|
509
|
+
else
|
|
510
|
+
break
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
indent,
|
|
514
|
+
text: this.slice(i)
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
space (this: string) {
|
|
520
|
+
if (!this) return this
|
|
521
|
+
let text_: string
|
|
522
|
+
text_ = this
|
|
523
|
+
.replace(new RegExp(cjk + `(['"])`, 'g'), '$1 $2')
|
|
524
|
+
.replace(new RegExp(`(['"])` + cjk, 'g'), '$1 $2')
|
|
525
|
+
|
|
526
|
+
.replace(/(["']+)\s*(.+?)\s*(["']+)/g, '$1$2$3')
|
|
527
|
+
|
|
528
|
+
.replace(new RegExp(cjk + '([\\+\\-\\*\\/=&\\\\\\|<>])([A-Za-z0-9])', 'g'), '$1 $2 $3')
|
|
529
|
+
.replace(new RegExp('([A-Za-z0-9])([\\+\\-\\*\\/=&\\\\\\|<>])' + cjk, 'g'), '$1 $2 $3')
|
|
530
|
+
|
|
531
|
+
const textBak = text_
|
|
532
|
+
|
|
533
|
+
text_ = text_.replace(new RegExp(cjk + '([\\(\\[\\{<\u201c]+(.*?)[\\)\\]\\}>\u201d]+)' + cjk, 'g'), '$1 $2 $4')
|
|
534
|
+
|
|
535
|
+
if (text_ === textBak)
|
|
536
|
+
text_ = text_
|
|
537
|
+
.replace(new RegExp(cjk + '([\\(\\[\\{<\u201c>])', 'g'), '$1 $2')
|
|
538
|
+
.replace(new RegExp('([\\)\\]\\}>\u201d<])' + cjk, 'g'), '$1 $2')
|
|
539
|
+
|
|
540
|
+
return text_
|
|
541
|
+
// eslint-disable-next-line no-useless-escape
|
|
542
|
+
.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g, '$1$3$5')
|
|
543
|
+
.replace(new RegExp(cjk + '([~!;:,\\.\\?\u2026])([A-Za-z0-9])', 'g'), '$1$2 $3')
|
|
544
|
+
.replace(new RegExp(cjk + '([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])', 'g'), '$1 $2')
|
|
545
|
+
.replace(new RegExp('([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])' + cjk, 'g'), '$1 $2')
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
to_slash (this: string) {
|
|
549
|
+
return this.replaceAll('\\', '/')
|
|
550
|
+
},
|
|
551
|
+
|
|
552
|
+
to_backslash (this: string) {
|
|
553
|
+
return this.replaceAll('/', '\\')
|
|
554
|
+
},
|
|
555
|
+
}),
|
|
556
|
+
})
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
// ------------------------------------ Date.prototype
|
|
560
|
+
Object.defineProperties(Date.prototype, to_method_property_descriptors({
|
|
561
|
+
to_str (this: Date) {
|
|
562
|
+
return this.toLocaleString().replace(/(\d+)\/(\d+)\/(\d+) ?(上午|下午)(\d+):(\d{2}):(\d{2}).*/, (matches, year, month, day, ampm, hour, minute, second) => {
|
|
563
|
+
hour = Number(hour)
|
|
564
|
+
if (ampm === '上午' && hour === 12) {
|
|
565
|
+
hour = 0
|
|
566
|
+
ampm = '凌晨'
|
|
567
|
+
}
|
|
568
|
+
else if (ampm === '上午' && hour <= 6) ampm = '凌晨'
|
|
569
|
+
else if (ampm === '上午' && hour <= 8 ) ampm = '早上'
|
|
570
|
+
else if (ampm === '上午' && hour <= 10) ampm = '上午'
|
|
571
|
+
else if (ampm === '上午' && hour <= 11) ampm = '中午'
|
|
572
|
+
else if (ampm === '下午' && hour === 12) ampm = '中午'
|
|
573
|
+
else if (ampm === '下午' && hour <= 5 ) ampm = '下午'
|
|
574
|
+
else ampm = '晚上'
|
|
575
|
+
|
|
576
|
+
return year + '.' + month.pad(2, { character: '0', position: 'left' }) + '.' + day.pad(2, { character: '0', position: 'left' }) + ' ' +
|
|
577
|
+
ampm + ' ' + hour.toString().pad(2, { character: '0', position: 'left' }) + ':' + minute + ':' + second
|
|
578
|
+
})
|
|
579
|
+
},
|
|
580
|
+
|
|
581
|
+
to_date_str (this: Date) {
|
|
582
|
+
return this.to_str().split(' ')[0]
|
|
583
|
+
},
|
|
584
|
+
|
|
585
|
+
to_time_str (this: Date) {
|
|
586
|
+
const [, ampm, time ] = this.to_str().split(' ')
|
|
587
|
+
return ampm + ' ' + time
|
|
588
|
+
},
|
|
589
|
+
}))
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
// ------------------------------------ Number.prototype
|
|
594
|
+
Object.defineProperties(Number.prototype, to_method_property_descriptors({
|
|
595
|
+
to_bin_str (this: number) {
|
|
596
|
+
return `0b${this.toString(2)}`
|
|
597
|
+
},
|
|
598
|
+
|
|
599
|
+
to_hex_str (this: number, length?: number) {
|
|
600
|
+
const s = this.toString(16)
|
|
601
|
+
// 长度自动对齐到 4 的倍数
|
|
602
|
+
if (!length)
|
|
603
|
+
length = Math.ceil(s.length / 4) * 4
|
|
604
|
+
return `0x${'0'.repeat(length - s.length)}${s}`
|
|
605
|
+
},
|
|
606
|
+
|
|
607
|
+
to_oct_str (this: number) {
|
|
608
|
+
return `0o${this.toString(8)}`
|
|
609
|
+
},
|
|
610
|
+
}))
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
// ------------------------------------ Array.prototype
|
|
615
|
+
Object.defineProperties(Array.prototype, {
|
|
616
|
+
// --- 文本处理工具方法
|
|
617
|
+
... to_method_property_descriptors({
|
|
618
|
+
trim_lines (this: string[], { trim_line = true, rm_empty_lines = true, rm_last_empty_lines = false }: { trim_line?: boolean, rm_empty_lines?: boolean, rm_last_empty_lines?: boolean } = { }) {
|
|
619
|
+
if (!this.length) return this
|
|
620
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
621
|
+
let lines = this
|
|
622
|
+
|
|
623
|
+
if (trim_line)
|
|
624
|
+
lines = lines.map(line => line.trim())
|
|
625
|
+
|
|
626
|
+
if (rm_empty_lines)
|
|
627
|
+
return lines.filter( line => line )
|
|
628
|
+
|
|
629
|
+
if (rm_last_empty_lines) {
|
|
630
|
+
lines.reverse()
|
|
631
|
+
const i_not_empty = lines.findIndex( line => line )
|
|
632
|
+
if (i_not_empty !== -1)
|
|
633
|
+
lines = lines.slice(i_not_empty)
|
|
634
|
+
lines.reverse()
|
|
635
|
+
return lines
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return lines
|
|
639
|
+
},
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
indent (this: string[], width?: number, character: string = ' ') {
|
|
643
|
+
return this.map(line =>
|
|
644
|
+
character.repeat(width) + line
|
|
645
|
+
)
|
|
646
|
+
},
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
join_lines (this: string[], append = true) {
|
|
650
|
+
return `${this.join('\n')}${append ? '\n' : ''}`
|
|
651
|
+
}
|
|
652
|
+
})
|
|
653
|
+
})
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
export function to_json (object: any, replacer?: any) {
|
|
657
|
+
return JSON.stringify(object, replacer, 4)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
export function is_codepoint_fullwidth (codepoint: number) {
|
|
661
|
+
// Code points are derived from:
|
|
662
|
+
// http://www.unix.org/Public/UNIDATA/EastAsianWidth.txt
|
|
663
|
+
return (
|
|
664
|
+
!Number.isNaN(codepoint) &&
|
|
665
|
+
codepoint >= 0x1100 &&
|
|
666
|
+
(
|
|
667
|
+
codepoint <= 0x115F || // Hangul Jamo
|
|
668
|
+
|
|
669
|
+
codepoint === 0x2026 || // …
|
|
670
|
+
codepoint === 0x203B || // ※
|
|
671
|
+
|
|
672
|
+
// arrows
|
|
673
|
+
(0x2190 <= codepoint && codepoint <= 0x21ff) ||
|
|
674
|
+
|
|
675
|
+
codepoint === 0x2329 || // left-pointing angle bracket
|
|
676
|
+
codepoint === 0x232a || // right-pointing angle bracket
|
|
677
|
+
|
|
678
|
+
// ①
|
|
679
|
+
(0x2460 <= codepoint && codepoint <= 0x24ff) ||
|
|
680
|
+
|
|
681
|
+
// box drawing
|
|
682
|
+
(0x2500 <= codepoint && codepoint <= 0x257f) ||
|
|
683
|
+
|
|
684
|
+
// shapes, symbols, …
|
|
685
|
+
(0x2580 <= codepoint && codepoint <= 0x2bef) ||
|
|
686
|
+
|
|
687
|
+
// cjk radicals supplement .. enclosed cjk letters and months
|
|
688
|
+
(0x2e80 <= codepoint && codepoint <= 0x3247 && codepoint !== 0x303f) ||
|
|
689
|
+
|
|
690
|
+
// enclosed cjk letters and months .. cjk unified ideographs extension a
|
|
691
|
+
(0x3250 <= codepoint && codepoint <= 0x4dbf) ||
|
|
692
|
+
|
|
693
|
+
// cjk unified ideographs .. yi radicals
|
|
694
|
+
(0x4e00 <= codepoint && codepoint <= 0xa4c6) ||
|
|
695
|
+
|
|
696
|
+
// hangul jamo extended-a
|
|
697
|
+
(0xa960 <= codepoint && codepoint <= 0xa97c) ||
|
|
698
|
+
|
|
699
|
+
// hangul syllables
|
|
700
|
+
(0xac00 <= codepoint && codepoint <= 0xd7a3) ||
|
|
701
|
+
|
|
702
|
+
// cjk compatibility ideographs
|
|
703
|
+
(0xf900 <= codepoint && codepoint <= 0xfaff) ||
|
|
704
|
+
|
|
705
|
+
// vertical forms
|
|
706
|
+
(0xfe10 <= codepoint && codepoint <= 0xfe19) ||
|
|
707
|
+
|
|
708
|
+
// cjk compatibility forms .. small form variants
|
|
709
|
+
(0xfe30 <= codepoint && codepoint <= 0xfe6b) ||
|
|
710
|
+
|
|
711
|
+
// halfwidth and fullwidth forms
|
|
712
|
+
(0xff01 <= codepoint && codepoint <= 0xff60) ||
|
|
713
|
+
(0xffe0 <= codepoint && codepoint <= 0xffe6) ||
|
|
714
|
+
|
|
715
|
+
// kana supplement
|
|
716
|
+
(0x1b000 <= codepoint && codepoint <= 0x1b001) ||
|
|
717
|
+
|
|
718
|
+
// enclosed ideographic supplement
|
|
719
|
+
(0x1f200 <= codepoint && codepoint <= 0x1f251) ||
|
|
720
|
+
|
|
721
|
+
// cjk unified ideographs extension b .. tertiary ideographic plane
|
|
722
|
+
(0x20000 <= codepoint && codepoint <= 0x3fffd)
|
|
723
|
+
)
|
|
724
|
+
)
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|