UNPKG

rumble-charts

Version:

Truly declarative React charts components

83 lines (80 loc) 2.54 kB
import { isString } from './isString.js'; import { isUndefined } from './isUndefined.js'; import { normalizeNumber } from './normalizeNumber.js'; import { isNumber } from './isNumber.js'; var TOP = 'top'; var BOTTOM = 'bottom'; var MIDDLE = 'middle'; var LEFT = 'left'; var RIGHT = 'right'; var CENTER = 'center'; var HORIZONTAL = [TOP, BOTTOM, MIDDLE]; var VERTICAL = [LEFT, RIGHT, CENTER]; function getCoords(position, layerWidth, layerHeight, width, height) { if (layerWidth === void 0) { layerWidth = 0; } if (layerHeight === void 0) { layerHeight = 0; } if (width === void 0) { width = 0; } if (height === void 0) { height = 0; } if (isString(position)) { position = position.trim().split(' ').map(function (value) { return value.trim(); }); } if (Array.isArray(position)) { var cleanPosition = position.map(function (pos) { return isString(pos) ? pos.trim().toLowerCase() : pos; }); var _x = cleanPosition[0], _y = cleanPosition[1]; if (isString(cleanPosition[0]) && HORIZONTAL.indexOf(cleanPosition[0]) !== -1) { _y = cleanPosition[0]; } if (isString(cleanPosition[1]) && VERTICAL.indexOf(cleanPosition[1]) !== -1) { _x = cleanPosition[1]; } var result = { x: 0, y: 0 }; if (isString(_x)) { if (_x === LEFT) { result.x = 0; } else if (_x === RIGHT) { result.x = layerWidth - width; } else if (_x === CENTER) { result.x = (layerWidth - width) / 2; } else { result.x = normalizeNumber(_x, layerWidth); } } else if (isNumber(_x)) { result.x = _x; } else if (isUndefined(_x)) { result.x = 0; } if (isString(_y)) { if (_y === TOP) { result.y = 0; } else if (_y === BOTTOM) { result.y = layerHeight - height; } else if (_y === MIDDLE) { result.y = (layerHeight - height) / 2; } else { result.y = normalizeNumber(_y, layerHeight); } } else if (isNumber(_y)) { result.y = _y; } else if (isUndefined(_y)) { result.y = 0; } return result; } else { return { x: 0, y: 0 }; } } export { getCoords };