convert-css-length
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,74 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
var baseFontSize, parseUnit, unit, unitLess; | ||
parseUnit = require('parse-unit'); | ||
require('console-polyfill'); | ||
baseFontSize = "16px"; | ||
unit = function(length) { | ||
return parseUnit(length)[1]; | ||
}; | ||
unitLess = function(length) { | ||
return parseUnit(length)[0]; | ||
}; | ||
module.exports = function(baseFontSize) { | ||
if (baseFontSize == null) { | ||
baseFontSize = baseFontSize; | ||
} | ||
return function(length, toUnit, fromContext, toContext) { | ||
var fromUnit, outputLength, pxLength; | ||
if (fromContext == null) { | ||
fromContext = baseFontSize; | ||
} | ||
if (toContext == null) { | ||
toContext = fromContext; | ||
} | ||
fromUnit = unit(length); | ||
if (fromUnit === toUnit) { | ||
return length; | ||
} | ||
pxLength = unitLess(length); | ||
if (unit(fromContext) !== "px") { | ||
console.warn("Parameter fromContext must resolve to a value in pixel units."); | ||
} | ||
if (unit(toContext) !== "px") { | ||
console.warn("Parameter toContext must resolve to a value in pixel units."); | ||
} | ||
if (fromUnit !== "px") { | ||
if (fromUnit === "em") { | ||
pxLength = unitLess(length) * unitLess(fromContext); | ||
} else if (fromUnit === "rem") { | ||
pxLength = unitLess(length) * unitLess(baseFontSize); | ||
} else if (fromUnit === "ex") { | ||
pxLength = unitLess(length) * unitLess(fromContext) * 2; | ||
} else if (fromUnit === "ch" || fromUnit === "vw" || fromUnit === "vh" || fromUnit === "vmin") { | ||
console.warn(fromUnit + " units can't be reliably converted; Returning original value."); | ||
return length; | ||
} else { | ||
console.warn(fromUnit + " is an unknown or unsupported length unit; Returning original value."); | ||
return length; | ||
} | ||
} | ||
outputLength = pxLength; | ||
if (toUnit !== "px") { | ||
if (toUnit === "em") { | ||
outputLength = pxLength / unitLess(toContext); | ||
} else if (toUnit === "rem") { | ||
outputLength = pxLength / unitLess(baseFontSize); | ||
} else if (toUnit === "ex") { | ||
outputLength = pxLength / unitLess(toContext) / 2; | ||
} else if (toUnit === "ch" || toUnit === "vw" || toUnit === "vh" || toUnit === "vmin") { | ||
console.warn(toUnit + " units can't be reliably converted; Returning original value."); | ||
return length; | ||
} else { | ||
console.warn(toUnit + " is an unknown or unsupported length unit; Returning original value."); | ||
return length; | ||
} | ||
} | ||
return parseFloat(outputLength.toFixed(5)) + toUnit; | ||
}; | ||
}; | ||
var e=require("parse-unit"),r=function(r){return e(r)[0]};module.exports=function(n){return null==n&&(n=n),function(u,i,t,f){null==t&&(t=n),null==f&&(f=t);var l=function(r){return e(r)[1]}(u);if(l===i)return u;var o=r(u);if("px"!==l)if("em"===l)o=r(u)*r(t);else if("rem"===l)o=r(u)*r(n);else{if("ex"!==l)return u;o=r(u)*r(t)*2}var a=o;if("px"!==i)if("em"===i)a=o/r(f);else if("rem"===i)a=o/r(n);else{if("ex"!==i)return u;a=o/r(f)/2}return parseFloat(a.toFixed(5))+i}}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "convert-css-length", | ||
"description": "Convert between css lengths e.g. em->px or px->rem", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Kyle Mathews <mathews.kyle@gmail.com>", | ||
@@ -16,4 +16,6 @@ "bugs": { | ||
"coffee-script": "^1.9.0", | ||
"microbundle": "^0.4.4", | ||
"mocha": "^2.1.0", | ||
"mocha-unfunk-reporter": "^0.4.0" | ||
"mocha-unfunk-reporter": "^0.4.0", | ||
"prettier": "^1.12.1" | ||
}, | ||
@@ -31,2 +33,3 @@ "homepage": "https://github.com/KyleAMathews/convert-css-length", | ||
"license": "MIT", | ||
"source": "src/index.js", | ||
"main": "dist/index.js", | ||
@@ -38,4 +41,5 @@ "repository": { | ||
"scripts": { | ||
"test": "NODE_ENV=test node_modules/.bin/mocha -w --recursive --compilers coffee:coffee-script/register -R mocha-unfunk-reporter" | ||
"build": "microbundle", | ||
"format": "prettier --write src/index.js" | ||
} | ||
} |
@@ -22,10 +22,10 @@ # convert-css-length | ||
convert('1rem', 'px'); | ||
// ---> 16px | ||
// ---> 21px | ||
// Convert px to em. | ||
convert('30px', 'em'); | ||
// ---> 1.875em | ||
// ---> 1.42857em | ||
// Convert em to pixels using fromContext. | ||
// em(s) are relative to the font-size at the same node. If you're setting an em on a node whose font-size | ||
// em(s) are relative to the font-size at the same element. If you're setting an em on a element whose font-size | ||
// is different than the base font size, you'll need to pass that font-size as the third parameter. | ||
@@ -32,0 +32,0 @@ // Or just use rem instead which sizes everything relative to the base node. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
287026
20
111
6
3