sys-shim 0.0.1-23 → 0.0.1-6

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/src/util.js DELETED
@@ -1,395 +0,0 @@
1
- import { DeepProxy } from './proxy-deep.js'
2
- const KEYS_MAP = new Map()
3
- export function get([key]) {
4
- return KEYS_MAP.get(key) || key
5
- }
6
-
7
- /**
8
- * 获取 `/**` 中 `*` 号的数量
9
- * @param {*} inputStr
10
- * @returns
11
- */
12
- export function getCodeLine(inputStr) {
13
- // 使用正则表达式匹配以 `/` 开头,后面跟着连续的 `*` 号的模式,使用贪婪匹配
14
- const pattern = /\/\*{1,}/g
15
- const matches = inputStr.match(pattern)
16
-
17
- // 如果没有匹配到任何 '/**',返回0或者其他默认值
18
- if (!matches) {
19
- return 0
20
- }
21
-
22
- // 初始化最大 '**' 数量和对应的 '/**' 字符串
23
- let maxAsterisks = 0
24
- let maxAsterisksMatch = ``
25
-
26
- // 遍历所有匹配结果,找出包含最多 '*' 号的那一个
27
- for (const match of matches) {
28
- const asterisksCount = match.length - 1 // 减去前面的 '/'
29
- if (asterisksCount > maxAsterisks) {
30
- maxAsterisks = asterisksCount
31
- maxAsterisksMatch = match
32
- }
33
- }
34
-
35
- // 返回最多的 '*' 号数量
36
- return maxAsterisks
37
- }
38
-
39
- export const sleep = (time = 1e3) => new Promise(resolve => setTimeout(resolve, time))
40
- export function deepProxy({
41
- keys = [`then`, `catch`],
42
- cb = (records) => {
43
- return new Promise(async (res, rej) => {
44
- // 模拟异步操作
45
- setTimeout(() => {
46
- res(records)
47
- }, Math.random() * 1000)
48
- })
49
- },
50
- } = {}) {
51
- keys.forEach(key => {
52
- let _val = Symbol(key)
53
- KEYS_MAP.set(key, _val)
54
- KEYS_MAP.set(_val, key)
55
- })
56
- function getRecords(context = {}) {
57
- return context.records || []
58
- }
59
- // 代理处理程序
60
- const handler = {
61
- get(target, key, receiver) {
62
- let records = getRecords(this)
63
- if (keys.includes(key)) {
64
- let promise = cb(records)
65
- records.hackRun = true // 已运行过
66
- return promise[key].bind(promise)
67
- } else {
68
- records.push({ type: `get`, key: get([key]) })
69
- let newTarget = function () { }
70
- return this.nest(newTarget, { userData: { records } })
71
- }
72
- },
73
- apply(target, thisArg, args) {
74
- let records = getRecords(this)
75
- setTimeout(() => {
76
- let recordsEnd = getRecords(this)
77
- !recordsEnd.hackRun && (cb(recordsEnd), recordsEnd.hackRun = true)
78
- }, 0)
79
- const key = records[records.length - 1].key
80
- records[records.length - 1] = { type: `apply`, key, arg: args }
81
- let newTarget = function () { }
82
- return this.nest(newTarget, { userData: { records } })
83
- },
84
- construct(target, args) {
85
- let records = getRecords(this)
86
- records.push({ type: `construct`, arg: args })
87
- let newTarget = function () { }
88
- return this.nest(newTarget, { userData: { records } })
89
- },
90
- defineProperty(target, key, args) {
91
- let records = getRecords(this)
92
- records.push({ type: `defineProperty`, key, arg: args })
93
- let newTarget = function () { }
94
- return this.nest(newTarget, { userData: { records } })
95
- },
96
- deleteProperty(target, key) {
97
- let records = getRecords(this)
98
- records.push({ type: `deleteProperty`, key })
99
- let newTarget = function () { }
100
- return this.nest(newTarget, { userData: { records } })
101
-
102
- },
103
- set(target, key, value) {
104
- let records = getRecords(this)
105
- records.push({ type: `set`, key, arg: value })
106
- let newTarget = function () { }
107
- return this.nest(newTarget, { userData: { records } })
108
- },
109
- getOwnPropertyDescriptor(target, prop) {
110
- let records = getRecords(this)
111
- records.push({ type: `getOwnPropertyDescriptor`, key: prop })
112
- let newTarget = function () { }
113
- return { configurable: true, enumerable: true, value: this.nest(newTarget) }
114
- },
115
- getPrototypeOf(target) {
116
- let records = getRecords(this)
117
- records.push({ type: `getPrototypeOf` })
118
- let newTarget = function () { }
119
- return this.nest(newTarget, { userData: { records } })
120
- },
121
- has(target, prop) {
122
- let records = getRecords(this)
123
- records.push({ type: `has`, key: prop })
124
- return true
125
- },
126
- isExtensible(target) {
127
- let records = getRecords(this)
128
- records.push({ type: `isExtensible` })
129
- return true
130
- },
131
- setPrototypeOf(target, prototype) {
132
- let records = getRecords(this)
133
- records.push({ type: `setPrototypeOf`, arg: prototype })
134
- return true
135
- },
136
- ownKeys(target) {
137
- let records = getRecords(this)
138
- records.push({ type: `ownKeys` })
139
- return Reflect.ownKeys(target)
140
- },
141
- preventExtensions(target) {
142
- let records = getRecords(this)
143
- records.push({ type: `preventExtensions` })
144
- Object.preventExtensions(target)
145
- return true
146
- },
147
- }
148
-
149
- // 返回初始对象的代理
150
- return new DeepProxy({}, handler)
151
- }
152
-
153
- export function binaryArrayToBuffer(binaryArray) {
154
- let buffer = new ArrayBuffer(binaryArray.length)
155
- let view = new Uint8Array(buffer)
156
- for (let i = 0; i < binaryArray.length; i++) {
157
- view[i] = binaryArray[i]
158
- }
159
- return buffer
160
- }
161
-
162
- /**
163
- * 删除左边空格
164
- * @param {*} str
165
- * @returns
166
- */
167
- export function removeLeft(str) {
168
- const lines = str.split(`\n`)
169
- // 获取应该删除的空白符数量
170
- const minSpaceNum = lines.filter(item => item.trim())
171
- .map(item => item.match(/(^\s+)?/)[0].length)
172
- .sort((a, b) => a - b)[0]
173
- // 删除空白符
174
- const newStr = lines
175
- .map(item => item.slice(minSpaceNum))
176
- .join(`\n`)
177
- return newStr
178
- }
179
-
180
- /**
181
- * 简单的模板功能
182
- * @param {*} template
183
- * @param {*} data
184
- * @returns
185
- */
186
- export function simpleTemplate(template, data) {
187
- return template.replace(/#\{(\w+)\}/g, (match, key, pos) => {
188
- const lineStr = getLineContainingChar(template, pos)
189
- const spaceNum = getMinSpaceNum(lineStr)
190
- const val = data[key] || ``
191
- const newVal = addSpace(val, {num: spaceNum})
192
- return newVal
193
- })
194
- }
195
-
196
- /**
197
- * 返回字符所在位置的整行文本
198
- * @param {*} text
199
- * @param {*} charPosition
200
- * @returns
201
- */
202
- export function getLineContainingChar(text, charPosition) {
203
- // 将多行文本分割成行数组
204
- const lines = text.split(`\n`)
205
-
206
- // 遍历行数组,找到包含给定字符位置的行
207
- for (let i = 0; i < lines.length; i++) {
208
- // 计算当前行的字符位置
209
- const lineLength = lines[i].length
210
- if (charPosition < lineLength) {
211
- // 如果字符位置在当前行内,返回该行
212
- return lines[i]
213
- } else {
214
- // 否则,减去当前行的长度,继续检查下一行
215
- charPosition -= lineLength + 1 // +1 是为了减去换行符
216
- }
217
- }
218
-
219
- // 如果没有找到包含给定字符位置的行,返回null或适当的错误信息
220
- return null
221
- }
222
-
223
- /**
224
- * 获取最小空白符数量
225
- * @param {*} str
226
- * @returns
227
- */
228
- export function getMinSpaceNum(str) {
229
- const lines = str.split(`\n`)
230
- const minSpaceNum = lines.filter(item => item.trim())
231
- .map(item => item.match(/(^\s+)?/)[0].length)
232
- .sort((a, b) => a - b)[0]
233
- return minSpaceNum
234
- }
235
-
236
- /**
237
- * 添加空白符
238
- * @param {*} str
239
- * @param {*} opt
240
- * @returns
241
- */
242
- export function addSpace(str, opt = {}) {
243
- opt = {
244
- num: 0, // 字符符数量
245
- space: ` `,
246
- skip: 0, // 要跳过设置的索引
247
- ...opt,
248
- }
249
- const lines = str.split(`\n`)
250
- const newStr = lines
251
- .map((item, index) => index <= opt.skip ? item : opt.space.repeat(opt.num) + item)
252
- .join(`\n`)
253
- return newStr
254
- }
255
-
256
- /**
257
- * 获取 uuid
258
- * @returns
259
- */
260
- export function getUuid () {
261
- if (typeof crypto === `object`) {
262
- if (typeof crypto.randomUUID === `function`) {
263
- return crypto.randomUUID()
264
- }
265
- if (typeof crypto.getRandomValues === `function` && typeof Uint8Array === `function`) {
266
- const callback = (c) => {
267
- const num = Number(c)
268
- return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16)
269
- }
270
- return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, callback)
271
- }
272
- }
273
- let timestamp = new Date().getTime()
274
- let perforNow = (typeof performance !== `undefined` && performance.now && performance.now() * 1000) || 0
275
- return `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, (c) => {
276
- let random = Math.random() * 16
277
- if (timestamp > 0) {
278
- random = (timestamp + random) % 16 | 0
279
- timestamp = Math.floor(timestamp / 16)
280
- } else {
281
- random = (perforNow + random) % 16 | 0
282
- perforNow = Math.floor(perforNow / 16)
283
- }
284
- return (c === `x` ? random : (random & 0x3) | 0x8).toString(16)
285
- })
286
- }
287
-
288
- /**
289
- * 获取 uuid
290
- * @returns
291
- */
292
- export function getCodeId () {
293
- return `c_${getUuid()}`.replace(/[_-]/g, ``)
294
- }
295
-
296
- export function isUTF8MultiByteStart(byte) {
297
- // 如果字节的高位为11,则是多字节字符的起始字节
298
- return (byte & 0xC0) === 0xC0
299
- }
300
-
301
- export function isUTF8MultiByteContinuation(byte) {
302
- // 如果字节的高位为10,则是多字节字符的延续字节
303
- return (byte & 0xC0) === 0x80
304
- }
305
-
306
-
307
- /**
308
- * 根据字节长度分割字符串
309
- * @param {*} param0
310
- * @returns
311
- */
312
- export function sliceStringByBytes({lib, str, sliceLength}) {
313
- const uint8Array = lib.encoder.encode(str)
314
- let slices = []
315
- let start = 0
316
-
317
- while (start < uint8Array.length) {
318
- let end = start + sliceLength
319
- if (end > uint8Array.length) {
320
- end = uint8Array.length
321
- } else {
322
- // 确保不在多字节字符中间断开
323
- while (end > start && isUTF8MultiByteContinuation(uint8Array[end - 1])) {
324
- end--
325
- }
326
- // 如果我们在多字节字符的起始处中止,则再次前移
327
- if (end > start && isUTF8MultiByteStart(uint8Array[end - 1])) {
328
- end--
329
- }
330
- }
331
-
332
- const slice = uint8Array.subarray(start, end)
333
- slices.push(lib.decoder.decode(slice))
334
- start = end // 设置下次分片的起始位置
335
- }
336
-
337
- return slices
338
- }
339
-
340
- export function isType(data, type = undefined) { // 判断数据是否为 type, 或返回 type
341
- const dataType = Object.prototype.toString.call(data).match(/\s(.+)]/)[1].toLowerCase()
342
- return type ? (dataType === type.toLowerCase()) : dataType
343
- }
344
-
345
- /**
346
- * 判断是否为空值
347
- * @param {*} value 要判断的值
348
- */
349
- export function isEmpty(value) {
350
- return [NaN, null, undefined, ``, [], {}].some((emptyItem) =>
351
- typeof value === `string` && value
352
- ? false
353
- : JSON.stringify(value) === JSON.stringify(emptyItem),
354
- )
355
- }
356
-
357
- /**
358
- * 删除空值
359
- * @param {object} obj 要处理的数据
360
- */
361
- export function removeEmpty(obj) {
362
- return JSON.parse(JSON.stringify(obj), (key, value) => {
363
- if (isEmpty(value) === false && Array.isArray(value)) {
364
- value = value.filter((v) => !isEmpty(v))
365
- }
366
- return isEmpty(value) ? undefined : value
367
- })
368
- }
369
-
370
- /**
371
- * 函数缓存器,相同参数只会执行一次
372
- * @param {*} fn
373
- * @returns
374
- */
375
- export function memoize(fn) {
376
- const cache = new Map() // 使用Map来存储缓存结果
377
-
378
- function memoized(...args) {
379
- const key = JSON.stringify(args) // 将参数转换为字符串作为缓存的键
380
- if (cache.has(key)) {
381
- return cache.get(key) // 如果缓存中已存在,直接返回缓存的结果
382
- }
383
-
384
- const result = fn.apply(this, args) // 否则,调用函数并存储结果
385
- cache.set(key, result)
386
- return result
387
- }
388
-
389
- // 添加一个方法来清除缓存
390
- memoized.clearCache = function() {
391
- cache.clear()
392
- }
393
-
394
- return memoized
395
- }
@@ -1,8 +0,0 @@
1
- {
2
- "browserArguments": "--disable-web-security --allow-running-insecure-content",
3
- "form": {
4
- "right": 1300,
5
- "bottom": 800
6
- },
7
- "page": "#{page}"
8
- }
@@ -1,9 +0,0 @@
1
- document.addEventListener(`DOMContentLoaded`, async () => {
2
- new window.Sys().then(async main => {
3
- window.sys = main
4
- // 编程式设置窗口图标
5
- const tabIcon = `./favicon.ico`
6
- const [, exist] = await window.sys.native.io.exist(tabIcon)
7
- exist && await window.sys.native.win.image.setIcon(window.sys.hwnd, tabIcon)
8
- })
9
- })
@@ -1,6 +0,0 @@
1
- 直接通过 url 生成程序。
2
-
3
- ``` sh
4
- sys-shim pack --input [url]
5
- ```
6
-