Skip to content

Commit

Permalink
handle multi-line values with line breaks and parens
Browse files Browse the repository at this point in the history
  • Loading branch information
aproragadozo committed Nov 26, 2024
1 parent 69bc14d commit 5d794df
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,22 @@ function convertToObject(sourceString) {
// result object declaration
const styleSheet = {};

// split the string into lines
// filter out empty or whitespace-only lines
const lines = sourceString
.replace(/\n\s*\n/g, '\n') // remove empty lines
.split('\n')
.filter(line => line.trim() && line.trim() !== ';');

let currentKey = null;

// process lines
for (const line of lines) {
// check if this is the continuation of a previous line
if (currentKey && line.trim() && !line.includes(':')) {
// append to existing values
styleSheet[currentKey] += ' ' + line.trim();
continue;
}
// resume normal key-value parsing
const [key, value] = line.split(':').map(part => part.trim());

if (key && value) {
const cleanedValue = value.replace(/;+$/, '').trim();
// store the current key for potential continuation
currentKey = key;

// add to stylesheet result
styleSheet[key] = cleanedValue;
const processedString = sourceString
.replace(/\n\s*\n/g, '\n') // Remove completely empty lines
.replace(/;\s*\n\s*\)/g, ' )') // Handle semicolon-newline-closing parenthesis

Check failure on line 14 in src/convertToObject.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 82. Maximum allowed is 80
.replace(/\n\s*/g, ' '); // Replace newlines with spaces

// Split into declarations
const declarations = processedString
.split(';')
.filter(decl => decl.trim());

for (const declaration of declarations) {
const [key, ...valueParts] = declaration.split(':').map(part => part.trim());

if (key) {
const value = valueParts.join(':').trim().replace(/;+$/, '');
styleSheet[key] = value;
}
}
return styleSheet;
Expand Down

0 comments on commit 5d794df

Please sign in to comment.