Skip to content

Instantly share code, notes, and snippets.

@biuuu
Last active May 10, 2021 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biuuu/7c5a1afc9ccb3880de16618895093597 to your computer and use it in GitHub Desktop.
Save biuuu/7c5a1afc9ccb3880de16618895093597 to your computer and use it in GitHub Desktop.
闪耀色彩剧情转CSV
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