(data: object[], type: DataFileType)
| 50 | }); |
| 51 | |
| 52 | export const loadDataArray = (data: object[], type: DataFileType) => new Promise<DataContent>((resolve, reject) => { |
| 53 | const parse = type === 'csv' || type === 'tsv'; |
| 54 | if (parse) { |
| 55 | //convert empty strings to null so that vega.inferType will get dates |
| 56 | data.forEach(row => { |
| 57 | for (const column in row) { |
| 58 | if (row[column] === '') { |
| 59 | row[column] = null; |
| 60 | } |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | const columns = SandDance.util. |
| 65 | getColumnsFromData(SandDance.VegaDeckGl.base.vega.inferTypes, data) |
| 66 | .filter(c => c.name && c.name.trim()) |
| 67 | .sort((a, b) => a.name.localeCompare(b.name)); |
| 68 | if (parse) { |
| 69 | const booleanColumns = columns.filter(c => c.type === 'boolean'); |
| 70 | const dateColumns = columns.filter(c => c.type === 'date'); |
| 71 | const numericColumns = columns.filter(c => c.type === 'integer' || c.type === 'number'); |
| 72 | data.forEach(obj => { |
| 73 | booleanColumns.forEach(c => { |
| 74 | obj[c.name] = ('' + obj[c.name]).toLowerCase() === 'true'; |
| 75 | }); |
| 76 | dateColumns.forEach(c => { |
| 77 | const input = obj[c.name]; |
| 78 | if (input !== null) { |
| 79 | const d = new Date(input) as DateWithSource; |
| 80 | d.input = input; |
| 81 | obj[c.name] = d; |
| 82 | } |
| 83 | }); |
| 84 | numericColumns.forEach(c => { |
| 85 | const n = parseFloat(obj[c.name]); |
| 86 | obj[c.name] = isNaN(n) ? null : n; |
| 87 | }); |
| 88 | }); |
| 89 | } |
| 90 | resolve({ data, columns }); |
| 91 | }); |
no test coverage detected