Sha256: 63c35e8ebdd77629eb685f32dc52dcfd4e19f725943990c07d3cf1416544f632

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

define(['../lang/isArray', './append'], function (isArray, append) {

    /*
     * Helper function to flatten to a destination array.
     * Used to remove the need to create intermediate arrays while flattening.
     */
    function flattenTo(arr, result, level) {
        if (arr == null) {
            return result;
        } else if (level === 0) {
            append(result, arr);
            return result;
        }

        var value,
            i = -1,
            len = arr.length;
        while (++i < len) {
            value = arr[i];
            if (isArray(value)) {
                flattenTo(value, result, level - 1);
            } else {
                result.push(value);
            }
        }
        return result;
    }

    /**
     * Recursively flattens an array.
     * A new array containing all the elements is returned.
     * If `shallow` is true, it will only flatten one level.
     */
    function flatten(arr, level) {
        level = level == null? -1 : level;
        return flattenTo(arr, [], level);
    }

    return flatten;

});

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
entangled-0.0.16 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.15 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.14 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.13 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.12 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.11 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js
entangled-0.0.10 spec/dummy/public/node_modules/bower/node_modules/mout/src/array/flatten.js