chparser.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { IAnalyticType, IDataType, IRow, ISemanticType } from "../interfaces";
  2. const DB_DATA_TYPE_TO_DATA_TYPE: { [key: string]: IDataType } = {
  3. 'Int8': 'integer',
  4. 'Int16': 'integer',
  5. 'Int32': 'integer',
  6. 'Int64': 'integer',
  7. 'UInt8': 'integer',
  8. 'UInt16': 'integer',
  9. 'UInt32': 'number',
  10. 'UInt64': 'number',
  11. 'Float32': 'number',
  12. 'Float64': 'number',
  13. 'BOOLEAN': 'boolean',
  14. 'String': 'string'
  15. }
  16. const DEFAULT_SEMANTIC_TYPE = {
  17. 'number': 'quantitative',
  18. 'integer': 'quantitative',
  19. 'boolean': 'nominal',
  20. 'date': 'temporal',
  21. 'string': 'nominal'
  22. } as const;
  23. const DEFAULT_ANALYTIC_TYPE = {
  24. 'number': 'measure',
  25. 'integer': 'measure',
  26. 'boolean': 'dimension',
  27. 'date': 'dimension',
  28. 'string': 'dimension'
  29. } as const;
  30. export function dbDataType2DataType (dbDataType: string): IDataType {
  31. return DB_DATA_TYPE_TO_DATA_TYPE[dbDataType] || 'string';
  32. }
  33. export function inferSemanticTypeFromDataType (dataType: IDataType): ISemanticType {
  34. return DEFAULT_SEMANTIC_TYPE[dataType]
  35. }
  36. export function inferAnalyticTypeFromDataType (dataType: IDataType): IAnalyticType {
  37. return DEFAULT_ANALYTIC_TYPE[dataType];
  38. }
  39. export function parseCell(rawValue: string, dataType: string) {
  40. switch (dataType) {
  41. case 'Int8':
  42. case 'Int16':
  43. case 'Int32':
  44. case 'Int64':
  45. // case 'Int128':
  46. // case 'Int256':
  47. case 'UInt8':
  48. case 'UInt16':
  49. case 'UInt32':
  50. case 'UInt64':
  51. return parseInt(rawValue);
  52. case 'Float32':
  53. case 'Float64':
  54. return Number(rawValue);
  55. default:
  56. return rawValue;
  57. }
  58. }
  59. export function parseTable (str: string, fields: {fid: string; dataType: string}[]): IRow[] {
  60. const rows: IRow[] = [];
  61. const rawRows = str.slice(0, -1).split('\n');
  62. for (let rawRow of rawRows) {
  63. const row: IRow = {};
  64. const rowValues = rawRow.split(/[\t]/);
  65. for (let i = 0; i < fields.length; i++) {
  66. row[fields[i].fid] = parseCell(rowValues[i], fields[i].dataType)
  67. }
  68. rows.push(row)
  69. }
  70. return rows;
  71. }