fixed.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Column, ColumnHelpers } from 'mol-data/db'
  7. import { trimStr, Tokens } from '../tokenizer'
  8. import { parseIntSkipLeadingWhitespace, parseFloatSkipLeadingWhitespace } from '../number-parser'
  9. export default function FixedColumnProvider(lines: Tokens) {
  10. return function<T extends Column.Schema>(offset: number, width: number, type: T) {
  11. return FixedColumn(lines, offset, width, type);
  12. }
  13. }
  14. export function FixedColumn<T extends Column.Schema>(lines: Tokens, offset: number, width: number, schema: T): Column<T['T']> {
  15. const { data, indices, count: rowCount } = lines;
  16. const { valueType: type } = schema;
  17. const value: Column<T['T']>['value'] = type === 'str' ? row => {
  18. let s = indices[2 * row] + offset, le = indices[2 * row + 1];
  19. if (s >= le) return '';
  20. let e = s + width;
  21. if (e > le) e = le;
  22. return trimStr(data, s, e);
  23. } : type === 'int' ? row => {
  24. const s = indices[2 * row] + offset;
  25. if (s > indices[2 * row + 1]) return 0;
  26. return parseIntSkipLeadingWhitespace(data, s, s + width);
  27. } : row => {
  28. const s = indices[2 * row] + offset;
  29. if (s > indices[2 * row + 1]) return 0;
  30. return parseFloatSkipLeadingWhitespace(data, s, s + width);
  31. };
  32. return {
  33. schema: schema,
  34. __array: void 0,
  35. isDefined: true,
  36. rowCount,
  37. value,
  38. valueKind: row => Column.ValueKind.Present,
  39. toArray: params => ColumnHelpers.createAndFillArray(rowCount, value, params),
  40. areValuesEqual: (rowA, rowB) => value(rowA) === value(rowB)
  41. };
  42. }