-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ba628d
commit 87eb510
Showing
11 changed files
with
122 additions
and
14,490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const format = {}; | ||
/* CSV and SQLite toArray and toString tools */ | ||
format.csv = {}; | ||
format.csv.toArray = content => { | ||
const rows = content.split('\r\n'); | ||
return rows.map(row => row | ||
// remove '"' | ||
.replace(/""([^"])/g, String.fromCharCode(0) + '$1').replace(/"""$/, String.fromCharCode(0) + '"') | ||
// remove ',' | ||
.replace(/,"([^"]+)"/, (a, b) => ',"' + b.replace(/,/g, String.fromCharCode(1)) + '"') | ||
.replace(/^"([^"]+)"/, (a, b) => '"' + b.replace(/,/g, String.fromCharCode(1)) + '"') | ||
// split | ||
.split(',') | ||
// remove closing '"' | ||
.map(s => s | ||
.replace(/"/g, '') | ||
// join '"' | ||
.split(String.fromCharCode(0)).join('"') | ||
// join ',' | ||
.split(String.fromCharCode(1)).join(',') | ||
).map(s => { | ||
if (s === '') { | ||
return ''; | ||
} | ||
else if (isNaN(s)) { | ||
return s; | ||
} | ||
else if (s.indexOf('.') !== -1) { | ||
return parseFloat(s); | ||
} | ||
return parseInt(s); | ||
}) | ||
); | ||
}; | ||
format.csv.toString = d2arr => { | ||
return d2arr.map(arr => arr.map(str => { | ||
if (isNaN(str)) { | ||
const quote = str.indexOf('"') !== -1 || str.indexOf(',') !== -1; | ||
str = str.replace(/"/g, '""'); | ||
if (quote) { | ||
return `"${str}"`; | ||
} | ||
else { | ||
return str; | ||
} | ||
} | ||
return str; | ||
}).join(',')).join('\r\n'); | ||
}; | ||
format.sql = {}; | ||
format.sql.toColumns = arr => { | ||
return arr.map(str => { | ||
if (str.indexOf(' ') !== -1 || str.indexOf('.') !== -1) { | ||
return `"${str.replace(/"/g, '""')}"`; | ||
} | ||
return str; | ||
}).join(', '); | ||
}; | ||
format.sql.toValues = arr => { | ||
return arr.map(str => { | ||
if (str === '' || str === null) { | ||
return 'null'; | ||
} | ||
else if (isNaN(str)) { | ||
return `"${str.replace(/"/g, '""')}"`; | ||
} | ||
else { | ||
return str; | ||
} | ||
}).join(', '); | ||
}; | ||
|
||
export default format; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.