.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / util / remove-indentation.js
1 'use strict';
2
3 var trim = require('trim');
4 var repeat = require('repeat-string');
5 var getIndent = require('./get-indentation');
6
7 module.exports = indentation;
8
9 var C_SPACE = ' ';
10 var C_NEWLINE = '\n';
11 var C_TAB = '\t';
12
13 /* Remove the minimum indent from every line in `value`.
14  * Supports both tab, spaced, and mixed indentation (as
15  * well as possible). */
16 function indentation(value, maximum) {
17   var values = value.split(C_NEWLINE);
18   var position = values.length + 1;
19   var minIndent = Infinity;
20   var matrix = [];
21   var index;
22   var indentation;
23   var stops;
24   var padding;
25
26   values.unshift(repeat(C_SPACE, maximum) + '!');
27
28   while (position--) {
29     indentation = getIndent(values[position]);
30
31     matrix[position] = indentation.stops;
32
33     if (trim(values[position]).length === 0) {
34       continue;
35     }
36
37     if (indentation.indent) {
38       if (indentation.indent > 0 && indentation.indent < minIndent) {
39         minIndent = indentation.indent;
40       }
41     } else {
42       minIndent = Infinity;
43
44       break;
45     }
46   }
47
48   if (minIndent !== Infinity) {
49     position = values.length;
50
51     while (position--) {
52       stops = matrix[position];
53       index = minIndent;
54
55       while (index && !(index in stops)) {
56         index--;
57       }
58
59       if (
60         trim(values[position]).length !== 0 &&
61         minIndent &&
62         index !== minIndent
63       ) {
64         padding = C_TAB;
65       } else {
66         padding = '';
67       }
68
69       values[position] = padding + values[position].slice(
70         index in stops ? stops[index] + 1 : 0
71       );
72     }
73   }
74
75   values.shift();
76
77   return values.join(C_NEWLINE);
78 }