1 | import {
|
2 | __,
|
3 | divide,
|
4 | multiply,
|
5 | contains,
|
6 | join,
|
7 | view,
|
8 | compose,
|
9 | lensIndex,
|
10 | isNil,
|
11 | none,
|
12 | complement,
|
13 | } from 'ramda';
|
14 | import { UNITS, SEPARATOR_VALUES } from '../const';
|
15 |
|
16 | export const separatorValueForUnit = unit => SEPARATOR_VALUES[unit];
|
17 |
|
18 | export const appendUnit = (value, unit) => join('', [value, unit]);
|
19 |
|
20 | export const unitIsRemOrEm = contains(__, [
|
21 | UNITS.DIMENSIONS.EM,
|
22 | UNITS.DIMENSIONS.REM,
|
23 | ]);
|
24 |
|
25 | export const pxToRemOrEmValue = (value, baseFontSize) =>
|
26 | divide(value, baseFontSize);
|
27 |
|
28 | export const remOrEmToPxValue = (value, baseFontSize) =>
|
29 | multiply(value, baseFontSize);
|
30 |
|
31 | export const toDimensionOutput = (unit, baseFontSize, value) =>
|
32 | appendUnit(
|
33 | unitIsRemOrEm(unit) ? pxToRemOrEmValue(value, baseFontSize) : value,
|
34 | unit
|
35 | );
|
36 |
|
37 | export 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 |
|
45 | export const numericPartOfUnitedNumber = compose(
|
46 | view(lensIndex(0)),
|
47 | elementsOfUnitedNumber
|
48 | );
|
49 |
|
50 | export const unitPartOfUnitedNumber = compose(
|
51 | view(lensIndex(1)),
|
52 | elementsOfUnitedNumber
|
53 | );
|
54 |
|
55 | export const unitedDimensionToUnitlessPixelValue = (value, baseFontSize) => {
|
56 | const [number, unit] = elementsOfUnitedNumber(value);
|
57 | return unitIsRemOrEm(unit) ? remOrEmToPxValue(number, baseFontSize) : number;
|
58 | };
|
59 |
|
60 | export const unitedResolutionToUnitlessValue = value =>
|
61 | numericPartOfUnitedNumber(value);
|