Blame view

node_modules/es5-ext/array/#/flatten.js 877 Bytes
f7563de62   Palak Handa   first commit
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
37
38
39
  // Stack grow safe implementation
  
  'use strict';
  
  var ensureValue = require('../../object/valid-value')
  
    , isArray = Array.isArray, hasOwnProperty = Object.prototype.hasOwnProperty;
  
  module.exports = function () {
  	var input = ensureValue(this), index = 0, remaining, remainingIndexes, l, i, result = [];
  	main: //jslint: ignore
  	while (input) {
  		l = input.length;
  		for (i = index; i < l; ++i) {
  			if (!hasOwnProperty.call(input, i)) continue;
  			if (isArray(input[i])) {
  				if (i < (l - 1)) {
  					if (!remaining) {
  						remaining = [];
  						remainingIndexes = [];
  					}
  					remaining.push(input);
  					remainingIndexes.push(i + 1);
  				}
  				input = input[i];
  				index = 0;
  				continue main;
  			}
  			result.push(input[i]);
  		}
  		if (remaining) {
  			input = remaining.pop();
  			index = remainingIndexes.pop();
  		} else {
  			input = null;
  		}
  	}
  	return result;
  };