wb-table/js/lib/core.js

239 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-03-21 16:46:49 +08:00
/**
* 保存词组
*/
2022-03-20 21:30:49 +08:00
export function saveFile(bin, fileName) {
var link = document.createElement('a')
link.href = URL.createObjectURL(bin)
link.download = fileName
// 兼容火狐浏览器对于a链接click无效的问题将a链接作为子节点放置到body元素下
document.body.appendChild(link)
link.click()
// 下载后移除a链接
document.body.removeChild(link)
}
2022-03-21 16:46:49 +08:00
/**
* 生成五笔编码
*/
export function createCode(dict, word) {
if (/^[a-zA-Z]+/.test(word)) {
return word.match(/^([a-zA-Z]+)/)[1].toLowerCase()
}
switch (word.length) {
case 1: {
let c = dict.get(word)
return c?.shift()
}
case 2: {
let c1 = dict.get(word[0])?.pop()
let c2 = dict.get(word[1])?.pop()
if (c1 && c2) {
return c1.slice(0, 2) + c2.slice(0, 2)
} else {
console.error(word, '词组中存在未收录单字, 请到github上提交issues')
break
}
}
2022-03-21 18:23:47 +08:00
case 4: {
let c1 = dict.get(word[0])?.pop()
let c2 = dict.get(word[1])?.pop()
let c3 = dict.get(word[2])?.pop()
let c4 = dict.get(word[3])?.pop()
if (c1 && c2 && c3 && c4) {
return c1[0] + c2[0] + c3[0] + c4[0]
} else {
console.error(word, '词组中存在未收录单字, 请到github上提交issues')
break
}
}
2022-03-21 16:46:49 +08:00
default: {
let c1 = dict.get(word[0])?.pop()
let c2 = dict.get(word[1])?.pop()
let ce = dict.get(word[word.length - 1])?.pop()
if (c1 && c2 && ce) {
2022-03-21 18:23:47 +08:00
return c1[0] + c2[0] + ce.slice(0, 2)
2022-03-21 16:46:49 +08:00
} else {
console.error(word, '词组中存在未收录单字, 请到github上提交issues')
break
}
}
}
}
2022-03-18 19:04:12 +08:00
/**
* 增强版 String
*/
export class SString {
#origin = ''
#list = []
constructor(str = '') {
this.#origin = str
for (let it of str) {
this.#list.push(it)
2022-03-15 20:01:12 +08:00
}
2022-03-18 19:04:12 +08:00
}
2022-03-15 20:01:12 +08:00
2022-03-18 19:04:12 +08:00
get length() {
return this.#list.length
}
2022-03-21 16:46:49 +08:00
toLowerCase() {
return this.#origin.toLowerCase()
}
toUpperCase() {
return this.#origin.toUpperCase()
}
2022-03-18 19:04:12 +08:00
at(index = 0) {
if (this.length > 0) {
while (index < 0) {
index += this.length
2022-03-15 20:01:12 +08:00
}
2022-03-18 19:04:12 +08:00
return this.#list[index]
2022-03-15 20:01:12 +08:00
}
2022-03-18 19:04:12 +08:00
}
split() {
return this.#list
}
forEach(callback) {
for (let i in this.#list) {
if (callback(this.#list[i], i) === false) {
break
2022-03-17 19:22:46 +08:00
}
}
2022-03-18 19:04:12 +08:00
return this
}
toString() {
return this.#origin
}
2022-03-15 20:01:12 +08:00
}
2022-03-18 19:04:12 +08:00
/**
* 特殊版 Enum
*/
2022-03-11 15:47:27 +08:00
export class Enum {
#dict_k = Object.create(null)
#dict_v = Object.create(null)
length = 0
constructor(obj = {}) {
for (let k in obj) {
this.add(k, obj[k])
}
}
add(k, v) {
if (this.#dict_k[k]) {
var _v = this.#dict_k[k]
for (let t of _v) {
this.#dict_v[t] = this.#dict_v[t].filter(i => i !== k)
}
} else {
this.#dict_k[k] = Array.isArray(v) ? v : [v]
this.length++
}
if (Array.isArray(v)) {
for (let t of v) {
if (this.#dict_v[t]) {
this.#dict_v[t].push(k)
} else {
this.#dict_v[t] = [k]
}
}
} else {
this.#dict_v[v] = [k]
}
}
remove(k) {
var v = this.#dict_k[k]
if (v) {
delete this.#dict_k[k]
2022-03-16 01:43:17 +08:00
for (let t of v) {
delete this.#dict_v[t]
2022-03-11 15:47:27 +08:00
}
this.length--
}
}
get(k) {
2022-03-21 16:46:49 +08:00
// k += ''
k = k.toString()
2022-03-11 15:47:27 +08:00
if (this.#dict_k[k]) {
2022-03-21 16:46:49 +08:00
return [...this.#dict_k[k]]
2022-03-11 15:47:27 +08:00
} else if (this.#dict_v[k]) {
2022-03-21 16:46:49 +08:00
return [...this.#dict_v[k]]
2022-03-11 15:47:27 +08:00
}
}
2022-03-21 18:09:40 +08:00
clone() {
var data = JSON.parse(this.toJson())
return new Enum(data)
}
2022-03-20 21:30:49 +08:00
concat(...args) {
for (let li of args) {
li.forEach((it, k) => {
this.add(k, it)
})
}
}
2022-03-21 16:46:49 +08:00
slice(f, t) {
var res = []
var n = 0
if (t === void 0) {
t = this.length
}
if (f < 0) {
f += this.length
}
this.forEach((v, k) => {
if (n >= t) {
return false
}
if (n >= f) {
res.push({ [k]: [...v] })
}
n++
})
return res
}
2022-03-11 18:58:43 +08:00
forEach(callback, forV) {
var dict = forV ? this.#dict_v : this.#dict_k
for (let k in dict) {
2022-03-20 21:30:49 +08:00
if (callback(dict[k], k) === false) {
2022-03-11 15:47:27 +08:00
break
}
}
return this
}
2022-03-16 01:43:17 +08:00
toString(forV) {
var dict = forV ? this.#dict_v : this.#dict_k
var text = ''
for (let k in dict) {
text += `${k} ${dict[k].join(' ')}\n`
}
return text.trim()
}
toJson(forV) {
return JSON.stringify(forV ? this.#dict_v : this.#dict_k)
2022-03-11 15:47:27 +08:00
}
}