style-value-types
Advanced tools
Comparing version 4.1.1 to 4.1.2
@@ -5,2 +5,12 @@ # Changelog | ||
### [4.1.2] 2021-03-19 | ||
### Added | ||
- Adding `exports` to `package.json`. | ||
### Updated | ||
- `tslib` to latest. | ||
### [4.1.1] 2021-03-01 | ||
@@ -7,0 +17,0 @@ |
242
lib/index.js
@@ -1,232 +0,10 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var tslib = require('tslib'); | ||
var clamp = function (min, max) { return function (v) { | ||
return Math.max(Math.min(v, max), min); | ||
}; }; | ||
var sanitize = function (v) { return (v % 1 ? Number(v.toFixed(5)) : v); }; | ||
var floatRegex = /(-)?([\d]*\.?[\d])+/g; | ||
var colorRegex = /(#[0-9a-f]{6}|#[0-9a-f]{3}|#(?:[0-9a-f]{2}){2,4}|(rgb|hsl)a?\((-?[\d\.]+%?[,\s]+){2,3}\s*\/*\s*[\d\.]+%?\))/gi; | ||
var singleColorRegex = /^(#[0-9a-f]{3}|#(?:[0-9a-f]{2}){2,4}|(rgb|hsl)a?\((-?[\d\.]+%?[,\s]+){2,3}\s*\/*\s*[\d\.]+%?\))$/i; | ||
function isString(v) { | ||
return typeof v === 'string'; | ||
} | ||
var number = { | ||
test: function (v) { return typeof v === 'number'; }, | ||
parse: parseFloat, | ||
transform: function (v) { return v; }, | ||
}; | ||
var alpha = tslib.__assign(tslib.__assign({}, number), { transform: clamp(0, 1) }); | ||
var scale = tslib.__assign(tslib.__assign({}, number), { default: 1 }); | ||
var createUnitType = function (unit) { return ({ | ||
test: function (v) { | ||
return isString(v) && v.endsWith(unit) && v.split(' ').length === 1; | ||
}, | ||
parse: parseFloat, | ||
transform: function (v) { return "" + v + unit; }, | ||
}); }; | ||
var degrees = createUnitType('deg'); | ||
var percent = createUnitType('%'); | ||
var px = createUnitType('px'); | ||
var vh = createUnitType('vh'); | ||
var vw = createUnitType('vw'); | ||
var progressPercentage = tslib.__assign(tslib.__assign({}, percent), { parse: function (v) { return percent.parse(v) / 100; }, transform: function (v) { return percent.transform(v * 100); } }); | ||
var isColorString = function (type, testProp) { return function (v) { | ||
return ((isString(v) && singleColorRegex.test(v) && v.startsWith(type)) || | ||
(testProp && Object.prototype.hasOwnProperty.call(v, testProp))); | ||
}; }; | ||
var splitColor = function (aName, bName, cName) { return function (v) { | ||
var _a; | ||
if (!isString(v)) | ||
return v; | ||
var _b = v.match(floatRegex), a = _b[0], b = _b[1], c = _b[2], alpha = _b[3]; | ||
return _a = {}, | ||
_a[aName] = parseFloat(a), | ||
_a[bName] = parseFloat(b), | ||
_a[cName] = parseFloat(c), | ||
_a.alpha = alpha !== undefined ? parseFloat(alpha) : 1, | ||
_a; | ||
}; }; | ||
var hsla = { | ||
test: isColorString('hsl', 'hue'), | ||
parse: splitColor('hue', 'saturation', 'lightness'), | ||
transform: function (_a) { | ||
var hue = _a.hue, saturation = _a.saturation, lightness = _a.lightness, _b = _a.alpha, alpha$1 = _b === void 0 ? 1 : _b; | ||
return ('hsla(' + | ||
Math.round(hue) + | ||
', ' + | ||
percent.transform(sanitize(saturation)) + | ||
', ' + | ||
percent.transform(sanitize(lightness)) + | ||
', ' + | ||
sanitize(alpha.transform(alpha$1)) + | ||
')'); | ||
}, | ||
}; | ||
var clampRgbUnit = clamp(0, 255); | ||
var rgbUnit = tslib.__assign(tslib.__assign({}, number), { transform: function (v) { return Math.round(clampRgbUnit(v)); } }); | ||
var rgba = { | ||
test: isColorString('rgb', 'red'), | ||
parse: splitColor('red', 'green', 'blue'), | ||
transform: function (_a) { | ||
var red = _a.red, green = _a.green, blue = _a.blue, _b = _a.alpha, alpha$1 = _b === void 0 ? 1 : _b; | ||
return 'rgba(' + | ||
rgbUnit.transform(red) + | ||
', ' + | ||
rgbUnit.transform(green) + | ||
', ' + | ||
rgbUnit.transform(blue) + | ||
', ' + | ||
sanitize(alpha.transform(alpha$1)) + | ||
')'; | ||
}, | ||
}; | ||
function parseHex(v) { | ||
var r = ''; | ||
var g = ''; | ||
var b = ''; | ||
var a = ''; | ||
if (v.length > 5) { | ||
r = v.substr(1, 2); | ||
g = v.substr(3, 2); | ||
b = v.substr(5, 2); | ||
a = v.substr(7, 2); | ||
} | ||
else { | ||
r = v.substr(1, 1); | ||
g = v.substr(2, 1); | ||
b = v.substr(3, 1); | ||
a = v.substr(4, 1); | ||
r += r; | ||
g += g; | ||
b += b; | ||
a += a; | ||
} | ||
return { | ||
red: parseInt(r, 16), | ||
green: parseInt(g, 16), | ||
blue: parseInt(b, 16), | ||
alpha: a ? parseInt(a, 16) / 255 : 1, | ||
}; | ||
} | ||
var hex = { | ||
test: isColorString('#'), | ||
parse: parseHex, | ||
transform: rgba.transform, | ||
}; | ||
var color = { | ||
test: function (v) { return rgba.test(v) || hex.test(v) || hsla.test(v); }, | ||
parse: function (v) { | ||
if (rgba.test(v)) { | ||
return rgba.parse(v); | ||
} | ||
else if (hsla.test(v)) { | ||
return hsla.parse(v); | ||
} | ||
else { | ||
return hex.parse(v); | ||
} | ||
}, | ||
transform: function (v) { | ||
return isString(v) | ||
? v | ||
: v.hasOwnProperty('red') | ||
? rgba.transform(v) | ||
: hsla.transform(v); | ||
}, | ||
}; | ||
var colorToken = '${c}'; | ||
var numberToken = '${n}'; | ||
function test(v) { | ||
var _a, _b, _c, _d; | ||
return (isNaN(v) && | ||
isString(v) && | ||
((_b = (_a = v.match(floatRegex)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) + ((_d = (_c = v.match(colorRegex)) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0); | ||
} | ||
function analyse(v) { | ||
var values = []; | ||
var numColors = 0; | ||
var colors = v.match(colorRegex); | ||
if (colors) { | ||
numColors = colors.length; | ||
v = v.replace(colorRegex, colorToken); | ||
values.push.apply(values, colors.map(color.parse)); | ||
} | ||
var numbers = v.match(floatRegex); | ||
if (numbers) { | ||
v = v.replace(floatRegex, numberToken); | ||
values.push.apply(values, numbers.map(number.parse)); | ||
} | ||
return { values: values, numColors: numColors, tokenised: v }; | ||
} | ||
function parse(v) { | ||
return analyse(v).values; | ||
} | ||
function createTransformer(v) { | ||
var _a = analyse(v), values = _a.values, numColors = _a.numColors, tokenised = _a.tokenised; | ||
var numValues = values.length; | ||
return function (v) { | ||
var output = tokenised; | ||
for (var i = 0; i < numValues; i++) { | ||
output = output.replace(i < numColors ? colorToken : numberToken, i < numColors ? color.transform(v[i]) : sanitize(v[i])); | ||
} | ||
return output; | ||
}; | ||
} | ||
var convertNumbersToZero = function (v) { | ||
return typeof v === 'number' ? 0 : v; | ||
}; | ||
function getAnimatableNone(v) { | ||
var parsed = parse(v); | ||
var transformer = createTransformer(v); | ||
return transformer(parsed.map(convertNumbersToZero)); | ||
} | ||
var complex = { test: test, parse: parse, createTransformer: createTransformer, getAnimatableNone: getAnimatableNone }; | ||
var maxDefaults = new Set(['brightness', 'contrast', 'saturate', 'opacity']); | ||
function applyDefaultFilter(v) { | ||
var _a = v.slice(0, -1).split('('), name = _a[0], value = _a[1]; | ||
if (name === 'drop-shadow') | ||
return v; | ||
var number = (value.match(floatRegex) || [])[0]; | ||
if (!number) | ||
return v; | ||
var unit = value.replace(number, ''); | ||
var defaultValue = maxDefaults.has(name) ? 1 : 0; | ||
if (number !== value) | ||
defaultValue *= 100; | ||
return name + '(' + defaultValue + unit + ')'; | ||
} | ||
var functionRegex = /([a-z-]*)\(.*?\)/g; | ||
var filter = tslib.__assign(tslib.__assign({}, complex), { getAnimatableNone: function (v) { | ||
var functions = v.match(functionRegex); | ||
return functions ? functions.map(applyDefaultFilter).join(' ') : v; | ||
} }); | ||
exports.alpha = alpha; | ||
exports.color = color; | ||
exports.complex = complex; | ||
exports.degrees = degrees; | ||
exports.filter = filter; | ||
exports.hex = hex; | ||
exports.hsla = hsla; | ||
exports.number = number; | ||
exports.percent = percent; | ||
exports.progressPercentage = progressPercentage; | ||
exports.px = px; | ||
exports.rgbUnit = rgbUnit; | ||
exports.rgba = rgba; | ||
exports.scale = scale; | ||
exports.vh = vh; | ||
exports.vw = vw; | ||
export * from './types'; | ||
export { number, scale, alpha } from './numbers'; | ||
export { degrees, percent, progressPercentage, px, vw, vh, } from './numbers/units'; | ||
export { hsla } from './color/hsla'; | ||
export { rgba, rgbUnit } from './color/rgba'; | ||
export { hex } from './color/hex'; | ||
export { color } from './color'; | ||
export { complex } from './complex'; | ||
export { filter } from './complex/filter'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "style-value-types", | ||
"version": "4.1.1", | ||
"version": "4.1.2", | ||
"description": "Parsers, transformers and tests for special value types, eg: %, hex codes etc.", | ||
@@ -9,2 +9,7 @@ "main": "lib/index.js", | ||
"jsnext:main": "dist/es/index.js", | ||
"exports": { | ||
"import": "./dist/es/index.js", | ||
"require": "./dist/style-value-types.cjs.js", | ||
"default": "./dist/es/index.js" | ||
}, | ||
"files": [ | ||
@@ -63,4 +68,4 @@ "lib", | ||
"hey-listen": "^1.0.8", | ||
"tslib": "^1.10.0" | ||
"tslib": "^2.1.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
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
62167
54
1042
+ Addedtslib@2.8.1(transitive)
- Removedtslib@1.14.1(transitive)
Updatedtslib@^2.1.0