本文整理匯總了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'