# calculate-measurement

> Mathematical operations with CSS unit

Latest version **0.1.0** (published 2016-04-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install calculate-measurement
pnpm add calculate-measurement
yarn add calculate-measurement
bun add calculate-measurement
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-04-25 |
| First published | 2016-04-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Wai Park Soon |
| Maintainers | pswai |
| Keywords | calculate, measurement, unit, css |

## Links

- npm: https://www.npmjs.com/package/calculate-measurement
- Repository: https://github.com/pswai/calculate-measurement
- Homepage: https://github.com/pswai/calculate-measurement#readme
- Issues: https://github.com/pswai/calculate-measurement/issues
- npm.io page: https://npm.io/package/calculate-measurement

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2016-04-25

## README

calculate-measurement [![npm version](https://badge.fury.io/js/calculate-measurement.svg)](https://badge.fury.io/js/calculate-measurement) [![Build Status](https://travis-ci.org/pswai/calculate-measurement.svg?branch=master)](https://travis-ci.org/pswai/calculate-measurement)
---------
> Mathematical operations with CSS unit

```javascript
import calculateMeasurement from 'calculate-measurement';

// Calculation with unit (default: addition)
let addPixel = calculateMeasurement('300px', 50); // '350px'

// Custom operation
let multiplyPercentage = calculateMeasurement('10%', 2, (value, delta) => value * delta); // '20%'

// Creating shorthand with lodash
let minusWithMinimum = _.partialRight(calculateMeasurement, (value, delta) => {
  return Math.max(value - delta, 100); // result is at least 100
});

let moreThan100 = minusWithMinimum('200px', 90); // '110px'
let lessThan100 = minusWithMinimum('200px', 110); // '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](__tests__/calculate-measurement.spec.js) 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
[MIT](LICENSE.txt)


### Changelog

#### v0.1.0
- First release

---
_Source: https://npm.io/package/calculate-measurement · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
