From 2e5f3bb75538dd65a3ae0b24ef5d581142f67002 Mon Sep 17 00:00:00 2001 From: smtaha512 Date: Wed, 20 Oct 2021 20:04:00 +0200 Subject: [PATCH] feat: add functionality to parse property names that start with hyphen --- extension.js | 86 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/extension.js b/extension.js index e42fc1c..bf60099 100644 --- a/extension.js +++ b/extension.js @@ -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() {}