-
Notifications
You must be signed in to change notification settings - Fork 2
/
MindNode.js
64 lines (55 loc) · 1.65 KB
/
MindNode.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
'use strict';
const PLUS = '+';
const HENG = '-';
const SHU = '|';
const BLANK = '\u3000';
function byteLength(str) {
// returns the byte length of an utf8 string
var s = 0;
for(var i = str.length - 1; i >= 0; i--) {
var code = str.charCodeAt(i);
if(code > 0x7f && code <= 0x7ff) s += 1;
else if(code > 0x7ff && code <= 0xffff) s += 1;
else s = s + 9.7 / 17;
// if(code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate
}
return s;
}
//将一串中英混合的字符串变成一个固定长度的数组
var transStrToMustLengthArray = function (str, len) {
var arr = str.split('');
while(arr.length != len) {
var temp = arr.shift();
arr[0] = temp + arr[0];
}
return arr;
}
class MindNode {
constructor(str) {
this.str = str;
var strLength = byteLength(this.str, 'utf8');
console.log(strLength)
var mustLength = Math.ceil(strLength); // 中间文字的长度
this.map = [
[PLUS, HENG].concat(HENG.repeat(mustLength).split('')).concat([HENG, PLUS]),
[SHU, BLANK].concat(transStrToMustLengthArray(this.str, mustLength)).concat([BLANK, SHU]),
[PLUS, HENG].concat(HENG.repeat(mustLength).split('')).concat([HENG, PLUS])
];
this.w = mustLength + 4;
this.h = 3;
this.x = 0;
this.y = 0;
this.children = [];
}
addChild(node) {
this.children.push(node);
}
output() {
var result = '';
this.map.forEach(function (m) {
result += m.join('') + '\n'
})
return result;
}
}
module.exports = MindNode;