这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"main": "build/index.js",
"scripts": {
"build": "babel src -d build",
"lint": "eslint src",
"mocha": "mocha --compilers js:babel-core/register",
"prepublish": "npm run build && npm test",
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register"
"test": "npm run lint && npm run mocha"
},
"repository": {
"type": "git",
Expand Down
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const removeHash = hex => (hex.charAt(0) === '#' ? hex.slice(1) : hex);

const parseHex = (nakedHex) => {
const isShort = (
3 === nakedHex.length
|| 4 === nakedHex.length
nakedHex.length === 3
|| nakedHex.length === 4
);

const twoDigitHexR = isShort ? `${nakedHex.slice(0, 1)}${nakedHex.slice(0, 1)}` : nakedHex.slice(0, 2);
Expand All @@ -23,17 +23,21 @@ const parseHex = (nakedHex) => {

const hexToDecimal = hex => parseInt(hex, 16);

const hexesToDecimals = ({ r, g, b, a }) => ({
const hexesToDecimals = ({
r, g, b, a,
}) => ({
r: hexToDecimal(r),
g: hexToDecimal(g),
b: hexToDecimal(b),
a: +((hexToDecimal(a) / 255).toFixed(2)),
});

const isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n);
const isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n); // eslint-disable-line no-restricted-globals, max-len

const formatRgb = (decimalObject, parameterA) => {
const { r, g, b, a: parsedA } = decimalObject;
const {
r, g, b, a: parsedA,
} = decimalObject;
const a = isNumeric(parameterA) ? parameterA : parsedA;

return `rgba(${r}, ${g}, ${b}, ${a})`;
Expand Down