util.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Iterator } from 'mol-data'
  7. import { Field, Category } from '../encoder';
  8. export function getFieldDigitCount(field: Field) {
  9. if (field.defaultFormat && typeof field.defaultFormat.digitCount !== 'undefined') return Math.max(0, Math.min(field.defaultFormat.digitCount, 16));
  10. return 6;
  11. }
  12. export function getIncludedFields(category: Category.Instance) {
  13. return category.fields.some(f => !!f.shouldInclude)
  14. ? category.fields.filter(f => !f.shouldInclude || category.source.some(src => f.shouldInclude!(src.data)))
  15. : category.fields;
  16. }
  17. export interface CategoryInstanceData<Ctx = any> {
  18. instance: Category.Instance<Ctx>,
  19. rowCount: number,
  20. source: { data: any, keys: () => Iterator<any>, rowCount: number }[]
  21. }
  22. export function getCategoryInstanceData<Ctx>(category: Category<Ctx>, ctx?: Ctx): CategoryInstanceData<Ctx> {
  23. const instance = category.instance(ctx as any);
  24. let sources = instance.source.filter(s => s.rowCount > 0);
  25. if (!sources.length) return { instance, rowCount: 0, source: [] };
  26. const rowCount = sources.reduce((a, c) => a + c.rowCount, 0);
  27. const source = sources.map(c => ({
  28. data: c.data,
  29. keys: () => c.keys ? c.keys() : Iterator.Range(0, c.rowCount - 1),
  30. rowCount: c.rowCount
  31. }));
  32. return { instance, rowCount, source };
  33. }