node-px2rem
Advanced tools
Comparing version 1.0.8 to 1.1.0
'use strict'; | ||
var fs = require('fs'); | ||
var pxtorem = require('../index.js'); | ||
var px2rem = require('../index.js'); | ||
var css = fs.readFileSync('main.css', 'utf8'); | ||
var processedCss = pxtorem.process(css, { | ||
mediaQuery: true, | ||
var processedCSS = px2rem.process(css, { | ||
mediaQuery: true | ||
}); | ||
fs.writeFile('main-rem.css', processedCss, function(err) { | ||
fs.writeFile('main-rem.css', processedCSS, function(err) { | ||
if (err) { | ||
@@ -12,0 +12,0 @@ throw err; |
154
index.js
'use strict'; | ||
/** | ||
* Requires | ||
*/ | ||
var postcss = require('postcss'); | ||
var postCSS = require('postcss'); | ||
var extend = require('extend'); | ||
/** | ||
* Px regular expression | ||
* @type {RegExp} | ||
*/ | ||
var pxRegEx = /(\d*\.?\d+)px/ig; | ||
var pxRegExp = /(\d*\.?\d+)px/ig; | ||
/** | ||
* Defaults | ||
* @type {Object} | ||
*/ | ||
var defaults = { | ||
/** | ||
* Root value | ||
* @type {Number} | ||
*/ | ||
rootValue: 16, | ||
/** | ||
* Unit precision | ||
* @type {Number} | ||
*/ | ||
unitPrecision: 5, | ||
/** | ||
* Property black list | ||
* @type {Array} | ||
*/ | ||
propertyBlackList: [], | ||
/** | ||
* Property white list | ||
* @type {Array} | ||
*/ | ||
propertyWhiteList: [], | ||
/** | ||
* Replace | ||
* @type {Boolean} | ||
*/ | ||
replace: false, | ||
/** | ||
* Media query | ||
* @type {Boolean} | ||
*/ | ||
mediaQuery: false, | ||
/** | ||
* Min pixel | ||
* @type {Number} | ||
*/ | ||
minPx: 1, | ||
minPx: 1 | ||
}; | ||
/** | ||
* To pixel | ||
* @param {String} value | ||
* @return {Number|Boolean} | ||
*/ | ||
var o; | ||
var px2rem; | ||
function toPx(value) { | ||
@@ -80,12 +34,4 @@ var parts = /^(\d+\.?\d*)([a-zA-Z%]*)$/.exec(value); | ||
} | ||
return false; | ||
} | ||
/** | ||
* To fixed | ||
* @param {Number} number | ||
* @param {Integer} precision | ||
* @return {Number} | ||
*/ | ||
function toFixed(number, precision) { | ||
@@ -98,23 +44,12 @@ var multiplier = Math.pow(10, precision + 1); | ||
/** | ||
* Px replace | ||
* @param {String} $1 | ||
* @return {Number} | ||
*/ | ||
function pxReplace($1) { | ||
$1 = parseFloat($1); | ||
if (defaults.minPx >= $1) { | ||
return $1 + 'px'; | ||
function pxReplace(strArg) { | ||
var str = parseFloat(strArg); | ||
if (o.minPx >= str) { | ||
return str + 'px'; | ||
} | ||
return toFixed($1 / toPx(defaults.rootValue), defaults.unitPrecision) + 'rem'; | ||
return toFixed(str / toPx(o.rootValue), o.unitPrecision) + 'rem'; | ||
} | ||
/** | ||
* Equals | ||
* @param {Object} decls | ||
* @param {String} prop | ||
* @param {String} value | ||
* @return {Boolean} | ||
*/ | ||
function equals(decls, prop, value) { | ||
@@ -126,42 +61,29 @@ return decls.some(function(decl) { | ||
/** | ||
* Pixel to rem | ||
* @param {Object} options | ||
*/ | ||
function Px2Rem(options) { | ||
if (options) { | ||
defaults = extend(true, {}, defaults, options); | ||
} | ||
o = extend(true, {}, defaults, options); | ||
} | ||
/** | ||
* Process | ||
* @param {String} css | ||
* @param {Object} options | ||
* @return {Object} | ||
*/ | ||
Px2Rem.prototype.process = function(css, options) { | ||
return postcss(this.postCss).process(css, options).css; | ||
return postCSS(this.postCSS).process(css, options).css; | ||
}; | ||
/** | ||
* Post css | ||
* @param {String} css | ||
*/ | ||
Px2Rem.prototype.postCss = function(css) { | ||
Px2Rem.prototype.postCSS = function(css) { | ||
css.walkDecls(function(decl, i) { | ||
if (defaults.propertyBlackList.indexOf(decl.prop) !== -1) { | ||
var rule; | ||
var value; | ||
if (o.propertyBlackList.indexOf(decl.prop) !== -1) { | ||
return; | ||
} | ||
if (defaults.propertyWhiteList.length > 0 && | ||
defaults.propertyWhiteList.indexOf(decl.prop) === -1) { | ||
if (o.propertyWhiteList.length > 0 && | ||
o.propertyWhiteList.indexOf(decl.prop) === -1) { | ||
return; | ||
} | ||
var rule = decl.parent; | ||
var value = decl.value; | ||
rule = decl.parent; | ||
value = decl.value; | ||
if (value.indexOf('px') !== -1) { | ||
value = value.replace(pxRegEx, pxReplace); | ||
value = value.replace(pxRegExp, pxReplace); | ||
@@ -172,7 +94,7 @@ if (equals(rule.nodes, decl.prop, value)) { | ||
if (defaults.replace) { | ||
if (o.replace) { | ||
decl.value = value; | ||
} else { | ||
rule.insertAfter(i, decl.clone({ | ||
value: value, | ||
value: value | ||
})); | ||
@@ -183,3 +105,3 @@ } | ||
if (defaults.mediaQuery) { | ||
if (o.mediaQuery) { | ||
css.each(function(rule) { | ||
@@ -191,3 +113,3 @@ if (rule.type !== 'atrule' && rule.name !== 'media') { | ||
if (rule.params.indexOf('px') !== -1) { | ||
rule.params = rule.params.replace(pxRegEx, pxReplace); | ||
rule.params = rule.params.replace(pxRegExp, pxReplace); | ||
} | ||
@@ -198,22 +120,10 @@ }); | ||
/** | ||
* Pixel to rem | ||
* @param {Object} options | ||
* @return {Object} | ||
*/ | ||
var px2rem = function(options) { | ||
px2rem = function(options) { | ||
return new Px2Rem(options); | ||
}; | ||
/** | ||
* Process | ||
* @param {String} css | ||
* @param {Object} options | ||
* @param {Object} postCssOptions | ||
* @return {Object} | ||
*/ | ||
px2rem.process = function(css, options, postCssOptions) { | ||
return new Px2Rem(options).process(css, postCssOptions); | ||
px2rem.process = function(css, options, postCSSOptions) { | ||
return new Px2Rem(options).process(css, postCSSOptions); | ||
}; | ||
module.exports = px2rem; |
{ | ||
"name": "node-px2rem", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Pixel to rem postproccessor", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "eslint . && nyc ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -27,8 +28,22 @@ "repository": { | ||
"dependencies": { | ||
"extend": "3.0.0", | ||
"postcss": "5.0.13" | ||
"extend": "^3.0.0", | ||
"postcss": "^5.0.14" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.11.0", | ||
"coveralls": "^2.11.6", | ||
"eslint": "^1.10.3", | ||
"eslint-config-mito": "^1.0.0", | ||
"nyc": "^5.3.0", | ||
"pre-commit": "^1.1.2" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "mito/legacy" | ||
}, | ||
"pre-commit": [ | ||
"test" | ||
] | ||
} |
@@ -1,3 +0,3 @@ | ||
# Pixel to rem [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] | ||
Version: **1.0.8** | ||
# 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.0** | ||
@@ -16,10 +16,11 @@ ## Installation | ||
var css = fs.readFileSync('main.css', 'utf8'); | ||
var processedCss = px2rem.process(css, { | ||
var processedCSS = px2rem.process(css, { | ||
rootValue: 16 | ||
}); | ||
fs.writeFile('main-rem.css', processedCss, function(err) { | ||
fs.writeFile('main-rem.css', processedCSS, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Done.'); | ||
@@ -56,7 +57,11 @@ }); | ||
# License | ||
MIT © 2015 Gergely Kovács (gg.kovacs@gmail.com) | ||
MIT © 2016 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 | ||
[travis-image]: https://travis-ci.org/ggkovacs/node-px2rem.svg?branch=master | ||
[travis-url]: https://travis-ci.org/ggkovacs/node-px2rem | ||
[daviddm-image]: https://david-dm.org/ggkovacs/node-px2rem.svg?theme=shields.io | ||
[daviddm-url]: https://david-dm.org/ggkovacs/node-px2rem | ||
[coverage-image]: https://coveralls.io/repos/ggkovacs/node-px2rem/badge.svg?service=github&branch=master | ||
[coverage-url]: https://coveralls.io/github/ggkovacs/node-px2rem?branch=master |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
23713
21
442
0
66
6
2
1
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedpostcss@5.2.18(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
- Removedextend@3.0.0(transitive)
- Removedpostcss@5.0.13(transitive)
Updatedextend@^3.0.0
Updatedpostcss@^5.0.14