This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.js
115 lines (106 loc) · 2.74 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const fs = require('fs')
const Database = require('better-sqlite3')
exports.readFileToJson = path => {
let data = fs.readFileSync(path, 'utf-8')
return JSON.parse(data)
}
/**
* 写入文件
* @param content
* @param path
*/
exports.saveToFile = (content, path) => {
return fs.writeFileSync(path, content, 'utf-8')
}
/**
* 追加内容
* @param content
* @param path
*/
exports.addToFile = (content, path) => {
return fs.appendFileSync(path, content, 'utf-8')
}
/**
* mondayfirst/XXQG_TiKu格式题库转对象
* @param question
* @param answer
* @returns {{options: *[], answers: *[], title: *}}
*/
exports.mfToObj = (question, answer) => {
const a = []
const q = question.split('|')
const options = []
let key = ['title', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
for (let i = 1; i < q.length; i++) {
const option = {
value: key[i],
text: q[i]
}
options.push(option)
if (q[i] === answer) {
a.push(option)
}
}
return {
title: q[0],
options,
answers: a
}
}
/**
* 题库对象转Twelve-blog/Study_hamibot Json格式题库
*/
exports.objToTwJson = obj => {
let q = ''
for (let i = 0; i < obj.answers.length; i++) {
q += `${obj.answers[i].value}`
}
q += ` ${obj.title.replace(/ /g,'')}`
// q += ` ${obj.title.split(' ').join('')}`
const title_prefix = obj.title.slice(0, 4)
const option_text = obj.options.map(o => o.text)
if (title_prefix === '选择词语' || title_prefix === '选择正确') {
q += ',' + option_text.join(',') + '。'
}
q += ' ' + option_text.join('\t')
return q
}
/**
* 题库对象转Twelve-blog/Study_hamibot SQL格式题库
*/
exports.objToTwSQL = obj => {
let sql = ''
sql += `INSERT INTO 'tiku' VALUES ('${obj.title}`
for (let i = 0; i < obj.options.length; i++) {
sql += `|${obj.options[i].text}`
}
sql += `','${obj.answers[0].text}', NULL, '${obj.answers[0].value}', '${time()}');`
return sql
}
/**
* 格式化输出时间YYYY mm-DD hh:mm:ss
* @param d
* @param dash
* @param colon
* @returns {string}
*/
function time(d=new Date(), dash='-',colon=':') {
function pad(n) {
return n < 10 ? '0' + n : n
}
return d.getFullYear() + dash +
pad(d.getMonth() + 1) + dash +
pad(d.getDate()) + ' ' +
pad(d.getHours()) + colon +
pad(d.getMinutes()) + colon +
pad(d.getSeconds())
}
exports.initDb = async path => {
if (fs.existsSync(path)) {
console.info(`Delete old ${path} file.`)
fs.unlinkSync(path)
}
const db = new Database(path)
await db.exec(`CREATE TABLE 'tiku' ('question' CHAR(231), 'answer' CHAR(29), 'wrongAnswer' CHAR(23), 'option' CHAR(10), 'datetime' int)`)
return db
}