wb-table/js/index.js

104 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-03-11 12:13:08 +08:00
/**
*
* @author yutent<yutent.io@gmail.com>
* @date 2022/03/11 12:03:59
*/
import '//unpkg.yutent.top/anot/dist/anot.js'
2022-03-11 15:47:27 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/layer/index.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/input.js'
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/button.js'
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/radio.js'
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/switch.js'
import fetch from '//unpkg.yutent.top/@bytedo/fetch/dist/index.js'
2022-03-11 15:47:27 +08:00
import { Enum } from './lib/core.js'
const WB_CODE_NAME = new Enum({ 1: '一级简码', 2: '二级简码', 3: '三级简码', 4: '四级简码' })
const WB_TABLE = new Enum()
2022-03-11 12:13:08 +08:00
Anot.hideProperty(WB_TABLE, 'length', 0)
Anot({
$id: 'app',
state: {
2022-03-11 15:47:27 +08:00
single: 0,
words: 0,
result: '',
filter: {
txt: '',
2022-03-11 15:56:24 +08:00
table: '86'
2022-03-11 15:47:27 +08:00
}
2022-03-11 12:13:08 +08:00
},
mounted() {
2022-03-11 12:15:23 +08:00
fetch('./data/table.txt')
2022-03-11 12:13:08 +08:00
.then(r => r.text())
.then(r => {
2022-03-11 16:48:07 +08:00
//
2022-03-11 12:13:08 +08:00
r.split('\n').forEach(it => {
it = it
.trim()
.split(' ')
.map(_ => _.trim())
let k = it.shift()
if (k) {
2022-03-11 15:47:27 +08:00
WB_TABLE.add(k, it)
2022-03-11 12:13:08 +08:00
}
})
this.single = WB_TABLE.length
})
2022-03-11 15:47:27 +08:00
},
methods: {
search() {
var params = { ...this.filter }
2022-03-11 15:56:24 +08:00
var reverse = false
2022-03-11 15:47:27 +08:00
var res
2022-03-11 15:56:24 +08:00
params.txt = params.txt.trim().toLowerCase()
2022-03-11 15:47:27 +08:00
2022-03-11 15:56:24 +08:00
reverse = /^[a-z]{1,4}$/.test(params.txt)
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (!reverse) {
params.txt = params.txt.replace(/[\sa-z]/g, '')
}
2022-03-11 16:48:07 +08:00
if (reverse || params.txt.length === 1) {
2022-03-11 16:23:29 +08:00
res = [WB_TABLE.get(params.txt)]
2022-03-11 16:48:07 +08:00
} else {
res = params.txt.split('').map(t => WB_TABLE.get(t))
2022-03-11 16:23:29 +08:00
}
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (reverse) {
if (res[0]) {
res = `${params.txt} 】👉\t${res[0].join('\t\t')}`
2022-03-11 15:56:24 +08:00
} else {
2022-03-11 16:23:29 +08:00
res = `${
params.txt
} 👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库`
2022-03-11 15:56:24 +08:00
}
2022-03-11 15:47:27 +08:00
} else {
2022-03-11 16:23:29 +08:00
res = res
.map((it, i) => {
if (it) {
return `${params.txt[i]} 】👉\t${it
.map(t => `${WB_CODE_NAME.get(t.length)}: ${t.toUpperCase()}`)
.join('\t\t')}`
} else {
return `${
params.txt[i]
} 👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库`
}
})
.join('\n')
2022-03-11 15:47:27 +08:00
}
2022-03-11 16:23:29 +08:00
2022-03-11 16:48:07 +08:00
this.result = `查询耗时: ${t1}ms\n查询结果: \n${res}`
2022-03-11 15:47:27 +08:00
}
2022-03-11 12:13:08 +08:00
}
})