-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
102 lines (83 loc) · 2.27 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
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
const border = require('./lib/border')
const {
EOL,
PAD,
parsePadding,
getOptions,
centerText
} = require('./lib/utils')
module.exports = (text, options) => {
let result = ''
const strArr = text.split(EOL)
const arr = strArr.map(item => item.length)
const myOptions = getOptions(options)
const {
height,
width,
style,
title,
padding,
textAlign,
titleAlign,
marginLeft,
marginTop,
marginBottom
} = myOptions
let max = Math.max.apply(null, [title.length, width, Math.max.apply(null, arr)])
const boxHeight = Math.max.apply(null, [strArr.length, height])
const { tl, tr, bl, br, v, h } = border[style]
const { pt, pr, pl, pb } = parsePadding(padding)
result += tl
if (title) {
max += 2
}
const header = h.repeat(max - title.length + pl + pr - 2)
const padTR = tr + EOL
switch (titleAlign) {
case 'center':
result += h + centerText(max, title, h) + h + padTR
break
case 'right':
result += header + h
result += title + h + padTR
break
case 'left':
default:
result += h + title
result += header + h + padTR
}
const total = max + pl + pr
const hr = v + PAD.repeat(total) + v + EOL
result += hr.repeat(pt)
strArr.map(str => {
const padLeft = v + PAD.repeat(pl)
const padRight = PAD.repeat(pr) + v + EOL
if (str) {
result += padLeft
result += textAlign === 'center' ? centerText(max, str, PAD)
: textAlign === 'right' ? str.padStart(max)
: str.padEnd(max)
result += padRight
}
})
result += hr.repeat(boxHeight - strArr.length)
result += hr.repeat(pb)
result += bl + h.repeat(total) + br
// Add left margin
let resultLeftMargin = ''
result.split(EOL).forEach(line => {
let tempLine = ''
for (let i = 0; i < marginLeft; i++) tempLine += ' '
resultLeftMargin += tempLine + line + EOL
})
// Add top margin
let resultTopMargin = ''
for (let i = 0; i < marginTop; i++) resultTopMargin += EOL
// Add bottom margin
let resultBottomMargin = ''
for (let i = 0; i < marginBottom; i++) resultBottomMargin += EOL
let realResult = resultTopMargin + resultLeftMargin
realResult = realResult.substr(0, realResult.length - 1)
realResult += resultBottomMargin
return realResult
}