-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
70 lines (58 loc) · 1.57 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
const ucwords = (string) => string.charAt(0).toUpperCase() + string.slice(1);
const selectDatatype = (fieldLineRaw) => {
const fieldLine = fieldLineRaw.toLowerCase();
if (
fieldLine.includes('varchar') ||
fieldLine.includes('text') ||
fieldLine.includes('char') ||
fieldLine.includes('datetime')
)
return 'String';
if (fieldLine.includes('decimal') || fieldLine.includes('float')) return 'Float';
if (fieldLine.includes('tinyint') || fieldLine.includes('float')) return 'Boolean';
if (
fieldLine.includes('int') ||
fieldLine.includes('bigint') ||
fieldLine.includes('numeric') ||
fieldLine.includes('real')
)
return 'Int';
};
const checkField = (lineRaw) => {
const line = lineRaw.toLowerCase();
const excludeKeywords = ['charset', 'constraint', 'foreign key', 'engine'];
const isBlacklisted = excludeKeywords.some((keyword) => line.includes(keyword));
if (isBlacklisted) {
return false;
}
const dataTypes = [
'int',
'varchar',
'char',
'numeric',
'bigint',
'real',
'tinyint',
'decimal',
'text',
'float',
'datetime',
];
const checkFields = dataTypes.map((it) => {
if (line.includes(it)) return true;
});
if (checkFields.includes(true)) return true;
return false;
};
const camelCase = (str) => {
return (str.slice(0, 1).toLowerCase() + str.slice(1))
.replace(/([-_ ]){1,}/g, ' ')
.split(/[-_ ]/)
.reduce((cur, acc) => {
return cur + acc[0].toUpperCase() + acc.substring(1);
});
};
const generateTableName = (str) => {
return ucwords(camelCase(str));
};
export { ucwords, selectDatatype, checkField, generateTableName };