summaryrefslogtreecommitdiff
path: root/source/helpers/calculate-css-calc-string.ts
diff options
context:
space:
mode:
Diffstat (limited to 'source/helpers/calculate-css-calc-string.ts')
-rw-r--r--source/helpers/calculate-css-calc-string.ts12
1 files changed, 12 insertions, 0 deletions
diff --git a/source/helpers/calculate-css-calc-string.ts b/source/helpers/calculate-css-calc-string.ts
new file mode 100644
index 00000000..e711ca85
--- /dev/null
+++ b/source/helpers/calculate-css-calc-string.ts
@@ -0,0 +1,12 @@
+import looseParseInt from './loose-parse-int';
+
+/**
+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);
+}