es6-comprehensions
Version:
Transforms ES6 Array Comprehensions to ES5 compliant equivalent.
42 lines (34 loc) • 1.04 kB
JavaScript
;
var utils = module.exports;
/**
* This function is borrowed from https://github.com/square/es6-arrow-function/blob/master/lib/util.js
*
* Copyright (c) 2013, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
utils.guessTabWidth = function(source) {
var counts = []; // Sparse array.
var lastIndent = 0;
source.split("\n").forEach(function(line) {
var indent = /^\s*/.exec(line)[0].length;
var diff = Math.abs(indent - lastIndent);
counts[diff] = ~~counts[diff] + 1;
lastIndent = indent;
});
var maxCount = -1;
var result = 2;
for (var tabWidth = 1;
tabWidth < counts.length;
tabWidth += 1) {
if (tabWidth in counts &&
counts[tabWidth] > maxCount) {
maxCount = counts[tabWidth];
result = tabWidth;
}
}
return result;
};