UNPKG

1.59 kBJavaScriptView Raw
1import {
2 __,
3 divide,
4 multiply,
5 contains,
6 join,
7 view,
8 compose,
9 lensIndex,
10 isNil,
11 none,
12 complement,
13} from 'ramda';
14import { UNITS, SEPARATOR_VALUES } from '../const';
15
16export const separatorValueForUnit = unit => SEPARATOR_VALUES[unit];
17
18export const appendUnit = (value, unit) => join('', [value, unit]);
19
20export const unitIsRemOrEm = contains(__, [
21 UNITS.DIMENSIONS.EM,
22 UNITS.DIMENSIONS.REM,
23]);
24
25export const pxToRemOrEmValue = (value, baseFontSize) =>
26 divide(value, baseFontSize);
27
28export const remOrEmToPxValue = (value, baseFontSize) =>
29 multiply(value, baseFontSize);
30
31export const toDimensionOutput = (unit, baseFontSize, value) =>
32 appendUnit(
33 unitIsRemOrEm(unit) ? pxToRemOrEmValue(value, baseFontSize) : value,
34 unit
35 );
36
37export const elementsOfUnitedNumber = value => {
38 const captures = /^(-?\d+(?:.\d+)?)([a-z]+)?$/.exec(value);
39 if (none(complement(isNil), [captures, captures[1], captures[2]])) {
40 throw new Error(`You can't get the numeric portion of '${value}'`);
41 }
42 return [Number(captures[1]), captures[2]];
43};
44
45export const numericPartOfUnitedNumber = compose(
46 view(lensIndex(0)),
47 elementsOfUnitedNumber
48);
49
50export const unitPartOfUnitedNumber = compose(
51 view(lensIndex(1)),
52 elementsOfUnitedNumber
53);
54
55export const unitedDimensionToUnitlessPixelValue = (value, baseFontSize) => {
56 const [number, unit] = elementsOfUnitedNumber(value);
57 return unitIsRemOrEm(unit) ? remOrEmToPxValue(number, baseFontSize) : number;
58};
59
60export const unitedResolutionToUnitlessValue = value =>
61 numericPartOfUnitedNumber(value);