Repository: digitalocean/nginxconfig.io
File: src/nginxconfig/generators/to_conf.js (JavaScript)

Code before:
            retVal += recurse(item[1], depth + 1);
            retVal += indent + '}\n\n';

            // Done
            previousBlock = true;
            continue;
        }

        // Update key length if we've just left a block
        if (previousBlock) {
            longestKeyLen = longestKey(entries.slice(i));
            previousBlock = false;
        }

        // Otherwise, assume it can be made a string
        // Ensure we're working with an array
        const val = Array.isArray(item[1]) ? item[1] : [item[1]];

        // Calculate spacing
        const keyValSpacing = (longestKeyLen - item[0].length) + 1;
        const keyValIndent = (' ').repeat(Math.max(keyValSpacing, 0));

        // Work through each item in the array
        val.forEach(subVal => {
            const val = subVal.toString();
            retVal += indent + (item[0] + keyValIndent + val).trim() + (item[0].startsWith('#') ? '' : endLine) + '\n';
        });
    }

    return retVal;
};

export default entriesOrObject => {
    // Generate the conf
    let conf = recurse(entriesOrObject, 0);

    // Do some regex cleanup
    conf = conf
        // Cleanup triple linebreaks
        .replace(/\n\n\n/g, '\n\n')
        // Double linebreak before comment
        .replace(/^([^\S\r\n]*[^#\s].*[^\n])\n([^\S\r\n]*)#/gm, '$1\n\n$2#')
        // Single linebreak between comment and block
        .replace(/^([^\S\r\n]*#.*)(?:\n[^\S\r\n]*)+\n([^\S\r\n]*.*{)/gm, '$1\n$2')
        // Double linebreak after double comment
        .replace(/^([^\S\r\n]*#.*\n[^\S\r\n]*#.*\n)([^\S\r\n]*[^#\s])/gm, '$1\n$2')
        // No newline for empty blocks
        .replace(/^([^\S\r\n]*.*{)\n[^\S\r\n]*(})/gm, '$1$2');

    // Cleanup extra linebreaks between multiple close blocks
    // Use a loop as this has overlapping matches

Review comment (suggestion):
```suggestion
            retVal += indent + (item[0] + keyValIndent + val).trim() + (item[0].startsWith('#') ? '' : ';') + '\n';
```
This change can be reverted

Code after:
            retVal += recurse(item[1], depth + 1);
            retVal += indent + '}\n\n';

            // Done
            previousBlock = true;
            continue;
        }

        // Update key length if we've just left a block
        if (previousBlock) {
            longestKeyLen = longestKey(entries.slice(i));
            previousBlock = false;
        }

        // Otherwise, assume it can be made a string
        // Ensure we're working with an array
        const val = Array.isArray(item[1]) ? item[1] : [item[1]];

        // Calculate spacing
        const keyValSpacing = (longestKeyLen - item[0].length) + 1;
        const keyValIndent = (' ').repeat(Math.max(keyValSpacing, 0));

        // Work through each item in the array
        val.forEach(subVal => {
            const val = subVal.toString();
            retVal += indent + (item[0] + keyValIndent + val).trim() + (item[0].startsWith('#') ? '' : ';') + '\n';
        });
    }

    return retVal;
};

export default entriesOrObject => {
    // Generate the conf
    let conf = recurse(entriesOrObject, 0);

    // Do some regex cleanup
    conf = conf
        // Cleanup triple linebreaks
        .replace(/\n\n\n/g, '\n\n')
        // Double linebreak before comment
        .replace(/^([^\S\r\n]*[^#\s].*[^\n])\n([^\S\r\n]*)#/gm, '$1\n\n$2#')
        // Single linebreak between comment and block
        .replace(/^([^\S\r\n]*#.*)(?:\n[^\S\r\n]*)+\n([^\S\r\n]*.*{)/gm, '$1\n$2')
        // Double linebreak after double comment
        .replace(/^([^\S\r\n]*#.*\n[^\S\r\n]*#.*\n)([^\S\r\n]*[^#\s])/gm, '$1\n$2')
        // No newline for empty blocks
        .replace(/^([^\S\r\n]*.*{)\n[^\S\r\n]*(})/gm, '$1$2');

    // Cleanup extra linebreaks between multiple close blocks
    // Use a loop as this has overlapping matches