Skip to content

Instantly share code, notes, and snippets.

@ronaldroe
Last active December 13, 2023 15:27
Show Gist options
  • Select an option

  • Save ronaldroe/8ac6ad5861e7650e52a27a04aa705ef8 to your computer and use it in GitHub Desktop.

Select an option

Save ronaldroe/8ac6ad5861e7650e52a27a04aa705ef8 to your computer and use it in GitHub Desktop.

Revisions

  1. ronaldroe revised this gist Dec 13, 2023. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion flatten-test.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    // Decided against a testing framework for something so simple.
    // This test takes an array, loops through each top level index
    // and logs an error if the index contains an array.
    // Passes are tallied, and passes/fails are output at the end of the test.
  2. ronaldroe revised this gist Mar 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flatten.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ const flatten = arr => {

    // If the current item is an array, recurse into it.
    // It's usually better to not use recursion, but in this case,
    // it makes it easier to continue to the next level.
    // depth is unknown, and recursion makes sense.
    } else {

    // Spread the results of the recursion into the output array.
  3. ronaldroe revised this gist Mar 9, 2020. No changes.
  4. ronaldroe created this gist Mar 9, 2020.
    35 changes: 35 additions & 0 deletions flatten-test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    // Decided against a testing framework for something so simple.
    // This test takes an array, loops through each top level index
    // and logs an error if the index contains an array.
    // Passes are tallied, and passes/fails are output at the end of the test.

    const flattenTest = arr => {

    // Define error message and initialize pass counter
    const errorMessage = ind => `Index ${ind} contains an array`;
    let passCount = 0;

    arr.forEach((item, ind) => {

    // If the item is an array, log the message
    if(Array.isArray(item)){

    console.error(errorMessage(ind));

    // If not an array, increment pass counter
    } else {

    passCount++;

    }

    });

    // Log out results
    console.log(`${arr.length} total top level indexes.`);
    console.log(`${passCount} tests passed.`);
    console.log(`${arr.length - passCount} tests failed.`);

    }

    module.exports = flattenTest;
    30 changes: 30 additions & 0 deletions flatten.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    const flatten = arr => {

    let outputArr = [];

    // Loop through each index of the array
    arr.forEach(item => {

    // If the current item is not an array, push its value immediately.
    if(!Array.isArray(item)){

    outputArr.push(item);

    // If the current item is an array, recurse into it.
    // It's usually better to not use recursion, but in this case,
    // it makes it easier to continue to the next level.
    } else {

    // Spread the results of the recursion into the output array.
    outputArr.push(...flatten(item));

    }

    });

    return outputArr;

    }

    // Created for use with Node or other CommonJS
    module.exports = flatten;
    15 changes: 15 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // Run this file using `node index.js` to see results.
    const flattenTest = require('./flatten-test.js');
    const flatten = require('./flatten.js');

    const arr = [[1,2,[3]],4];

    // First, test flatten function
    console.log('Running example array with flatten function');

    flattenTest(flatten(arr));

    // Second, show test failures using input array
    console.log('\nRunning example array without flatten function')

    flattenTest(arr);