-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
52 lines (43 loc) · 1.26 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*!
* align-text <https://github.com/jonschlinkert/align-text>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var typeOf = require('kind-of');
var repeat = require('repeat-string');
var longest = require('longest');
module.exports = function alignText(val, fn) {
var originalType = typeOf(val);
var lines = val;
if (originalType === 'string') {
lines = val.split(/(?:\r\n|\n)/);
} else if (originalType !== 'array') {
throw new TypeError('No input found. Provide a string or array to align.');
}
var fnType = typeOf(fn);
var max = longest(lines);
var len = lines.length;
var idx = -1;
var res = [];
while (++idx < len) {
var line = String(lines[idx]);
var diff;
if (fnType === 'function') {
diff = fn(line.length, max.length, line, lines, idx);
} else if (fnType === 'number') {
diff = fn;
} else {
diff = max.length - line.length;
}
if (typeOf(diff) === 'number') {
res.push(repeat(' ', diff) + line);
} else if (typeOf(diff) === 'object') {
var result = repeat(diff.character || ' ', diff.indent || 0);
res.push((diff.prefix || '') + result + line);
}
}
if (originalType === 'array') return res;
return res.join('\n');
};