-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
99 lines (83 loc) · 2.06 KB
/
main.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
$('button#execute').click(async () => {
const fileRegex = /[<>:"\/\\|?* .]/g
$('button#execute').prop('disabled', true)
setTimeout(() => {
$('button#execute').prop('disabled', false)
}, 250)
$('div.list').empty()
const fileList = Array.from(
$('input#csv-file').prop('files')
)
if (fileList.length === 0) {
alert('请选择文件!')
return
}
const columnInput = $(
'input[name="column"]:checked'
).val()
if (columnInput === undefined) {
alert('请选择列!')
return
}
const columnTrim = (data) => {
switch (columnInput) {
case '789':
return data.slice(6, 9)
case '6':
return data.slice(5, 6)
}
}
const parser = (file) => {
const condition = (data) =>
columnTrim(data).every(
(item) => Number.parseInt(item) >= 0
)
const stepFn = (results, parser) => {
// skip the first 3 lines
if (lineCount > 3 && !condition(results.data)) {
$('<p></p>')
.text(`line ${lineCount}: ${results.data}`)
.appendTo(
`details.${file.name.replace(fileRegex, '-')}`
)
}
lineCount += 1
}
const errorFn = (error) => {
$('<p></p>')
.text(`line ${lineCount}: ${error}`)
.addClass('error')
.appendTo(
`details.${file.name.replace(fileRegex, '-')}`
)
lineCount += 1
}
const completeFn = () => {
lineCount = 1
}
let lineCount = 1
Papa.parse(file, {
delimiter: ',',
dynamicTyping: true,
encoding: 'GBK',
step: stepFn,
complete: completeFn,
error: errorFn,
beforeFirstChunk: undefined,
transform: undefined
})
}
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i]
var detail = $('<details></details>').addClass(
`${file.name.replace(fileRegex, '-')}`
)
detail.append(
$('<summary></summary>')
.text(file.name)
.addClass(file.name.replace(fileRegex, '-'))
)
detail.appendTo('div.list')
parser(file)
}
})