Spaces:
Sleeping
Sleeping
File size: 4,107 Bytes
be5030f |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
'use strict';
// Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment
const INDENT_REGEX = /^(?:( )+|\t+)/;
const INDENT_TYPE_SPACE = 'space';
const INDENT_TYPE_TAB = 'tab';
// Make a Map that counts how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
// The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
//
// indents = {
// t3: [1, 0],
// t4: [1, 5],
// s5: [1, 0],
// s12: [1, 0],
// }
function makeIndentsMap(string, ignoreSingleSpaces) {
const indents = new Map();
// Remember the size of previous line's indentation
let previousSize = 0;
let previousIndentType;
// Indents key (ident type + size of the indents/unindents)
let key;
for (const line of string.split(/\n/g)) {
if (!line) {
// Ignore empty lines
continue;
}
let indent;
let indentType;
let weight;
let entry;
const matches = line.match(INDENT_REGEX);
if (matches === null) {
previousSize = 0;
previousIndentType = '';
} else {
indent = matches[0].length;
if (matches[1]) {
indentType = INDENT_TYPE_SPACE;
} else {
indentType = INDENT_TYPE_TAB;
}
// Ignore single space unless it's the only indent detected to prevent common false positives
if (ignoreSingleSpaces && indentType === INDENT_TYPE_SPACE && indent === 1) {
continue;
}
if (indentType !== previousIndentType) {
previousSize = 0;
}
previousIndentType = indentType;
weight = 0;
const indentDifference = indent - previousSize;
previousSize = indent;
// Previous line have same indent?
if (indentDifference === 0) {
weight++;
// We use the key from previous loop
} else {
const absoluteIndentDifference = indentDifference > 0 ? indentDifference : -indentDifference;
key = encodeIndentsKey(indentType, absoluteIndentDifference);
}
// Update the stats
entry = indents.get(key);
if (entry === undefined) {
entry = [1, 0]; // Init
} else {
entry = [++entry[0], entry[1] + weight];
}
indents.set(key, entry);
}
}
return indents;
}
// Encode the indent type and amount as a string (e.g. 's4') for use as a compound key in the indents Map.
function encodeIndentsKey(indentType, indentAmount) {
const typeCharacter = indentType === INDENT_TYPE_SPACE ? 's' : 't';
return typeCharacter + String(indentAmount);
}
// Extract the indent type and amount from a key of the indents Map.
function decodeIndentsKey(indentsKey) {
const keyHasTypeSpace = indentsKey[0] === 's';
const type = keyHasTypeSpace ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB;
const amount = Number(indentsKey.slice(1));
return {type, amount};
}
// Return the key (e.g. 's4') from the indents Map that represents the most common indent,
// or return undefined if there are no indents.
function getMostUsedKey(indents) {
let result;
let maxUsed = 0;
let maxWeight = 0;
for (const [key, [usedCount, weight]] of indents) {
if (usedCount > maxUsed || (usedCount === maxUsed && weight > maxWeight)) {
maxUsed = usedCount;
maxWeight = weight;
result = key;
}
}
return result;
}
function makeIndentString(type, amount) {
const indentCharacter = type === INDENT_TYPE_SPACE ? ' ' : '\t';
return indentCharacter.repeat(amount);
}
module.exports = string => {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
// Identify indents while skipping single space indents to avoid common edge cases (e.g. code comments)
// If no indents are identified, run again and include all indents for comprehensive detection
let indents = makeIndentsMap(string, true);
if (indents.size === 0) {
indents = makeIndentsMap(string, false);
}
const keyOfMostUsedIndent = getMostUsedKey(indents);
let type;
let amount = 0;
let indent = '';
if (keyOfMostUsedIndent !== undefined) {
({type, amount} = decodeIndentsKey(keyOfMostUsedIndent));
indent = makeIndentString(type, amount);
}
return {
amount,
type,
indent
};
};
|