-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstringHelpers.tsx
75 lines (72 loc) · 2.16 KB
/
stringHelpers.tsx
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
export const wordWrapText = (
text: string,
length: number,
spill: number,
): string[] => {
const wrapArray: string[] = []; // clear existing array
const wordArray = text.trim().split(' ');
const idealLength =
length && length.constructor === Number && length > 0
? length
: text.length / 5;
spill = spill && spill.constructor === Number && spill > 0 ? spill : 0;
const spillLength = idealLength + spill;
let nextWord: string,
nextWordLen: number,
valueLen: number,
newLength: number,
maxWordLength: number,
overflow: boolean,
eof: boolean,
tmp: string | null,
diff: number | null,
getNextWord: boolean,
candidate: string;
const wordLoop = (value?: string, i?: number): string[] => {
/* returns an array of trimmed lines */
i = i || 0;
tmp = diff = null;
value = value || wordArray[i] || '';
valueLen = value.length;
nextWord = wordArray[i + 1] || '';
nextWordLen = nextWord.length || 0;
newLength = valueLen + nextWordLen;
overflow = newLength >= idealLength && newLength >= spillLength;
maxWordLength = Math.max(idealLength, spillLength);
eof = !wordArray[i + 1];
getNextWord = !eof && !overflow;
candidate = value + ' ' + nextWord;
if (getNextWord) {
return wordLoop(candidate, i + 1);
}
/*
Exception catcher:
ensure the next word length is shorter than max length
*/
if (nextWordLen >= maxWordLength) {
tmp = candidate.slice(0, maxWordLength);
diff = candidate.length - tmp.length;
wordArray[i + 1] = nextWord.slice(-diff);
value = tmp;
}
/*
Exception catcher:
truncate extremely long strings, update word array,
and recurse the remainder
*/
if (value.length > maxWordLength) {
tmp = value.slice(0, maxWordLength);
diff = value.length - tmp.length;
wrapArray.push(tmp);
value = value.slice(-diff);
return wordLoop(value, i);
}
if (value) {
// Here is where you can do something with each row of text
wrapArray.push(value.trim());
return wordLoop('', i + 1);
}
return wrapArray;
};
return wordLoop();
};