本文整理汇总了TypeScript中papaparse.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: parseAndNormalize
parseAndNormalize() {
let csv;
if (typeof document == "boolean") { // "boolean" is for false
let csvObj = Papa.parse(this.data, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
comments: "#",
});
csv = csvObj.data;
} else {
csv = Table.fromText(this.data);
csv.logger = this.logger;
}
this.data = null; // save RAM
this.log('rows after parse', csv.length);
csv = csv.trim();
// csv = csv.trimAll(); // prevents analyzeCSV from working
this.log('rows after trim', csv.length);
csv = this.analyzeCSV(csv);
this.log('rows after analyze', csv.length);
csv = this.normalizeCSV(csv);
this.log('rows after normalize', csv.length);
csv = this.convertDataTypes(csv);
this.log('rows after convertDataTypes', csv.length);
return csv;
}
示例2: error
options.fileReader.readText(fullFilePath, (err, data) => {
if (err) {
error(`fail "${filePath}" reading`, err);
return reject(new DdfCsvError(FILE_READING_ERROR, err, fullFilePath));
}
Papa.parse(stripBom(data), {
header: true,
skipEmptyLines: true,
dynamicTyping: (headerName) => {
// can't do dynamic typing without concept types loaded.
// concept properties are not parsed in first concept query
// reparsing of concepts resource is done in conceptLookup building
if (!options.conceptsLookup) {
return true;
}
// parsing to number/boolean based on concept type
const concept: any = options.conceptsLookup.get(headerName) || {};
return includes([ 'boolean', 'measure' ], concept.concept_type);
},
complete: result => {
debug(`finish reading "${filePath}"`);
resolve(result);
},
error: parseErr => {
error(`fail "${filePath}" parsing`, parseErr);
reject(new DdfCsvError(CSV_PARSING_ERROR, parseErr, filePath));
}
});
});
示例3: fromCSV
export function fromCSV(data: string, nameColIndex?: number, tagsColIndex?: number): Record[] {
const parsed = parse(data);
if (parsed.errors.length) {
throw new ImportError("invalid_csv");
}
return fromTable(parsed.data, nameColIndex, tagsColIndex);
}
示例4: dumpToCSV
public dumpToCSV(resultFile: string) {
const newResult = _.mapKeys(this.result as _.Dictionary<any>, (v, k) => {
if (k !== 'repoName') {
return `${requestTypeMapping.get(this.requestType)}_${k}`;
} else {
return 'repoName';
}
});
if (!fs.existsSync(resultFile)) {
console.log(papa.unparse([newResult]));
fs.writeFileSync(resultFile, papa.unparse([newResult]));
} else {
const file = fs.createReadStream(resultFile);
papa.parse(file, {
header: true,
complete: parsedResult => {
const originResults = parsedResult.data;
const index = originResults.findIndex(originResult => {
return originResult.repoName === newResult.repoName;
});
if (index === -1) {
originResults.push(newResult);
} else {
originResults[index] = { ...originResults[index], ...newResult };
}
fs.writeFileSync(resultFile, papa.unparse(originResults));
},
});
}
}
示例5: trimAndRemoveQuotes
export const csvToMap = (csv: string): MapResult => {
let errors = []
const trimmed = _.trimEnd(csv, '\n')
const parsedTVS = Papa.parse(trimmed)
const templateValuesData: string[][] = _.get(parsedTVS, 'data', [[]])
if (templateValuesData.length === 0) {
return {values: {}, errors}
}
const keys = new Set<string>()
let values = {}
for (const arr of templateValuesData) {
if (arr.length === 2 || (arr.length === 3 && arr[2] === '')) {
const key = trimAndRemoveQuotes(arr[0])
const value = trimAndRemoveQuotes(arr[1])
if (!keys.has(key) && key !== '') {
values[key] = value
keys.add(key)
}
} else {
errors = [...errors, arr[0]]
}
}
return {values, errors}
}
示例6: readFile
readFile(file: File): void {
this._transactions = [];
let result = Papa.parse(file, {
delimiter: ";",
header: true,
skipEmptyLines: true,
encoding: "utf-8",
complete: (result) => {
// it is a german localized file format:
// Date format: dd.mm.yyyy
// Number format: xxx,xx
for (let row of result.data) {
var tvm = new TransactionViewModel();
let timeStamp = this.dataParser.parseTimeStamp(row["Wertstellung"], "DD.MM.YYYY");
tvm = this.assignTimeStamp(tvm, timeStamp, false);
tvm.note = row["Buchungstext"];
tvm.value = this.dataParser.parseNumber(row["Betrag"]);
tvm.personId = this.dbValueProvider.getPersonIdFromName(row["Buchungstext"]);
tvm.currencyAccountId = this.dbValueProvider.getCurrencyAccountIdFromName("Konto", "EUR");
this.addRawData(tvm, row, result.meta.fields);
this._transactions.push(tvm);
}
}
});
}
示例7: fromLastPass
export function fromLastPass(data: string): Record[] {
let records = parse(data).data
// Remove first row as it only contains field names
.slice(1)
// Filter out empty rows
.filter(row => row.length > 1)
.map(lpParseRow);
return records;
}
示例8: async
return new Promise<BindableMoneyTransaction[]>((resolve) => {
const onComplete = async (results: ParseResult) => {
const repository = await ImportFacade.importToDb(results.data);
resolve(await repository.pullAll());
};
papaparse.parse(csv, {
header : true,
complete: onComplete
} as ParseConfig);
});
示例9:
export const parseResponseError = (response: string): FluxTable[] => {
const data = Papa.parse(response.trim()).data as string[][]
return [
{
id: uuid.v4(),
name: 'Error',
result: '',
groupKey: {},
dataTypes: {},
data,
},
]
}
示例10: parseString
parseString(csvString: string): CoreColumnTable {
let result: PapaParse.ParseResult = Papa.parse(csvString, this.options);
let numRows: number = result.data.length;
let fields: string[] = [];
let attrVectors: any[][] = [];
if (result.meta.fields) {
fields = result.meta.fields;
// Create attribute vectors
for (let c: number = 0; c < fields.length; ++c) {
let vector: any[] = [];
for (let r: number = 0; r < numRows; ++r) {
vector.push(result.data[r][fields[c]]);
}
attrVectors.push(vector);
}
} else {
// Find the number of columns needed for all rows
let numColumns: number = 0;
for (let r: number = 0; r < numRows; ++r) {
numColumns = Math.max(numColumns, result.data[r].length);
}
// Initialize fields
for (let c: number = 0; c < numColumns; ++c) {
fields.push('Column ' + (c + 1));
}
// Fill attribute vectors
for (let c: number = 0; c < numColumns; ++c) {
let vector: any[] = [];
for (let r: number = 0; r < numRows; ++r) {
vector.push(result.data[r][c]);
}
attrVectors.push(vector);
}
}
// Create the table
let table: CoreColumnTable = new CoreColumnTable({
fields: fields,
columns: attrVectors
});
table.detectTypes(true);
return table;
}
示例11: constructor
constructor() {
var self = this;
Papa.parse('/data/food.csv', {
download: true,
header: true,
skipEmptyLines: true,
complete: function (results) {
self.indices = results.data.map(item => {
return {
id: parseInt(item['ID']),
name: item['Name'],
index: parseInt(item['Glycemic Index'])
};
});
}
});
}
示例12: return
return (modelCsv: string) => {
return Papa.parse(modelCsv).data as T[]
}
示例13:
const parsedChunks = chunks.map(c => Papa.parse(c).data)
示例14: Error
export const parseTables = (responseChunk: string): FluxTable[] => {
const lines = responseChunk.split('\n')
const annotationLines: string = lines
.filter(line => line.startsWith('#'))
.join('\n')
.trim()
const nonAnnotationLines: string = lines
.filter(line => !line.startsWith('#'))
.join('\n')
.trim()
if (_.isEmpty(annotationLines)) {
throw new Error('Unable to extract annotation data')
}
if (_.isEmpty(nonAnnotationLines)) {
// A response may be truncated on an arbitrary line. This guards against
// the case where a response is truncated on annotation data
return []
}
const nonAnnotationData = Papa.parse(nonAnnotationLines).data
const annotationData = Papa.parse(annotationLines).data
const headerRow = nonAnnotationData[0]
const tableColIndex = headerRow.findIndex(h => h === 'table')
const resultColIndex = headerRow.findIndex(h => h === 'result')
interface TableGroup {
[tableId: string]: string[]
}
// Group rows by their table id
const tablesData = Object.values(
_.groupBy<TableGroup[]>(
nonAnnotationData.slice(1),
row => row[tableColIndex]
)
)
const groupRow = annotationData.find(row => row[0] === '#group')
const defaultsRow = annotationData.find(row => row[0] === '#default')
const dataTypeRow = annotationData.find(row => row[0] === '#datatype')
// groupRow = ['#group', 'false', 'true', 'true', 'false']
const groupKeyIndices = groupRow.reduce((acc, value, i) => {
if (value === 'true') {
return [...acc, i]
}
return acc
}, [])
const tables = tablesData.map(tableData => {
const dataRow = _.get(tableData, '0', defaultsRow)
const result: string =
_.get(dataRow, resultColIndex, '') ||
_.get(defaultsRow, resultColIndex, '')
const groupKey = groupKeyIndices.reduce((acc, i) => {
return {...acc, [headerRow[i]]: _.get(dataRow, i, '')}
}, {})
const name = Object.entries(groupKey)
.filter(([k]) => !['_start', '_stop'].includes(k))
.map(([k, v]) => `${k}=${v}`)
.join(' ')
const dataTypes = dataTypeRow.reduce(
(acc, dataType, i) => ({
...acc,
[headerRow[i]]: dataType,
}),
{}
)
return {
id: uuid.v4(),
data: [[...headerRow], ...tableData],
name,
result,
groupKey,
dataTypes,
}
})
return tables
}
示例15: isCSV
export function isCSV(data: string): Boolean {
return parse(data).errors.length === 0;
}