Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dsedinkin committed Jul 11, 2023
1 parent 1517b62 commit cf73f95
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 68 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

16 changes: 14 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
import { TCssBoxShadowValue, TCssBoxShadow } from "./type";
export declare const cssBoxShadow: (stringBoxShadows: TCssBoxShadowValue) => TCssBoxShadow;
type TGetColor = string | number | undefined;
type TCssBoxShadowValue = string;
type TCssBoxShadow = Array<{
inset: boolean;
x: string | number;
y: string | number;
blur: string | number;
spread: string | number;
color: TGetColor;
}> | undefined;

declare const cssBoxShadow: (stringBoxShadows: TCssBoxShadowValue) => TCssBoxShadow;

export { cssBoxShadow };
42 changes: 20 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssBoxShadow = void 0;
'use strict';

const toNumber = (v) => isFinite(+v) ? +v : v;
const parseUnit = (v) => {
const match = v.match(/^(0?[-.]?\d+)(r?e[m|x]|v[h|w|min|max]+|p[x|t|c]|[c|m]m|%|s|in|ch)$/);
const res = match ? parseFloat(match[1]) || match[1] : v;
return toNumber(res);
const match = v.match(/^(0?[-.]?\d+)(r?e[m|x]|v[h|w|min|max]+|p[x|t|c]|[c|m]m|%|s|in|ch)$/);
const res = match ? parseFloat(match[1]) || match[1] : v;
return toNumber(res);
};
const boxShadowParse = (string) => string.split(/\s(?![^(]*\))/).map((v) => parseUnit(v));
const getColor = (values) => values.find((v) => !isFinite(+v));
const cssBoxShadow = (stringBoxShadows) => {
try {
if (typeof stringBoxShadows !== "string")
throw new Error("Недопустимый формат box-shadow.");
return stringBoxShadows.split(/,(?![^\(]*\))/).map(stringBoxShadow => {
const inset = stringBoxShadow.includes("inset");
stringBoxShadow = stringBoxShadow.replace("inset", "").trim();
const valuesParsed = boxShadowParse(stringBoxShadow);
const color = getColor(valuesParsed);
const [x, y, blur, spread] = valuesParsed.filter((v) => v !== color);
return { inset, x, y, blur, spread, color };
});
}
catch (err) {
// console.error("lib cssBoxShadow: ", err);
return undefined;
}
try {
if (typeof stringBoxShadows !== "string")
throw new Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 box-shadow.");
return stringBoxShadows.split(/,(?![^\(]*\))/).map((stringBoxShadow) => {
const inset = stringBoxShadow.includes("inset");
stringBoxShadow = stringBoxShadow.replace("inset", "").trim();
const valuesParsed = boxShadowParse(stringBoxShadow);
const color = getColor(valuesParsed);
const [x, y, blur, spread] = valuesParsed.filter((v) => v !== color);
return { inset, x, y, blur, spread, color };
});
} catch (err) {
return void 0;
}
};

exports.cssBoxShadow = cssBoxShadow;
26 changes: 26 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const toNumber = (v) => isFinite(+v) ? +v : v;
const parseUnit = (v) => {
const match = v.match(/^(0?[-.]?\d+)(r?e[m|x]|v[h|w|min|max]+|p[x|t|c]|[c|m]m|%|s|in|ch)$/);
const res = match ? parseFloat(match[1]) || match[1] : v;
return toNumber(res);
};
const boxShadowParse = (string) => string.split(/\s(?![^(]*\))/).map((v) => parseUnit(v));
const getColor = (values) => values.find((v) => !isFinite(+v));
const cssBoxShadow = (stringBoxShadows) => {
try {
if (typeof stringBoxShadows !== "string")
throw new Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 box-shadow.");
return stringBoxShadows.split(/,(?![^\(]*\))/).map((stringBoxShadow) => {
const inset = stringBoxShadow.includes("inset");
stringBoxShadow = stringBoxShadow.replace("inset", "").trim();
const valuesParsed = boxShadowParse(stringBoxShadow);
const color = getColor(valuesParsed);
const [x, y, blur, spread] = valuesParsed.filter((v) => v !== color);
return { inset, x, y, blur, spread, color };
});
} catch (err) {
return void 0;
}
};

export { cssBoxShadow };
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "css-box-shadow",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "./index.js",
"types": "./index.d.ts",
"license": "MIT",
"author": {
"name": "Дмитрий Сединкин",
"url": "https://github.com/dsedinkin"
"name": "SedLab",
"url": "https://github.com/sedlab"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dsedinkin/css-box-shadow.git"
"url": "git+https://github.com/sedlab/css-box-shadow.git"
},
"dependencies": {}
}
17 changes: 0 additions & 17 deletions type.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions type.js

This file was deleted.

0 comments on commit cf73f95

Please sign in to comment.