style-unit
Advanced tools
Comparing version 3.0.0 to 3.0.1-0
@@ -11,11 +11,16 @@ "use strict"; | ||
exports.setDecimalPixelTransformer = setDecimalPixelTransformer; | ||
exports.convertUnit = convertUnit; | ||
exports.setUnitPrecision = setUnitPrecision; | ||
exports.cached = cached; | ||
exports.setTargetPlatform = setTargetPlatform; | ||
exports.convertUnit = void 0; | ||
var _universalEnv = require("universal-env"); | ||
var RPX_REG = /[-+]?\d*\.?\d+rpx/g; | ||
var GLOBAL_RPX_COEFFICIENT = '__rpx_coefficient__'; | ||
var GLOBAL_VIEWPORT_WIDTH = '__viewport_width__'; | ||
var global = typeof window === 'object' ? window : typeof global === 'object' ? global : {}; // convertUnit method targetPlatform | ||
var RPX_REG = /"[^"]+"|'[^']+'|url\([^\)]+\)|(\d*\.?\d+)rpx/g; | ||
var __rpx_coefficient__; | ||
var __viewport_width__; // convertUnit method targetPlatform | ||
var targetPlatform = _universalEnv.isWeb ? 'web' : _universalEnv.isWeex ? 'weex' : ''; // Init toFixed method | ||
@@ -32,9 +37,9 @@ | ||
var decimalPixelTransformer = function decimalPixelTransformer(rpx) { | ||
return parseFloat(rpx) * getRpx() + 'px'; | ||
var decimalPixelTransformer = function decimalPixelTransformer(rpx, $1) { | ||
return $1 ? parseFloat(rpx) * getRpx() + 'px' : rpx; | ||
}; // Default decimal vw transformer. | ||
var decimalVWTransformer = function decimalVWTransformer(rpx) { | ||
return toFixed(parseFloat(rpx) / (getViewportWidth() / 100), unitPrecision) + 'vw'; | ||
var decimalVWTransformer = function decimalVWTransformer(rpx, $1) { | ||
return $1 ? toFixed(parseFloat(rpx) / (getViewportWidth() / 100), unitPrecision) + 'vw' : rpx; | ||
}; // Default 1 rpx to 1 px | ||
@@ -86,15 +91,15 @@ | ||
function getRpx() { | ||
return global[GLOBAL_RPX_COEFFICIENT]; | ||
return __rpx_coefficient__; | ||
} | ||
function setRpx(rpx) { | ||
global[GLOBAL_RPX_COEFFICIENT] = rpx; | ||
__rpx_coefficient__ = rpx; | ||
} | ||
function getViewportWidth() { | ||
return global[GLOBAL_VIEWPORT_WIDTH]; | ||
return __viewport_width__; | ||
} | ||
function setViewportWidth(viewport) { | ||
global[GLOBAL_VIEWPORT_WIDTH] = viewport; | ||
__viewport_width__ = viewport; | ||
} | ||
@@ -111,5 +116,30 @@ /** | ||
} | ||
/** | ||
* Set unit precision. | ||
* @param n {Number} Unit precision, default to 4. | ||
*/ | ||
var cache = Object.create(null); | ||
function setUnitPrecision(n) { | ||
unitPrecision = n; | ||
} | ||
/** | ||
* Create a cached version of a pure function. | ||
* Use the first params as cache key. | ||
*/ | ||
function cached(fn) { | ||
var cache = new Map(); | ||
return function cachedFn() { | ||
var key = arguments.length <= 0 ? undefined : arguments[0]; | ||
if (!cache.has(key)) cache.set(key, fn.apply(void 0, arguments)); | ||
return cache.get(key); | ||
}; | ||
} | ||
function setTargetPlatform(platform) { | ||
targetPlatform = platform; | ||
} | ||
/** | ||
* Convert rpx. | ||
@@ -122,17 +152,10 @@ * @param value | ||
function convertUnit(value, prop, platform) { | ||
var cacheKey = prop + "-" + value; | ||
var hit = cache[cacheKey]; | ||
var convertUnit = cached(function (value, prop, platform) { | ||
if (platform) { | ||
cacheKey += "-" + platform; | ||
targetPlatform = platform; | ||
setTargetPlatform(platform); | ||
} | ||
if (hit) { | ||
return hit; | ||
} else { | ||
value = value + ''; | ||
return cache[cacheKey] = isRpx(value) ? calcRpx(value) : value; | ||
} | ||
} | ||
return isRpx(value) ? calcRpx(value) : value; | ||
}); | ||
exports.convertUnit = convertUnit; |
{ | ||
"name": "style-unit", | ||
"version": "3.0.0", | ||
"version": "3.0.1-0", | ||
"description": "style-unit", | ||
@@ -19,4 +19,4 @@ "license": "BSD-3-Clause", | ||
"dependencies": { | ||
"universal-env": "^2.0.0" | ||
"universal-env": "^3.0.0" | ||
} | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance 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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
5656
120
0
1
+ Added@uni/env@1.1.1(transitive)
+ Addeduniversal-env@3.3.3(transitive)
- Removeduniversal-env@2.0.0(transitive)
Updateduniversal-env@^3.0.0