Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ggkovacs committed Jan 22, 2019
1 parent 8945a98 commit efc8dba
Show file tree
Hide file tree
Showing 23 changed files with 4,230 additions and 5,263 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
Expand Down
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

19 changes: 19 additions & 0 deletions .eslintrc
@@ -0,0 +1,19 @@
{
"extends": [
"mito"
],
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [2, 2, {
"SwitchCase": 1
}],
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}],
'newline-per-chained-call': ['off']
}
}
5 changes: 5 additions & 0 deletions .huskyrc
@@ -0,0 +1,5 @@
{
"hooks": {
"pre-commit": "npm test"
}
}
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2018 Gergely Kovács (gg.kovacs@gmail.com)
Copyright © 2019 Gergely Kovács (gg.kovacs@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
38 changes: 19 additions & 19 deletions README.md
@@ -1,5 +1,5 @@
# Pixel to rem [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status][coverage-image]][coverage-url]
Version: **1.1.7**
Version: **2.0.0**

## Installation

Expand All @@ -10,19 +10,19 @@ Run `npm install node-px2rem`
```js
'use strict';

var fs = require('fs');
var px2rem = require('node-px2rem');
var css = fs.readFileSync('main.css', 'utf8');
var processedCSS = px2rem.process(css, {
rootValue: 16
const fs = require('fs');
const px2rem = require('node-px2rem');
const css = fs.readFileSync('main.css', 'utf8');
const processedCSS = px2rem.process(css, {
rootValue: 16
});

fs.writeFile('main-rem.css', processedCSS, function(err) {
if (err) {
throw err;
}
fs.writeFile('main-rem.css', processedCSS, (err) => {
if (err) {
throw err;
}

console.log('Done.');
console.log('Done.');
});
```

Expand All @@ -34,13 +34,13 @@ Type: `Object | Null`
Default:
```js
{
rootValue: 16,
unitPrecision: 5,
propertyBlackList: [],
propertyWhiteList: [],
replace: false,
mediaQuery: false,
minPx: 1
rootValue: 16,
unitPrecision: 5,
propertyBlackList: [],
propertyWhiteList: [],
replace: false,
mediaQuery: false,
minPx: 1
}
```

Expand All @@ -53,7 +53,7 @@ Default:
- `minPx` (Number) If minimum px greater than or equal can change from px to rem.

# License
MIT © 2018 Gergely Kovács (gg.kovacs@gmail.com)
MIT © 2019 Gergely Kovács (gg.kovacs@gmail.com)

[npm-image]: https://badge.fury.io/js/node-px2rem.svg
[npm-url]: https://npmjs.org/package/node-px2rem
Expand Down
21 changes: 11 additions & 10 deletions demo/index.js
@@ -1,16 +1,17 @@
'use strict';

var fs = require('fs');
var px2rem = require('../index.js');
var css = fs.readFileSync('main.css', 'utf8');
var processedCSS = px2rem.process(css, {
mediaQuery: true
const fs = require('fs');
const path = require('path');
const px2rem = require('../index.js');
const css = fs.readFileSync(path.join(__dirname, 'main.css'), 'utf8');
const processedCSS = px2rem.process(css, {
mediaQuery: true
});

fs.writeFile('main-rem.css', processedCSS, function(err) {
if (err) {
throw err;
}
fs.writeFile(path.join(__dirname, 'main-rem.css'), processedCSS, (err) => {
if (err) {
throw err;
}

console.log('Done.');
console.log('Done.');
});
163 changes: 77 additions & 86 deletions index.js
@@ -1,124 +1,115 @@
'use strict';

var postCSS = require('postcss');
var extend = require('extend');

var pxRegExp = /(\d*\.?\d+)px/ig;

var defaults = {
rootValue: 16,
unitPrecision: 5,
propertyBlackList: [],
propertyWhiteList: [],
replace: false,
mediaQuery: false,
minPx: 1
const postCSS = require('postcss');
const extend = require('extend');

const pxRegExp = /(\d*\.?\d+)px/ig;

const defaults = {
rootValue: 16,
unitPrecision: 5,
propertyBlackList: [],
propertyWhiteList: [],
replace: false,
mediaQuery: false,
minPx: 1
};

var o;

var px2rem;
let o;

function toPx(value) {
var parts = /^(\d+\.?\d*)([a-zA-Z%]*)$/.exec(value);
var number = parts[1];
var unit = parts[2];

if (unit === 'px' || unit === '') {
return parseFloat(number);
} else if (unit === 'em' || unit === 'rem') {
return parseFloat(number) * 16;
} else if (unit === '%') {
return (parseFloat(number) / 100) * 16;
}

return 1;
const parts = /^(\d+\.?\d*)([a-zA-Z%]*)$/.exec(value);
const [, number, unit] = parts;

if (unit === 'px' || unit === '') {
return parseFloat(number);
} else if (unit === 'em' || unit === 'rem') {
return parseFloat(number) * 16;
} else if (unit === '%') {
return (parseFloat(number) / 100) * 16;
}

return 1;
}

function toFixed(number, precision) {
var multiplier = Math.pow(10, precision + 1);
var wholeNumber = Math.floor(number * multiplier);
const multiplier = Math.pow(10, precision + 1);
const wholeNumber = Math.floor(number * multiplier);

return Math.round(wholeNumber / 10) * 10 / multiplier;
return Math.round(wholeNumber / 10) * 10 / multiplier;
}

function pxReplace(strArg) {
var str = parseFloat(strArg);
const str = parseFloat(strArg);

if (o.minPx >= str) {
return str + 'px';
}
if (o.minPx >= str) {
return `${str}px`;
}

return toFixed(str / toPx(o.rootValue), o.unitPrecision) + 'rem';
return `${toFixed(str / toPx(o.rootValue), o.unitPrecision)}rem`;
}

function equals(decls, prop, value) {
return decls.some(function(decl) {
return (decl.prop === prop && decl.value === value);
});
return decls.some(decl => (decl.prop === prop && decl.value === value));
}

function Px2Rem(options) {
o = extend(true, {}, defaults, options);
o = extend(true, {}, defaults, options);
}

Px2Rem.prototype.process = function(css, options) {
return postCSS(this.postCSS).process(css, options).css;
return postCSS(this.postCSS).process(css, options).css;
};

Px2Rem.prototype.postCSS = function(css) {
css.walkDecls(function(decl, i) {
var rule;
var value;

if (o.propertyBlackList.indexOf(decl.prop) !== -1) {
return;
}

if (o.propertyWhiteList.length > 0 &&
o.propertyWhiteList.indexOf(decl.prop) === -1) {
return;
}

rule = decl.parent;
value = decl.value;

if (value.indexOf('px') !== -1) {
value = value.replace(pxRegExp, pxReplace);

if (equals(rule.nodes, decl.prop, value)) {
return;
}

if (o.replace) {
decl.value = value;
} else {
rule.insertAfter(i, decl.clone({
value: value
}));
}
}
});
css.walkDecls((decl, i) => {
if (o.propertyBlackList.indexOf(decl.prop) !== -1) {
return;
}

if (o.mediaQuery) {
css.each(function(rule) {
if (rule.type !== 'atrule' && rule.name !== 'media') {
return;
}
if (o.propertyWhiteList.length > 0 && o.propertyWhiteList.indexOf(decl.prop) === -1) {
return;
}

const rule = decl.parent;
let { value } = decl;

if (value.indexOf('px') !== -1) {
value = value.replace(pxRegExp, pxReplace);

if (equals(rule.nodes, decl.prop, value)) {
return;
}

if (rule.params.indexOf('px') !== -1) {
rule.params = rule.params.replace(pxRegExp, pxReplace);
}
});
if (o.replace) {
decl.value = value;
} else {
rule.insertAfter(i, decl.clone({
value
}));
}
}
});

if (o.mediaQuery) {
css.each((rule) => {
if (rule.type !== 'atrule' && rule.name !== 'media') {
return;
}

if (rule.params.indexOf('px') !== -1) {
rule.params = rule.params.replace(pxRegExp, pxReplace);
}
});
}
};

px2rem = function(options) {
return new Px2Rem(options);
const px2rem = function(options) {
return new Px2Rem(options);
};

px2rem.process = function(css, options, postCSSOptions) {
return new Px2Rem(options).process(css, postCSSOptions);
return new Px2Rem(options).process(css, postCSSOptions);
};

module.exports = px2rem;

0 comments on commit efc8dba

Please sign in to comment.