Last active
May 10, 2021 15:18
-
-
Save biuuu/7c5a1afc9ccb3880de16618895093597 to your computer and use it in GitHub Desktop.
闪耀色彩剧情转CSV
This file contains hidden or 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
import CSV from 'papaparse' | |
const trim = (str) => { | |
if (!str) return '' | |
let _str = str.replace(/[\u0020]+$/g, '') | |
return _str.replace(/^[\u0020]+/g, '') | |
} | |
const replaceWrap = (text) => { | |
if (typeof text === 'string') { | |
return text.replace(/\r?\n/g, '\\n').replace(/\r/g, '\\n') | |
} | |
return text | |
} | |
const dataToCSV = (data) => { | |
const list = [] | |
data.forEach(item => { | |
let text = trim(replaceWrap(item.text)) | |
if (text) { | |
list.push({ | |
id: item.id || '0000000000000', | |
name: item.speaker || '', | |
text, | |
trans: '' | |
}) | |
} else if (item.select) { | |
list.push({ | |
id: 'select', | |
name: '', | |
text: trim(replaceWrap(item.select)), | |
trans: '' | |
}) | |
} | |
}) | |
list.push({ | |
id: 'info', name, text: '', trans: '' | |
}) | |
list.push({ | |
id: '译者', name: '', text: '', trans: '' | |
}) | |
return CSV.unparse(list) | |
} | |
export default dataToCSV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment