-
Notifications
You must be signed in to change notification settings - Fork 0
/
paragraphs-lines.js
81 lines (75 loc) · 2.52 KB
/
paragraphs-lines.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
export function paragraphsLines(text) {
const lines = text.trim().split('\n');
const textTitle = {
titleText: lines[0],
titleData: {
titleWordCount: lines[0].split(' ').length,
titleCharacterLength: lines[0].length
}
};
const paragraphs = [];
let paragraph = {
paragraphIndex: 0,
paragraphData: {
paragraphLineCount: 0,
paragraphWordCount: 0,
paragraphCharacterLength: 0
},
paragraphLines: []
};
let textLineCount = 0;
let textWordCount = 0;
let textCharacterLength = 0;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (i === 1 && line.trim() === '') continue; // Skip the empty line after the title
textLineCount++;
textWordCount += line.split(' ').length;
textCharacterLength += line.length;
if (line.trim() === '') {
if (paragraph.paragraphLines.length > 0) {
paragraphs.push(paragraph);
paragraph = {
paragraphIndex: paragraphs.length,
paragraphData: {
paragraphLineCount: 0,
paragraphWordCount: 0,
paragraphCharacterLength: 0
},
paragraphLines: []
};
}
paragraph.paragraphLines.push({
lineIndex: textLineCount - 1,
lineText: null,
lineData: {
lineWordCount: 0,
lineCharacterLength: 0
}
});
paragraph.paragraphData.paragraphLineCount++;
} else {
paragraph.paragraphLines.push({
lineIndex: textLineCount - 1,
lineText: line,
lineData: {
lineWordCount: line.split(' ').length,
lineCharacterLength: line.length
}
});
paragraph.paragraphData.paragraphLineCount++;
paragraph.paragraphData.paragraphWordCount += line.split(' ').length;
paragraph.paragraphData.paragraphCharacterLength += line.length;
}
}
if (paragraph.paragraphLines.length > 0) {
paragraphs.push(paragraph);
}
return {
textTitle: textTitle,
textLineCount: textLineCount,
textWordCount: textWordCount,
textCharacterLength: textCharacterLength,
textParagraphs: paragraphs
};
}