summaryrefslogtreecommitdiff
path: root/source/helpers/calculate-css-calc-string.ts
blob: 18527afd9dbb1b9d94ef90500ee94d0430bed0b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
import looseParseInt from './loose-parse-int.js';

/**
Compute the sum in a `calc()`. Only works with very simple sums of px values.
When used with custom properties, `calc()` are not evaluated when retrieved via `getComputedStyle()`

@example calculateCssCalcString('calc(1px + 2px)') => 3;
*/
export default function calculateCssCalcString(string: string): number {
	const addends = string.split('+').map(part => looseParseInt(part));
	return addends.reduce((a, b) => a + b);
}