-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (102 loc) · 3.93 KB
/
index.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
116
117
118
119
120
const nativeModule = require('bindings')('xlsx')
const privatePromiseWrap = (filepathString, doReadHeaders, headerTransformObj, columnsToDeleteArr, doPascalCase, pascalWords, stringColumns) => new Promise( (resolve, reject) => {
try {
if (!filepathString || typeof filepathString !== 'string') {
throw new Error('first argument must be a filepath in the form of a string')
}
nativeModule.readXlsxAsync(filepathString, doReadHeaders, headerTransformObj, columnsToDeleteArr, doPascalCase, pascalWords, stringColumns, (error, result) => {
if(error) {
reject(error)
} else if (result) {
resolve(result)
} else {
reject(new Error('unknown error occurred and the result is null'))
}
})
} catch (e) {
reject(e)
}
})
const readFile = (filepathString) => new Promise( async (resolve, reject) => {
try {
const columnsToDeleteArr = []
const result = await privatePromiseWrap(filepathString, false, null, columnsToDeleteArr, false, null, [])
resolve(result)
} catch (e) {
reject(e)
}
})
const readFileWithHeaders = (filepathString) => new Promise( async (resolve, reject) => {
try {
const columnsToDeleteArr = []
const result = await privatePromiseWrap(filepathString, true, null, columnsToDeleteArr, false, null, [])
resolve(result)
} catch (e) {
reject(e)
}
})
const readFileWithHeaderTransform = (filepathString, headerTransformObj) => new Promise( async (resolve, reject) => {
try {
const columnsToDeleteArr = []
const result = await privatePromiseWrap(filepathString, true, headerTransformObj, columnsToDeleteArr, false, null, [])
resolve(result)
} catch (e) {
reject(e)
}
})
const readFileWithHeaderTransformAndDelete = (filepathString, headerTransformFunc, columnsToDeleteArr) => new Promise( async (resolve, reject) => {
try {
const result = await privatePromiseWrap(filepathString, true, headerTransformFunc, columnsToDeleteArr, false, null, [])
resolve(result)
} catch (e) {
reject(e)
}
})
const readFileWithHeaderTransformDeleteAndPascalCase = (filepathString, headerTransformFunc, columnsToDeleteArr, pascalWords) => new Promise( async (resolve, reject) => {
try {
const result = await privatePromiseWrap(filepathString, true, headerTransformFunc, columnsToDeleteArr, true, pascalWords, [])
resolve(result)
} catch (e) {
reject(e)
}
})
const readFileWithHeaderTransformDeleteAndPascalCaseAndStringColumns = (filepathString, headerTransformFunc, columnsToDeleteArr, pascalWords, stringColumns) => new Promise( async (resolve, reject) => {
try {
const result = await privatePromiseWrap(filepathString, true, headerTransformFunc, columnsToDeleteArr, true, pascalWords, stringColumns)
resolve(result)
} catch (e) {
reject(e)
}
})
const replaceAll = (theString, findStr, replaceStr) => {
if(theString === null || typeof theString !== 'string')
{
throw new Error('theString must be a string')
}
if(findStr === null || typeof findStr !== 'string')
{
throw new Error('findStr must be a string')
}
if(replaceStr === null || typeof replaceStr !== 'string')
{
throw new Error('replaceStr must be a string')
}
return nativeModule.replaceAll(theString, findStr,replaceStr)
}
const toLower = (inputStr) => {
if(inputStr === null || typeof inputStr !== 'string')
{
throw new Error('inputStr must be a string')
}
return nativeModule.toLower(inputStr)
}
module.exports = {
readFile,
readFileWithHeaders,
readFileWithHeaderTransform,
readFileWithHeaderTransformAndDelete,
readFileWithHeaderTransformDeleteAndPascalCase,
readFileWithHeaderTransformDeleteAndPascalCaseAndStringColumns,
replaceAll,
toLower,
}