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 8e29527 commit 384e36a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 81 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;

export { cssToObject };
82 changes: 40 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
45 changes: 45 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -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 };
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 0 additions & 8 deletions type.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions type.js

This file was deleted.

0 comments on commit 384e36a

Please sign in to comment.