Mathematical operations with CSS unit
; // Calculation with unit (default: addition)let addPixel = ; // '350px' // Custom operationlet multiplyPercentage = ; // '20%' // Creating shorthand with lodashlet minusWithMinimum = _; let moreThan100 = ; // '110px'let lessThan100 = ; // '100px'
Install
npm install calculate-measurement --save
API
calculateMeasurement(value, delta, op)
value
Type: Number
| String
The value to be operated on. It can be a primitive number or a string with CSS unit. See tests for supported units.
delta
Type: Number
A unit-less number to indicate how much value
should be altered.
op
(optional)
Type: Function
Default: (unitlessValue, delta) => unitlessValue + delta
Custom function to compute end result.
By default it adds delta
to unitlessValue
, the unit-less number version of value
.
// Example: Subtraction
calculateMeasurement('50cm', 10, (a, b) => b - a); // '40cm'
// Example: Addition but limit at 100%
calculateMeasurement('80%', '30', (a, b) => Math.min(a + b, 100)); // '100%'
Returns
Calculation result in the format with value
.
Tests
Tests are very important to keep us in good shape.
npm test
License
Changelog
v0.1.0
- First release