-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
64 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |