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

Code before:

import isObject from '../util/is_object';

const isBlock = item => {
    // If an object, or kv entries, this is considered a block
    return isObject(item) || (Array.isArray(item) && item.every(i => Array.isArray(i) && i.length === 2));
};

const longestKey = items => {
    let longest = 0;

    for (const item of items) {
        // Only consider up to the first block
        if (isBlock(item[1]))
            return longest;

        // If this is the new longest, and not a comment, use this
        if (item[0].length > longest && !item[0].startsWith('#'))
            longest = item[0].length;
    }

    // Done!
    return longest;
};

const recurse = (entriesOrObject, depth, endLine = ';') => {
    // Support an object or kv array entries
    // Convert to entries if given an object
    const entries = isObject(entriesOrObject) ? Object.entries(entriesOrObject) : entriesOrObject;

    // If not a valid kv entries array, return
    if (!Array.isArray(entries) || !entries.every(i => Array.isArray(i) && i.length === 2)) return '';

    // Initial values
    let retVal = '';
    let longestKeyLen = longestKey(entries);
    const indent = ('    ').repeat(depth);

    // Track whether the previous was a block, for indentation
    let previousBlock = false;

    // Loop over every kv pair
    for (let i = 0; i < entries.length; i++) {
        const item = entries[i];

        // If a block (object or kv entries), recurse
        if (isBlock(item[1])) {
            // Recurse
            retVal += '\n' + indent + item[0] + ' {\n';
            retVal += recurse(item[1], depth + 1);
            retVal += indent + '}\n\n';

Review comment (suggestion):
```suggestion
const recurse = (entriesOrObject, depth) => {
```
This change can be reverted

Code after:

import isObject from '../util/is_object';

const isBlock = item => {
    // If an object, or kv entries, this is considered a block
    return isObject(item) || (Array.isArray(item) && item.every(i => Array.isArray(i) && i.length === 2));
};

const longestKey = items => {
    let longest = 0;

    for (const item of items) {
        // Only consider up to the first block
        if (isBlock(item[1]))
            return longest;

        // If this is the new longest, and not a comment, use this
        if (item[0].length > longest && !item[0].startsWith('#'))
            longest = item[0].length;
    }

    // Done!
    return longest;
};

const recurse = (entriesOrObject, depth) => {
    // Support an object or kv array entries
    // Convert to entries if given an object
    const entries = isObject(entriesOrObject) ? Object.entries(entriesOrObject) : entriesOrObject;

    // If not a valid kv entries array, return
    if (!Array.isArray(entries) || !entries.every(i => Array.isArray(i) && i.length === 2)) return '';

    // Initial values
    let retVal = '';
    let longestKeyLen = longestKey(entries);
    const indent = ('    ').repeat(depth);

    // Track whether the previous was a block, for indentation
    let previousBlock = false;

    // Loop over every kv pair
    for (let i = 0; i < entries.length; i++) {
        const item = entries[i];

        // If a block (object or kv entries), recurse
        if (isBlock(item[1])) {
            // Recurse
            retVal += '\n' + indent + item[0] + ' {\n';
            retVal += recurse(item[1], depth + 1);
            retVal += indent + '}\n\n';