Skip to content

Commit 28dd4d8

Browse files
committed
New precision option
Close #5
1 parent 3d5a95d commit 28dd4d8

11 files changed

+68
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# HEAD
22

33
- New gnu like exceptions ([ref](https://github.com/postcss/postcss-calc/issues/4))
4+
- New `precision` option ([ref](https://github.com/postcss/postcss-calc/issues/5))
45
# 2.1.0 - 2014-10-15
56

67
- Add source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))

README.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ This can be particularly useful with the [postcss-custom-properties](https://git
99

1010
## Installation
1111

12-
$ npm install postcss-calc
12+
```console
13+
$ npm install postcss-calc
14+
```
1315

1416
## Usage
1517

@@ -81,16 +83,31 @@ h1 {
8183

8284
Checkout [tests](test) for more examples.
8385

86+
### Options
87+
88+
#### `precision` (default: `5`)
89+
90+
Allow you to definine the precision for decimal numbers.
91+
92+
```js
93+
var out = postcss()
94+
.use(calc({precision: 10}))
95+
.process(css)
96+
.css
97+
```
98+
8499
---
85100

86101
## Contributing
87102

88103
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
89104

90-
$ git clone https://github.com/postcss/postcss-calc.git
91-
$ git checkout -b patch-1
92-
$ npm install
93-
$ npm test
105+
```console
106+
$ git clone https://github.com/postcss/postcss-calc.git
107+
$ git checkout -b patch-1
108+
$ npm install
109+
$ npm test
110+
```
94111

95112
## [Changelog](CHANGELOG.md)
96113

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ var helpers = require("postcss-message-helpers")
77
/**
88
* PostCSS plugin to reduce calc() function calls.
99
*/
10-
module.exports = function plugin() {
10+
module.exports = function plugin(options) {
11+
options = options || {}
12+
1113
return function(style) {
1214
style.eachDecl(function transformDecl(decl) {
1315
if (!decl.value) {
@@ -19,7 +21,7 @@ module.exports = function plugin() {
1921
return decl.value
2022
}
2123

22-
return reduceCSSCalc(decl.value)
24+
return reduceCSSCalc(decl.value, options.precision)
2325
})
2426
})
2527
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"index.js"
2222
],
2323
"dependencies": {
24-
"reduce-css-calc": "^1.1.2"
2524
"postcss-message-helpers": "^1.1.0",
25+
"reduce-css-calc": "^1.2.0"
2626
},
2727
"devDependencies": {
2828
"jscs": "^1.6.2",

test/fixtures/calc.actual.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
stuff {
2+
dont: care;
3+
ignore: calc(new(blah));
4+
resolved: 0.125rem otherfn(blah);
5+
unresolved: calc(1.125rem - 1px);
6+
prefixed-unresolved: -webkit-calc(1.125rem - 1px);
7+
z-index: 111;
8+
}
File renamed without changes.
File renamed without changes.

test/fixtures/precision.actual.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
calc {
2+
value: 0.333;
3+
}

test/fixtures/precision.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
calc {
2+
value: calc(1/3);
3+
}

test/fixtures/precision.expected.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
calc {
2+
value: 0.333;
3+
}

test/index.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
var test = require("tape")
2-
3-
var calc = require("..")
1+
var fs = require("fs")
42

5-
var read = require("fs").readFileSync
3+
var test = require("tape")
64
var postcss = require("postcss")
75
var customProperties = require("postcss-custom-properties")
6+
var calc = require("..")
7+
8+
function fixturePath(name) {
9+
return "test/fixtures/" + name + ".css"
10+
}
811

912
function fixture(name) {
10-
return read("test/" + name + ".css", "utf8").trim()
13+
return fs.readFileSync(fixturePath(name), "utf8").trim()
1114
}
1215

13-
test("resolve what is possible in complex calc", function(t) {
16+
function compareFixtures(t, name, options, message) {
1417
var actual = postcss()
1518
.use(customProperties())
16-
.use(calc())
17-
.process(fixture("calc"))
18-
.css
19-
.trim()
20-
var expected = fixture("calc.out")
21-
t.equal(actual, expected)
19+
.use(calc(options))
20+
.process(fixture(name), {from: fixturePath(name)})
21+
.css.trim()
22+
23+
// handy thing: checkout actual in the *.actual.css file
24+
fs.writeFile(fixturePath(name + ".actual"), actual)
25+
26+
return t.equal(actual, fixture(name + ".expected"), message ? message : "processed fixture '" + name + "' should be equal to expected output")
27+
}
28+
29+
test("calc", function(t) {
30+
compareFixtures(t, "calc", {}, "should resolve what is possible in complex calc")
31+
32+
compareFixtures(t, "precision", {precision: 3}, "should have a precision option that allow to control decimal precision of calcuations")
2233

2334
t.end()
2435
})

0 commit comments

Comments
 (0)