Skip to content

fix : css <=> js conversion #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2022
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
2 changes: 1 addition & 1 deletion packages/css-in-js-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css-in-js-helpers",
"version": "1.0.1",
"version": "1.0.2",
"description": "CSS in JS helpers",
"main": "dist/index.js",
"scripts": {
Expand Down
30 changes: 22 additions & 8 deletions packages/css-in-js-helpers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const RN_COMPLETIONS = require('../completions-rn.json')
const getBeginningWhitespace = string =>
string.match(/^\s+/) !== null ? string.match(/^\s+/)[0] : ''

const isCSS = item => (item.match(/;/g) || []).length === 1
// return if the item ends with ; and such semi-colon the only one in the item
const isCSS = item =>
item.trim().endsWith(';') && (item.match(/;/g) || []).length == 1

const isJS = item => (item.match(/,/g) || []).length === 1
// return if the item ends with , such comma is the only one which is not wrapped with quotes
const isJS = item =>
item.trim().endsWith(',') &&
item.match(/(,)(?=(?:[^"']|("|')[^"']*("|'))*$)/g).length === 1

const toHyphen = prop =>
prop.replace(/([A-Z])/g, char => `-${char[0].toLowerCase()}`)
Expand All @@ -15,17 +20,26 @@ const toCamel = prop =>
prop.replace(/-(\w|$)/g, (dash, next) => next.toUpperCase())

const toJS = item => {
const [prop, val] = item.split(':')
let [prop, val] = item.split(/:(.+)/, 2) // in case of bg-url, the value might contain colon :
val = val.trim().slice(0, -1) // remove trailing semi-colon
let wrappingQuotes = "'" // handle if the property already contains quotes
if (!isNaN(val)) {
wrappingQuotes = ''
} else if (val.includes("'") && val.includes('"')) {
return item
} else if (val.includes("'")) {
wrappingQuotes = '"'
}
return `${getBeginningWhitespace(prop)}${toCamel(
prop.trim()
)}: '${val.trim().replace(';', '')}',`
)}: ${wrappingQuotes}${val}${wrappingQuotes},`
}

const toCSS = item => {
const [prop, val] = item.split(':')
return `${getBeginningWhitespace(prop)}${toHyphen(
prop.trim()
)}: ${val.trim().replace(/'|,/g, '')};`
let [prop, val] = item.split(/:(.+)/, 2)
val = val.trim().slice(0, -1) // remove trailing comma
return `${getBeginningWhitespace(prop)}${toHyphen(prop.trim())}: ${isNaN(val.trim()) ? val.slice(1, -1) : Number(val)
};`
}

const firstCharsEqual = (str1, str2) => str1[0].toLowerCase() === str2[0].toLowerCase()
Expand Down