Skip to content

Instantly share code, notes, and snippets.

@trolit
Created January 27, 2022 17:19
Show Gist options
  • Save trolit/c891a5e65485829078207817ea7c92c9 to your computer and use it in GitHub Desktop.
Save trolit/c891a5e65485829078207817ea7c92c9 to your computer and use it in GitHub Desktop.
Extracts information about code structure that was passed as an string

Simple generic sample that allows to extract information about code structure which can be later analyzed in context of "whole part". See examples for demonstration.

/**
 * returns array that gives information about content structure **(based on curly brackets)**
 * @param {string} decoded_content decoded file content
 *
 * @example
 * 0 | 'module.exports = () => {'
 * 1 | 'if (condition1) {'
 * 2 | 'if (condition2) {'
 * 3 | 'console.log(\'log\');'
 * 4 | '}'
 * 5 | '}'
 * 6 | '}'
 *
 * // yields
 * [
 *  { from: 0, to: 6 },
 *  { from: 1, to: 5 },
 *  { from: 2, to: 4 }
 * ]
 *
 * @returns {Array<{from, to}>}
 */
module.exports = (decoded_content) => {
    const split_content = decoded_content.split('\n');

    let content_nests = [];

    for (let row = 0; row < split_content.length; row++) {
        const rowContent = split_content[row];

        for (let charIndex = 0; charIndex < rowContent.length; charIndex++) {
            const char = rowContent[charIndex];

            if (char === '{') {
                content_nests.push({
                    from: row,
                });
            } else if (char === '}') {
                for (let i = content_nests.length - 1; i >= 0; i--) {
                    if (content_nests[i].to) {
                        continue;
                    }

                    content_nests[i].to = row;

                    break;
                }
            }
        }
    }

    return content_nests;
};
Example 1

Input (raw)

<script>
export default {
 data(): {
     return {
         isOk: true
     }
 },
 
 methods : {
   test1() {
     console.log('test1');    
   },
   
   test2() {
     const x = 2;

     if (x === 2) {
	console.log('test2');
     }
   }
 }
};
</script>

Input (split)

┌─────────┬────────────────────────────────────┐
 (index)                Values               
├─────────┼────────────────────────────────────┤
    0                 '<script>'             
    1             'export default {'         │
│    2    │            ' data(): {'            │
│    3    │          '     return {'           │
│    4    │       '         isOk: true'        │
│    5    │              '     }'              │
│    6    │               ' },'                │
│    7    │                ' '                 │
│    8    │           ' methods : {'           │
│    9    │           '   test1() {'           │
│   10    │ '     console.log(\'test1\');    ' │
│   11    │              '   },'               │
│   12    │               '   '                │
│   13    │           '   test2() {'           │
│   14    │        '     const x = 2;'         │
│   15    │                 ''                 │
│   16    │       '     if (x === 2) {'        │
│   17    │    '\tconsole.log(\'test2\');'     │
│   18    │              '     }'              │
│   19    │               '   }'               │
│   20    │                ' }'                │
│   21    │                '};'                │
│   22    │            '</script>'             │
└─────────┴────────────────────────────────────┘

Output

┌─────────┬──────┬────┐
│ (index) │ from │ to │
├─────────┼──────┼────┤
│    0    │  1   │ 21 │
│    1    │  2   │ 6  │
│    2    │  3   │ 5  │
│    3    │  8   │ 20 │
│    4    │  9   │ 11 │
│    5    │  13  │ 19 │
│    6    │  16  │ 18 │
└─────────┴──────┴────┘
Example 2

Input (raw)

// SIMPLE "FLAVOUR" TESTS

// breakline above return
function test1() {
    const w = 2;   
    return w;
}

// unnecessary breakline
function test2() {

    return 2;
}

// use const instead of let & var
function test3() {
    let w = 55;
    var e = 4;

    return (w + e);
}

function test4() {

}

// typo
funetion test5() {
    return 2;
}

Input (split)

┌─────────┬─────────────────────────────────────┐
 (index)                Values                
├─────────┼─────────────────────────────────────┤
    0         '// SIMPLE "FLAVOUR" TESTS'     │
    1                     ''                  
    2         '// breakline above return'     │
    3            'function test1() {'         
    4            '    const w = 2;   '        │
│    5    │           '    return w;'           │
│    6    │                 '}'                 │
│    7    │                 ''                  │
│    8    │     '// unnecessary breakline'      │
    9            'function test2() {'         
   10                     ''                  │
│   11    │           '    return 2;'           │
│   12    │                 '}'                 │
│   13    │                 ''                  │
│   14    │ '// use const instead of let & var' │
   15            'function test3() {'         │
│   16    │          '    let w = 55;'          │
│   17    │          '    var e = 4;'           │
│   18    │                 ''                  │
│   19    │        '    return (w + e);'        │
│   20    │                 '}'                 │
│   21    │                 ''                  │
│   22    │        'function test4() {'         │
│   23    │               '    '                │
│   24    │                 '}'                 │
│   25    │                 ''                  │
│   26    │              '// typo'              │
   27            'funetion test5() {'         
   28               '    return 2;'           │
│   29    │                 '}'                 │
│   30    │                 ''                  │
│   31    │                 ''                  │
│   32    │                 ''                  │
│   33    │                 ''                  
└─────────┴─────────────────────────────────────┘

Output

┌─────────┬──────┬────┐
│ (index) │ from │ to │
├─────────┼──────┼────┤
│    0    │  3   │ 6  │
│    1    │  9   │ 12 │
│    2    │  15  │ 20 │
│    3    │  22  │ 24 │
│    4    │  27  │ 29 │
└─────────┴──────┴────┘

check in Programiz compiler :}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment