本文整理汇总了TypeScript中json2csv.Parser类的典型用法代码示例。如果您正苦于以下问题:TypeScript Parser类的具体用法?TypeScript Parser怎么用?TypeScript Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: writeCSVExport
writeCSVExport(opt: ExportOptions): string {
// Flatten data to only primitives.
const data = opt.data.map((d: any) => {
return Object.keys(d).reduce((prev: any, curr) => {
if (!isObject(d[curr]) && !isArray(d[curr])) {
prev[curr] = d[curr];
}
return prev;
}, {});
});
// Set fields if not given
const fields = opt.fields || Object.keys(opt.data[0]);
// Write data to client
const parser = new Parser({ fields, delimiter: exportDelimeter });
return parser.parse(data);
}
示例2: async
router.get('/export/:format', async (req, res) => {
const storage = botScopedStorage.get(req.params.botId)
const config = await bp.config.getModuleConfigForBot('qna', req.params.botId)
const data = await prepareExport(storage, { flat: true })
if (req.params.format === 'csv') {
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-disposition', `attachment; filename=qna_${moment().format('DD-MM-YYYY')}.csv`)
const categoryWrapper = storage.hasCategories() ? ['category'] : []
const parseOptions = {
fields: ['question', 'action', 'answer', 'answer2', ...categoryWrapper],
header: true
}
const json2csvParser = new Json2csvParser(parseOptions)
res.end(iconv.encode(json2csvParser.parse(data), config.exportCsvEncoding))
} else {
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-disposition', `attachment; filename=qna_${moment().format('DD-MM-YYYY')}.json`)
res.end(JSON.stringify(data))
}
})
示例3: parse
}
/**************
* Public API *
**************/
parse({});
parse([]);
parse({}, {});
interface Car {
car: string;
price: number;
}
new Parser();
const parser: Parser<ExampleObj> = new Parser({});
s = parser.parse({ str: '', num: 1, obj: {} });
const opts: json2csv.Options<Car> = {
fields: [{
label: 'Car Name',
value: 'car'
}, {
label: 'Price USD',
value: 'price'
}]
};
const json2csvParser = new Parser<Car>(opts);
parser.parse([]);
示例4: parse
interface ExampleObj {
str?: string;
num?: number;
obj?: object;
}
/**************
* Public API *
**************/
parse({});
parse([]);
parse({}, {});
new Parser();
const parser: Parser<ExampleObj> = new Parser({});
s = parser.parse({ str: '', num: 1, obj: {} });
parser.parse([]);
const transform: Transform<ExampleObj> = new Transform<ExampleObj>({ quote: '' });
const nodeTransform: NodeTransform = transform;
// Tests using examples from Readme
interface Car {
car: string;
price: number;
}
const opts: json2csv.Options<Car> = {
fields: [{
label: 'Car Name',
value: 'car'