Code coverage report for array/flatten.js

Statements: 100% (16 / 16)      Branches: 100% (6 / 6)      Functions: 100% (4 / 4)      Lines: 100% (15 / 15)     

All files » array/ » flatten.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371         1 7 2 2     5     5 11 11 5   6     5               1 2   1      
define(['../lang/isArray'], function (isArray) {
    /*
     * 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 (level === 0) {
            result.push.apply(result, arr);
            return;
        }
 
        var value,
            i = -1,
            n = arr.length;
        while (++i < n) {
            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, shallow) {
        return flattenTo(arr, [], shallow ? 1 : -1);
    }
    return flatten;
});