all files / candela/util/ index.js

90.48% Statements 38/42
100% Branches 12/12
75% Functions 3/4
84.62% Lines 22/26
4 statements, 3 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                     22×                
import { keys, type, read as dlread } from 'datalib';
 
export function getElementSize (el) {
  const style = window.getComputedStyle(el, null);
  const width = window.parseInt(style.getPropertyValue('width'));
  const height = window.parseInt(style.getPropertyValue('height'));
 
  return {
    width,
    height
  };
}
 
export function minmax (data) {
  let range = {
    min: null,
    max: null
  };
 
  if (data.length > 0) {
    range.min = range.max = data[0];
 
    for (let val of data) {
      if (val < range.min) {
        range.min = val;
      }
 
      if (val > range.max) {
        range.max = val;
      }
    }
  }
 
  return range;
}
 
// Version of datalib.type.inferAll() that handles fields
// with nested periods
export function inferAll (data) {
  let fields = keys(data[0]);
  let types = {};
  for (let i = 0; i < fields.length; i += 1) {
    types[fields[i]] = type.infer(data, '[' + fields[i] + ']');
  }
  return types;
}
 
// Version of datalib.read() that uses our inferAll() to handle
// fields with nested periods
export function read (data) {
  data.__types__ = inferAll(data);
  dlread(data, {parse: data.__types__});
}