Skip to content

feat: add functionality to parse property names that start with hyphen #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
86 changes: 52 additions & 34 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,69 @@ function activate(context) {
'react-css-copypaste.pasteCSS',
function () {
const clipboard = clipboardy.readSync();
let paste = '';
if (clipboard) {
const rows = clipboard.trim().split('\n');
rows.forEach((row) => {
const _row = row.trim().split(':');
let rules = _row[0].trim().split('-');
let rule = rules.shift();
for (let i = 0; i < rules.length; i++) {
rule +=
rules[i][0].toUpperCase() +
rules[i].substring(1, rules[i].length).toLowerCase();
}
let value = _row[1].trim().replace(';', '');
paste += `${rule}: '${value}',\n`;
});
clipboardy.writeSync(paste);
vscode.commands.executeCommand('editor.action.clipboardPasteAction'); // this should be formated paste
// clipboardy.writeSync(clipboard);
}
let paste = !clipboard ? '' : convertCss(clipboard);
clipboardy.writeSync(paste);
vscode.commands.executeCommand('editor.action.clipboardPasteAction'); // this should be formatted paste
}
);

context.subscriptions.push(disposable);
}

function convertCss(css = '') {
const converted = css
.trim()
.split('\n')
.map((item) => item.trim().split(':'))
.reduce(
(acc, [key, value]) => ({
...acc,
[key
.split('-')
.map((item, idx) =>
idx === 0
? item
: item.slice(0, 1).toUpperCase() + item.slice(1, item.length)
)
.join('')]: value.trim(),
}),
{}
);
return converted;
.reduce(rowIntoJsObject, {});

return JSON.stringify(converted, null, 2)
.split('\n')
.map(convertCssCommentIntoJsComment)
.join('\n')
.replace('{', '')
.replace('}', '')
.trim();
}

function rowIntoJsObject(accumulatedRow = {}, [key, value] = ['', '']) {
const propertyName = generatePropertyName(key);
const sanitizedValue = generateSanitizedValue(value);
return {
...accumulatedRow,
[propertyName]: sanitizedValue,
};
}

function generatePropertyName(key = '') {
if (key.startsWith('-')) {
return key;
}
return kebabToCamel(key);
}

function kebabToCamel(kebabCasedString = '') {
const [firstSection, ...remainingSplittedPropertyName] =
kebabCasedString.split('-');
return [
firstSection,
remainingSplittedPropertyName.map(
(item = '') => item.slice(0, 1).toUpperCase() + item.slice(1, item.length)
),
].join('');
}

function generateSanitizedValue(value = '') {
const sanitizedValue = value.trim().replace(';', '');
return Number.isNaN(+sanitizedValue) ? sanitizedValue : +sanitizedValue;
}

function convertCssCommentIntoJsComment(row = '') {
return !row.includes('/*')
? row
: '//'.concat(row.replace('/*', '').replace('*/', '').trim());
}

// this method is called when your extension is deactivated
function deactivate() {}

Expand Down