Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 3aeceac

Browse files
committed
🚀
0 parents  commit 3aeceac

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const postcss = require('postcss')
2+
const crypto = require('crypto')
3+
4+
const declPatterns = [
5+
/(margin|padding)-(left|right|top|bottom)/,
6+
/font-size/,
7+
]
8+
9+
function updateDecl(decl) {
10+
const hash = parseInt(crypto.createHash('sha1').update(JSON.stringify(decl)).digest('hex'), 16)
11+
const matches = /^(\d+)([a-z]+)$/i.exec(decl.value)
12+
13+
if (!matches) return
14+
15+
const diff = hash % 5
16+
17+
decl.replaceWith(decl.clone({
18+
value: `${Number(matches[1]) + diff}${matches[2]}`
19+
}))
20+
}
21+
22+
module.exports = postcss.plugin('postcss-pixel-imperfect', (opts) => (css) => {
23+
declPatterns.forEach((declPattern) => {
24+
css.walkDecls(declPattern, updateDecl)
25+
})
26+
})

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "postcss-pixel-imperfect",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"author": "Viacheslav Slinko <vslinko@yahoo.com>",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"postcss": "^5.0.21",
13+
"tape": "^4.6.0"
14+
}
15+
}

test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const test = require('tape')
2+
const postcss = require('postcss')
3+
const plugin = require('./index')
4+
5+
function run(t, input, output, opts = {}) {
6+
return postcss([ plugin(opts) ])
7+
.process(input)
8+
.then( result => {
9+
t.deepEqual(result.css, output)
10+
t.deepEqual(result.warnings().length, 0)
11+
t.end()
12+
})
13+
}
14+
15+
test('some', (t) => {
16+
run(t, `
17+
.header {
18+
font-size: 1pt;
19+
margin-top: 10px;
20+
margin-bottom: 10px;
21+
}
22+
`, `
23+
.header {
24+
font-size: 4pt;
25+
margin-top: 10px;
26+
margin-bottom: 13px;
27+
}
28+
`)
29+
})

0 commit comments

Comments
 (0)