Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

style-unit

Package Overview
Dependencies
Maintainers
7
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

style-unit - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1-0

75

lib/index.js

@@ -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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc