From 384e36a1bbe96ce2471aedbdb0db728009a8850b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A1=D0=B5?= =?UTF-8?q?=D0=B4=D0=B8=D0=BD=D0=BA=D0=B8=D0=BD?= Date: Tue, 11 Jul 2023 17:08:25 +0500 Subject: [PATCH] updates --- LICENSE | 21 -------------- index.d.ts | 12 +++++--- index.js | 82 +++++++++++++++++++++++++--------------------------- index.mjs | 45 ++++++++++++++++++++++++++++ package.json | 8 ++--- type.d.ts | 8 ----- type.js | 2 -- 7 files changed, 97 insertions(+), 81 deletions(-) delete mode 100644 LICENSE create mode 100644 index.mjs delete mode 100644 type.d.ts delete mode 100644 type.js diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 8ae0fbf..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022 SEDINKIN - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 83f376e..bb3892c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,8 @@ -import { TOpts } from "./type"; -export declare const cssToObject: (css: string, opts: TOpts) => { - [key: string]: any; -}; +type TOpts = { + numbers?: boolean; + camel?: boolean; +} | null; + +declare const cssToObject: (css: string, opts: TOpts) => Record; + +export { cssToObject }; diff --git a/index.js b/index.js index 8ab5dc2..5674daf 100644 --- a/index.js +++ b/index.js @@ -1,49 +1,47 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.cssToObject = void 0; -const stylis_1 = require("stylis"); +'use strict'; + +var stylis = require('stylis'); + const parseUnit = (value, returnValue = true, returnUnit = false) => { - const match = value.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 - ? { value: (parseFloat(match[1]) || match[1]), unit: match[2] } - : { value, unit: undefined }; - return returnValue ? res.value : returnUnit ? res.unit : res; + const match = value.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 ? { value: parseFloat(match[1]) || match[1], unit: match[2] } : { value, unit: void 0 }; + return returnValue ? res.value : returnUnit ? res.unit : res; }; -const parseToCamel = (value) => value - .replace(/(-[a-z])/g, x => x.toUpperCase()) - .replace(/-/g, ''); +const parseToCamel = (value) => value.replace(/(-[a-z])/g, (x) => x.toUpperCase()).replace(/-/g, ""); const parse = (opts) => (rules, result = {}) => { - rules.forEach((rule) => { - if (Array.isArray(rule.children)) { - let [key] = rule.props; - if (rule.type.includes("@media")) - key = `@media ${key}`; - const value = parse(opts)(rule.children); - if (Object.keys(result).includes(key)) - Object.assign(result[key], value); - else - result[key] = value; - } - else { - const key = opts?.camel ? parseToCamel(rule.props) : rule.props; - const value = opts?.numbers ? parseUnit(rule.children) : rule.children; - if (Object.keys(result).includes(key)) - Object.assign({ ...result[key] }, value); - else - Object.assign(result, { [key]: value }); - } - }); - return result; + rules.forEach((rule) => { + if (Array.isArray(rule.children)) { + let [key] = rule.props; + if (rule.type.includes("@media")) { + key = `@media ${key}`; + } + const value = parse(opts)(rule.children); + if (Object.keys(result).includes(key)) { + Object.assign(result[key], value); + } else { + result[key] = value; + } + } else { + const key = opts?.camel ? parseToCamel(rule.props) : rule.props; + const value = opts?.numbers ? parseUnit(rule.children) : rule.children; + if (Object.keys(result).includes(key)) { + Object.assign({ ...result[key] }, value); + } else { + Object.assign(result, { [key]: value }); + } + } + }); + return result; }; const cssToObject = (css, opts) => { - try { - const wrapped = (0, stylis_1.compile)(css); - const obj = parse(opts)(wrapped); - return obj; - } - catch (err) { - console.error("lib css-to-object: ", err); - return {}; - } + try { + const wrapped = stylis.compile(css); + const obj = parse(opts)(wrapped); + return obj; + } catch (err) { + console.error("lib css-to-object: ", err); + return {}; + } }; + exports.cssToObject = cssToObject; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..19a333e --- /dev/null +++ b/index.mjs @@ -0,0 +1,45 @@ +import { compile } from 'stylis'; + +const parseUnit = (value, returnValue = true, returnUnit = false) => { + const match = value.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 ? { value: parseFloat(match[1]) || match[1], unit: match[2] } : { value, unit: void 0 }; + return returnValue ? res.value : returnUnit ? res.unit : res; +}; +const parseToCamel = (value) => value.replace(/(-[a-z])/g, (x) => x.toUpperCase()).replace(/-/g, ""); +const parse = (opts) => (rules, result = {}) => { + rules.forEach((rule) => { + if (Array.isArray(rule.children)) { + let [key] = rule.props; + if (rule.type.includes("@media")) { + key = `@media ${key}`; + } + const value = parse(opts)(rule.children); + if (Object.keys(result).includes(key)) { + Object.assign(result[key], value); + } else { + result[key] = value; + } + } else { + const key = opts?.camel ? parseToCamel(rule.props) : rule.props; + const value = opts?.numbers ? parseUnit(rule.children) : rule.children; + if (Object.keys(result).includes(key)) { + Object.assign({ ...result[key] }, value); + } else { + Object.assign(result, { [key]: value }); + } + } + }); + return result; +}; +const cssToObject = (css, opts) => { + try { + const wrapped = compile(css); + const obj = parse(opts)(wrapped); + return obj; + } catch (err) { + console.error("lib css-to-object: ", err); + return {}; + } +}; + +export { cssToObject }; diff --git a/package.json b/package.json index c92a8f6..255efb5 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,17 @@ { "name": "css-to-object", - "version": "1.0.6", + "version": "1.0.7", "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-to-object.git" + "url": "git+https://github.com/sedlab/css-to-object.git" }, "dependencies": { "@types/stylis": "4.0.2", diff --git a/type.d.ts b/type.d.ts deleted file mode 100644 index 983e077..0000000 --- a/type.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -export declare type TOpts = { - numbers?: boolean; - camel?: boolean; -} | null; -export declare type TRule = { - [key: string]: any; -}; -export declare type TRules = Array; diff --git a/type.js b/type.js deleted file mode 100644 index c8ad2e5..0000000 --- a/type.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true });