@jayfong/x-utils 1.0.0-202510061015 → 3.3.0
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/lib/case.d.mts +184 -0
- package/lib/case.mjs +120 -0
- package/lib/chinese.d.mts +39 -0
- package/lib/chinese.mjs +52 -0
- package/lib/cli.d.mts +64 -0
- package/lib/cli.mjs +59 -0
- package/lib/clipboard.d.mts +50 -0
- package/lib/clipboard.mjs +122 -0
- package/lib/cmd.d.mts +38 -0
- package/lib/cmd.mjs +52 -0
- package/lib/crawler.d.mts +330 -0
- package/lib/crawler.mjs +313 -0
- package/lib/csv.d.mts +67 -0
- package/lib/csv.mjs +76 -0
- package/lib/data_set.d.mts +76 -0
- package/lib/data_set.mjs +104 -0
- package/lib/fs.d.mts +49 -0
- package/lib/fs.mjs +65 -0
- package/lib/json.d.mts +50 -0
- package/lib/json.mjs +74 -0
- package/lib/puppeteer.d.mts +88 -0
- package/lib/puppeteer.mjs +219 -0
- package/lib/runner.d.mts +52 -0
- package/lib/runner.mjs +50 -0
- package/lib/task.d.mts +37 -0
- package/lib/task.mjs +43 -0
- package/lib/text.d.mts +84 -0
- package/lib/text.mjs +141 -0
- package/lib/yaml.d.mts +51 -0
- package/lib/yaml.mjs +64 -0
- package/package.json +50 -164
- package/_cjs/ai.js +0 -80
- package/_cjs/alipan.js +0 -173
- package/_cjs/cli.js +0 -51
- package/_cjs/clipboard.js +0 -65
- package/_cjs/cmd.js +0 -42
- package/_cjs/compressor.js +0 -47
- package/_cjs/crawler.js +0 -341
- package/_cjs/csv.js +0 -97
- package/_cjs/data_set.js +0 -115
- package/_cjs/env.js +0 -82
- package/_cjs/fs.js +0 -86
- package/_cjs/html.js +0 -62
- package/_cjs/http_server.js +0 -79
- package/_cjs/json.js +0 -49
- package/_cjs/ksb.js +0 -218
- package/_cjs/number.js +0 -39
- package/_cjs/ocr.js +0 -74
- package/_cjs/package.json +0 -3
- package/_cjs/path.js +0 -35
- package/_cjs/pinyin.js +0 -37
- package/_cjs/puppeteer.js +0 -271
- package/_cjs/runner.js +0 -88
- package/_cjs/task.js +0 -41
- package/_cjs/text.js +0 -136
- package/_cjs/unicode/lib_unicode.js +0 -58643
- package/_cjs/unicode/unicode.js +0 -441
- package/_cjs/upload.js +0 -101
- package/_cjs/yaml.js +0 -52
- package/ai.d.ts +0 -26
- package/ai.js +0 -60
- package/alipan.d.ts +0 -209
- package/alipan.js +0 -153
- package/cli.d.ts +0 -14
- package/cli.js +0 -21
- package/clipboard.d.ts +0 -6
- package/clipboard.js +0 -35
- package/cmd.d.ts +0 -5
- package/cmd.js +0 -22
- package/compressor.d.ts +0 -5
- package/compressor.js +0 -17
- package/crawler.d.ts +0 -162
- package/crawler.js +0 -332
- package/csv.d.ts +0 -14
- package/csv.js +0 -67
- package/data_set.d.ts +0 -22
- package/data_set.js +0 -85
- package/env.d.ts +0 -6
- package/env.js +0 -52
- package/fs.d.ts +0 -23
- package/fs.js +0 -56
- package/html.d.ts +0 -7
- package/html.js +0 -45
- package/http_server.d.ts +0 -13
- package/http_server.js +0 -49
- package/json.d.ts +0 -6
- package/json.js +0 -19
- package/ksb.d.ts +0 -37
- package/ksb.js +0 -198
- package/number.d.ts +0 -3
- package/number.js +0 -9
- package/ocr.d.ts +0 -17
- package/ocr.js +0 -54
- package/path.d.ts +0 -2
- package/path.js +0 -5
- package/pinyin.d.ts +0 -8
- package/pinyin.js +0 -17
- package/puppeteer.d.ts +0 -38
- package/puppeteer.js +0 -241
- package/runner.d.ts +0 -13
- package/runner.js +0 -58
- package/task.d.ts +0 -6
- package/task.js +0 -21
- package/text.d.ts +0 -63
- package/text.js +0 -119
- package/unicode/lib_unicode.d.ts +0 -4
- package/unicode/lib_unicode.js +0 -58623
- package/unicode/unicode.d.ts +0 -347
- package/unicode/unicode.js +0 -426
- package/upload.d.ts +0 -34
- package/upload.js +0 -71
- package/yaml.d.ts +0 -6
- package/yaml.js +0 -22
package/puppeteer.js
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import { xr } from "@jayfong/x-request";
|
|
2
|
-
import { asyncExitHook } from "exit-hook";
|
|
3
|
-
import puppeteer from "puppeteer";
|
|
4
|
-
import { isFunction, isRegExp, memoize } from "vtils";
|
|
5
|
-
import { XClipboard } from "./clipboard.js";
|
|
6
|
-
class XPuppeteer {
|
|
7
|
-
browser;
|
|
8
|
-
async connectSystemBrowser() {
|
|
9
|
-
const {
|
|
10
|
-
data: { webSocketDebuggerUrl }
|
|
11
|
-
} = await xr.getJson({
|
|
12
|
-
url: "http://localhost:9222/json/version"
|
|
13
|
-
});
|
|
14
|
-
const browser = await puppeteer.connect({
|
|
15
|
-
browserWSEndpoint: webSocketDebuggerUrl,
|
|
16
|
-
// 都设为 0 以保持原有的宽高
|
|
17
|
-
defaultViewport: {
|
|
18
|
-
width: 0,
|
|
19
|
-
height: 0
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
this.browser = browser;
|
|
23
|
-
asyncExitHook(() => this.disconnect(), {
|
|
24
|
-
wait: 1e3
|
|
25
|
-
});
|
|
26
|
-
return browser;
|
|
27
|
-
}
|
|
28
|
-
async openBrowser(options = {}) {
|
|
29
|
-
const browser = await puppeteer.launch({
|
|
30
|
-
headless: options.headful ? false : true,
|
|
31
|
-
defaultViewport: {
|
|
32
|
-
width: 1440,
|
|
33
|
-
height: 900
|
|
34
|
-
},
|
|
35
|
-
userDataDir: options.dataDir,
|
|
36
|
-
args: options.proxy ? [`--proxy-server=${options.proxy}`] : void 0
|
|
37
|
-
});
|
|
38
|
-
this.browser = browser;
|
|
39
|
-
asyncExitHook(() => this.close(), {
|
|
40
|
-
wait: 1e3
|
|
41
|
-
});
|
|
42
|
-
return browser;
|
|
43
|
-
}
|
|
44
|
-
async openPage(urlOrHtml, options = {}) {
|
|
45
|
-
const isUrl = /^https?:\/\//i.test(urlOrHtml);
|
|
46
|
-
const pages = await this.browser.pages();
|
|
47
|
-
if (isUrl) {
|
|
48
|
-
const inputUrl = new URL(urlOrHtml);
|
|
49
|
-
const foundPage = pages.find((item) => {
|
|
50
|
-
const pageUrl = item.url();
|
|
51
|
-
const url = new URL(pageUrl);
|
|
52
|
-
return inputUrl.origin === url.origin && (pageUrl === urlOrHtml || !!options.match && (typeof options.match === "string" ? `${url.pathname}${url.search}`.includes(options.match) : options.match.test(`${url.pathname}${url.search}`)));
|
|
53
|
-
});
|
|
54
|
-
if (foundPage) {
|
|
55
|
-
return foundPage;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const page2 = await this.browser.newPage();
|
|
59
|
-
await page2.setUserAgent(
|
|
60
|
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
|
61
|
-
);
|
|
62
|
-
if (options.blockAssets) {
|
|
63
|
-
await page2.setRequestInterception(true);
|
|
64
|
-
page2.on("request", (req) => {
|
|
65
|
-
const resourceType = req.resourceType();
|
|
66
|
-
if (resourceType === "stylesheet" || resourceType === "font" || resourceType === "image") {
|
|
67
|
-
req.abort();
|
|
68
|
-
} else {
|
|
69
|
-
req.continue();
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
if (isUrl) {
|
|
74
|
-
await page2.goto(urlOrHtml, {
|
|
75
|
-
referer: options.referer || urlOrHtml
|
|
76
|
-
});
|
|
77
|
-
} else {
|
|
78
|
-
await page2.setContent(urlOrHtml);
|
|
79
|
-
}
|
|
80
|
-
return page2;
|
|
81
|
-
}
|
|
82
|
-
async input(page2, selector, textOrHtml, frameIndex) {
|
|
83
|
-
const mainPage = page2;
|
|
84
|
-
const keyboard = page2.keyboard;
|
|
85
|
-
if (frameIndex != null) {
|
|
86
|
-
page2 = page2.frames()[frameIndex];
|
|
87
|
-
}
|
|
88
|
-
const el2 = await page2.$(selector);
|
|
89
|
-
const isText = await el2.evaluate((el3) => {
|
|
90
|
-
const isInput = el3.tagName === "INPUT" || el3.tagName === "TEXTAREA";
|
|
91
|
-
const isText2 = isInput || el3.tagName === "SPAN";
|
|
92
|
-
return isText2;
|
|
93
|
-
});
|
|
94
|
-
if (isText) {
|
|
95
|
-
await XClipboard.writeText(textOrHtml);
|
|
96
|
-
} else {
|
|
97
|
-
await XClipboard.writeHtml(textOrHtml);
|
|
98
|
-
}
|
|
99
|
-
await el2.click();
|
|
100
|
-
await this.selectAll(page2);
|
|
101
|
-
await keyboard.press("Backspace");
|
|
102
|
-
await this.paste(mainPage);
|
|
103
|
-
}
|
|
104
|
-
async selectAll(page2) {
|
|
105
|
-
await page2.evaluate(() => {
|
|
106
|
-
const isMac = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
|
|
107
|
-
document.activeElement.dispatchEvent(
|
|
108
|
-
new KeyboardEvent("keydown", {
|
|
109
|
-
bubbles: true,
|
|
110
|
-
cancelable: true,
|
|
111
|
-
key: isMac ? "Meta" : "Control",
|
|
112
|
-
code: isMac ? "MetaLeft" : "ControlLeft",
|
|
113
|
-
location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT,
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"),
|
|
116
|
-
ctrlKey: !isMac,
|
|
117
|
-
metaKey: isMac,
|
|
118
|
-
charCode: 0,
|
|
119
|
-
keyCode: isMac ? 93 : 17,
|
|
120
|
-
which: isMac ? 93 : 17
|
|
121
|
-
})
|
|
122
|
-
);
|
|
123
|
-
const preventableEvent = new KeyboardEvent("keydown", {
|
|
124
|
-
bubbles: true,
|
|
125
|
-
cancelable: true,
|
|
126
|
-
key: "a",
|
|
127
|
-
code: "KeyA",
|
|
128
|
-
location: window.KeyboardEvent.DOM_KEY_LOCATION_STANDARD,
|
|
129
|
-
// @ts-ignore
|
|
130
|
-
getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"),
|
|
131
|
-
ctrlKey: !isMac,
|
|
132
|
-
metaKey: isMac,
|
|
133
|
-
charCode: 0,
|
|
134
|
-
keyCode: 65,
|
|
135
|
-
which: 65
|
|
136
|
-
});
|
|
137
|
-
const wasPrevented = !document.activeElement.dispatchEvent(preventableEvent) || preventableEvent.defaultPrevented;
|
|
138
|
-
if (!wasPrevented) {
|
|
139
|
-
document.execCommand("selectall", false, null);
|
|
140
|
-
}
|
|
141
|
-
document.activeElement.dispatchEvent(
|
|
142
|
-
new KeyboardEvent("keyup", {
|
|
143
|
-
bubbles: true,
|
|
144
|
-
cancelable: true,
|
|
145
|
-
key: isMac ? "Meta" : "Control",
|
|
146
|
-
code: isMac ? "MetaLeft" : "ControlLeft",
|
|
147
|
-
location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT,
|
|
148
|
-
// @ts-ignore
|
|
149
|
-
getModifierState: () => false,
|
|
150
|
-
charCode: 0,
|
|
151
|
-
keyCode: isMac ? 93 : 17,
|
|
152
|
-
which: isMac ? 93 : 17
|
|
153
|
-
})
|
|
154
|
-
);
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
async paste(page2) {
|
|
158
|
-
await page2.keyboard.down("Shift");
|
|
159
|
-
await page2.keyboard.press("Insert");
|
|
160
|
-
await page2.keyboard.up("Shift");
|
|
161
|
-
}
|
|
162
|
-
async queryByText(page, text) {
|
|
163
|
-
const el = await page.evaluateHandle(
|
|
164
|
-
(text, isRegExp, isFunction) => {
|
|
165
|
-
const textRe = isRegExp ? (() => {
|
|
166
|
-
const m = text.match(/^\/(.+)\/([^/]*)$/);
|
|
167
|
-
return new RegExp(m[1], m[2] || void 0);
|
|
168
|
-
})() : void 0;
|
|
169
|
-
const textFn = isFunction ? eval(`(() => {return ${text}})()`) : void 0;
|
|
170
|
-
const walker = document.createTreeWalker(
|
|
171
|
-
document.body,
|
|
172
|
-
NodeFilter.SHOW_TEXT,
|
|
173
|
-
{
|
|
174
|
-
acceptNode: (node) => {
|
|
175
|
-
return (isRegExp ? textRe.test(node.textContent) : isFunction ? textFn(node) : node.textContent.includes(text)) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
);
|
|
179
|
-
walker.nextNode();
|
|
180
|
-
return walker.currentNode.parentElement;
|
|
181
|
-
},
|
|
182
|
-
String(text),
|
|
183
|
-
isRegExp(text),
|
|
184
|
-
isFunction(text)
|
|
185
|
-
);
|
|
186
|
-
return el;
|
|
187
|
-
}
|
|
188
|
-
async disconnect() {
|
|
189
|
-
await this.browser.disconnect();
|
|
190
|
-
}
|
|
191
|
-
async close() {
|
|
192
|
-
await this.browser.close();
|
|
193
|
-
}
|
|
194
|
-
static async openBrowser(options = {}) {
|
|
195
|
-
const xp = new XPuppeteer();
|
|
196
|
-
await xp.openBrowser(options);
|
|
197
|
-
return xp;
|
|
198
|
-
}
|
|
199
|
-
static async openPage(urlOrHtml, options = {}) {
|
|
200
|
-
const xp = new XPuppeteer();
|
|
201
|
-
await xp.openBrowser(options);
|
|
202
|
-
const page2 = await xp.openPage(urlOrHtml, options);
|
|
203
|
-
page2.close = () => xp.close();
|
|
204
|
-
return page2;
|
|
205
|
-
}
|
|
206
|
-
static getPageHtmlXp = memoize(
|
|
207
|
-
async (options) => {
|
|
208
|
-
const xp = new XPuppeteer();
|
|
209
|
-
await (options.useSystemBrowser ? xp.connectSystemBrowser() : xp.openBrowser(options));
|
|
210
|
-
return xp;
|
|
211
|
-
},
|
|
212
|
-
(options) => options.instanceId
|
|
213
|
-
);
|
|
214
|
-
static async getPageHtml(url, options = {}) {
|
|
215
|
-
const hasInstanceId = !!options.instanceId;
|
|
216
|
-
options.instanceId ||= Date.now();
|
|
217
|
-
const xp = await XPuppeteer.getPageHtmlXp(options);
|
|
218
|
-
try {
|
|
219
|
-
const page2 = await xp.openPage(url, options);
|
|
220
|
-
if (options.afterOpenPage) {
|
|
221
|
-
await options.afterOpenPage({
|
|
222
|
-
browser: xp,
|
|
223
|
-
page: page2
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
const html = await page2.content();
|
|
227
|
-
return html;
|
|
228
|
-
} finally {
|
|
229
|
-
if (!hasInstanceId) {
|
|
230
|
-
if (options.useSystemBrowser) {
|
|
231
|
-
await xp.disconnect();
|
|
232
|
-
} else {
|
|
233
|
-
await xp.close();
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
export {
|
|
240
|
-
XPuppeteer
|
|
241
|
-
};
|
package/runner.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { DistinctQuestion } from 'inquirer';
|
|
2
|
-
import { MsValue } from 'vtils';
|
|
3
|
-
export type XRunnerOptions<TConfig extends Record<any, any>> = {
|
|
4
|
-
configFile: string;
|
|
5
|
-
delay: MsValue | ((retryCount: number) => MsValue);
|
|
6
|
-
questions: Array<DistinctQuestion<TConfig>>;
|
|
7
|
-
run: (config: TConfig) => any;
|
|
8
|
-
};
|
|
9
|
-
export declare class XRunner<TConfig extends Record<any, any>> {
|
|
10
|
-
private options;
|
|
11
|
-
constructor(options: XRunnerOptions<TConfig>);
|
|
12
|
-
run(): Promise<void>;
|
|
13
|
-
}
|
package/runner.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import inquirer from "inquirer";
|
|
2
|
-
import { ms, wait } from "vtils";
|
|
3
|
-
import { XFs } from "./fs.js";
|
|
4
|
-
class XRunner {
|
|
5
|
-
constructor(options) {
|
|
6
|
-
this.options = options;
|
|
7
|
-
}
|
|
8
|
-
async run() {
|
|
9
|
-
await XFs.ensureFile(this.options.configFile);
|
|
10
|
-
const savedConfig = await XFs.readJson(this.options.configFile, { throws: false }) || {};
|
|
11
|
-
const config = await inquirer.prompt(
|
|
12
|
-
this.options.questions.map((item) => {
|
|
13
|
-
return {
|
|
14
|
-
...item,
|
|
15
|
-
default: savedConfig[item.name] ?? item.default
|
|
16
|
-
};
|
|
17
|
-
})
|
|
18
|
-
);
|
|
19
|
-
await XFs.writeJson(this.options.configFile, config, { spaces: 2 });
|
|
20
|
-
let retryCount = 0;
|
|
21
|
-
while (true) {
|
|
22
|
-
try {
|
|
23
|
-
await this.options.run(config);
|
|
24
|
-
retryCount = 0;
|
|
25
|
-
} catch (err) {
|
|
26
|
-
const delay = typeof this.options.delay === "function" ? this.options.delay(retryCount) : this.options.delay;
|
|
27
|
-
console.log(err);
|
|
28
|
-
console.log(`\u6267\u884C\u62A5\u9519, \u7B49\u5F85${delay}`);
|
|
29
|
-
await wait(ms(delay));
|
|
30
|
-
retryCount++;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
new XRunner({
|
|
36
|
-
configFile: "./temp/test.json",
|
|
37
|
-
delay: "1s",
|
|
38
|
-
questions: [
|
|
39
|
-
{
|
|
40
|
-
name: "perCount",
|
|
41
|
-
message: "\u6BCF\u8F6E\u5904\u7406\u6761\u6570",
|
|
42
|
-
type: "number",
|
|
43
|
-
default: 500
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
name: "concurrencyCount",
|
|
47
|
-
message: "\u6BCF\u8F6E\u5E76\u884C\u5904\u7406\u6761\u6570",
|
|
48
|
-
type: "number",
|
|
49
|
-
default: 1
|
|
50
|
-
}
|
|
51
|
-
],
|
|
52
|
-
run: (config) => {
|
|
53
|
-
console.log(config);
|
|
54
|
-
}
|
|
55
|
-
}).run();
|
|
56
|
-
export {
|
|
57
|
-
XRunner
|
|
58
|
-
};
|
package/task.d.ts
DELETED
package/task.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { consola as log } from "consola";
|
|
2
|
-
import { asyncExitHook, gracefulExit } from "exit-hook";
|
|
3
|
-
class XTask {
|
|
4
|
-
log = log;
|
|
5
|
-
async dispose() {
|
|
6
|
-
}
|
|
7
|
-
run() {
|
|
8
|
-
asyncExitHook(() => this.dispose(), {
|
|
9
|
-
wait: 3e3
|
|
10
|
-
});
|
|
11
|
-
return this.start().then(() => {
|
|
12
|
-
gracefulExit(0);
|
|
13
|
-
}).catch((err) => {
|
|
14
|
-
log.error(err);
|
|
15
|
-
gracefulExit(1);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export {
|
|
20
|
-
XTask
|
|
21
|
-
};
|
package/text.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
export declare class XText {
|
|
2
|
-
private text;
|
|
3
|
-
/**
|
|
4
|
-
* 将字符串分割为字符数组。
|
|
5
|
-
*
|
|
6
|
-
* @see https://stackoverflow.com/a/71619350
|
|
7
|
-
*/
|
|
8
|
-
static split(text: string): string[];
|
|
9
|
-
/**
|
|
10
|
-
* 纯粹为了书写 HTML 有提示。
|
|
11
|
-
*/
|
|
12
|
-
static html(literals: any, ...interpolations: any): string;
|
|
13
|
-
/**
|
|
14
|
-
* 切割字符串。
|
|
15
|
-
*/
|
|
16
|
-
static cut(text: string, start: string, end: string): string;
|
|
17
|
-
/**
|
|
18
|
-
* 移除全部空白字符。
|
|
19
|
-
*/
|
|
20
|
-
static stripWhitespace(text: string): string;
|
|
21
|
-
/**
|
|
22
|
-
* 移除首尾空白字符。
|
|
23
|
-
*/
|
|
24
|
-
static stripMarginWhitespace(text: string): string;
|
|
25
|
-
/**
|
|
26
|
-
* HTML 实体编码。
|
|
27
|
-
*/
|
|
28
|
-
static encodeHtmlEntity(text: string): string;
|
|
29
|
-
/**
|
|
30
|
-
* HTML 实体解码。
|
|
31
|
-
*/
|
|
32
|
-
static decodeHtmlEntity(text: string): string;
|
|
33
|
-
/**
|
|
34
|
-
* 移除 HTML 代码。
|
|
35
|
-
*/
|
|
36
|
-
static stripHtml(text: string): string;
|
|
37
|
-
constructor(text: string);
|
|
38
|
-
/**
|
|
39
|
-
* 切割字符串。
|
|
40
|
-
*/
|
|
41
|
-
cut(start: string, end: string): this;
|
|
42
|
-
/**
|
|
43
|
-
* 移除全部空白字符。
|
|
44
|
-
*/
|
|
45
|
-
stripWhitespace(): this;
|
|
46
|
-
/**
|
|
47
|
-
* 移除首尾空白字符。
|
|
48
|
-
*/
|
|
49
|
-
stripMarginWhitespace(): this;
|
|
50
|
-
/**
|
|
51
|
-
* HTML 实体编码。
|
|
52
|
-
*/
|
|
53
|
-
encodeHtmlEntity(): this;
|
|
54
|
-
/**
|
|
55
|
-
* HTML 实体解码。
|
|
56
|
-
*/
|
|
57
|
-
decodeHtmlEntity(): this;
|
|
58
|
-
/**
|
|
59
|
-
* 移除 HTML 代码。
|
|
60
|
-
*/
|
|
61
|
-
stripHtml(): this;
|
|
62
|
-
toString(): string;
|
|
63
|
-
}
|
package/text.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
decode as decodeHtmlEntity,
|
|
3
|
-
encode as encodeHtmlEntity
|
|
4
|
-
} from "html-entities";
|
|
5
|
-
class XText {
|
|
6
|
-
constructor(text) {
|
|
7
|
-
this.text = text;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* 将字符串分割为字符数组。
|
|
11
|
-
*
|
|
12
|
-
* @see https://stackoverflow.com/a/71619350
|
|
13
|
-
*/
|
|
14
|
-
static split(text) {
|
|
15
|
-
return [...new Intl.Segmenter().segment(text)].map((x) => x.segment);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* 纯粹为了书写 HTML 有提示。
|
|
19
|
-
*/
|
|
20
|
-
static html(literals, ...interpolations) {
|
|
21
|
-
let result = "";
|
|
22
|
-
for (let i = 0; i < interpolations.length; i++) {
|
|
23
|
-
result += literals[i];
|
|
24
|
-
result += interpolations[i];
|
|
25
|
-
}
|
|
26
|
-
result += literals[literals.length - 1];
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 切割字符串。
|
|
31
|
-
*/
|
|
32
|
-
static cut(text, start, end) {
|
|
33
|
-
text = text.split(start)[1].split(end)[0];
|
|
34
|
-
return text;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 移除全部空白字符。
|
|
38
|
-
*/
|
|
39
|
-
static stripWhitespace(text) {
|
|
40
|
-
text = text.replace(/\s+/g, "");
|
|
41
|
-
return text;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* 移除首尾空白字符。
|
|
45
|
-
*/
|
|
46
|
-
static stripMarginWhitespace(text) {
|
|
47
|
-
text = text.trim();
|
|
48
|
-
return text;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* HTML 实体编码。
|
|
52
|
-
*/
|
|
53
|
-
static encodeHtmlEntity(text) {
|
|
54
|
-
text = encodeHtmlEntity(text);
|
|
55
|
-
return text;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* HTML 实体解码。
|
|
59
|
-
*/
|
|
60
|
-
static decodeHtmlEntity(text) {
|
|
61
|
-
text = decodeHtmlEntity(text);
|
|
62
|
-
return text;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* 移除 HTML 代码。
|
|
66
|
-
*/
|
|
67
|
-
static stripHtml(text) {
|
|
68
|
-
text = text.replace(/<script[\s>].+?<\/script>/g, "").replace(/<style[\s>].+?<\/style>/g, "").replace(/<(.+?)>/g, "");
|
|
69
|
-
return text;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* 切割字符串。
|
|
73
|
-
*/
|
|
74
|
-
cut(start, end) {
|
|
75
|
-
this.text = XText.cut(this.text, start, end);
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* 移除全部空白字符。
|
|
80
|
-
*/
|
|
81
|
-
stripWhitespace() {
|
|
82
|
-
this.text = XText.stripWhitespace(this.text);
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* 移除首尾空白字符。
|
|
87
|
-
*/
|
|
88
|
-
stripMarginWhitespace() {
|
|
89
|
-
this.text = XText.stripMarginWhitespace(this.text);
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* HTML 实体编码。
|
|
94
|
-
*/
|
|
95
|
-
encodeHtmlEntity() {
|
|
96
|
-
this.text = XText.encodeHtmlEntity(this.text);
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* HTML 实体解码。
|
|
101
|
-
*/
|
|
102
|
-
decodeHtmlEntity() {
|
|
103
|
-
this.text = XText.decodeHtmlEntity(this.text);
|
|
104
|
-
return this;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* 移除 HTML 代码。
|
|
108
|
-
*/
|
|
109
|
-
stripHtml() {
|
|
110
|
-
this.text = XText.stripHtml(this.text);
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
toString() {
|
|
114
|
-
return this.text;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
export {
|
|
118
|
-
XText
|
|
119
|
-
};
|
package/unicode/lib_unicode.d.ts
DELETED