-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpedant.js
86 lines (77 loc) · 2.66 KB
/
pedant.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
var pedant = {
validate : function(lines) {
var inQuote = false;
var currQuoteLength = 0;
var quoteStartIndex = 0;
var MAX_QUOTE_LENGTH = 20;
var MAX_PUNCTUATION_LENGTH = 3;
var punctuation = [ ',', '.', '!', ';', ';', '"' ];
var punctuationAmount = 0;
var punctuationStartIndex = 0;
var quotes = [ '"', "'" ];
var contractionEndings =
[ 't', 's', 'm', 're', 's', 've', 'd', 'll', 'em' ];
function printError(msg, type, line, col) {
console.log(msg + ", " + type + ", line " + (line + 1) + ", col " +
(col + 1));
}
lines = lines.split("\n");
for (var j = 0; j < lines.length; j++) {
text = lines[j];
for (var i = 0; i < text.length; i++) {
if ("(".indexOf(text[i]) > -1) {
if ("(".indexOf(text[i + 1]) > -1) {
printError("too many parenthesis", "PunctuationError", j, i);
}
}
if (punctuation.indexOf(text[i]) > -1) {
if (punctuationAmount == 0) {
punctuationStartIndex = i;
}
punctuationAmount++;
if ((text[i + 1] != ")") && (text[i + 1] != " ")) {
printError("no space after punctuation", "PunctuationError", j,
punctuationStartIndex);
}
} else {
punctuationStartIndex = 0;
punctuationAmount = 0;
}
if (punctuationAmount > MAX_PUNCTUATION_LENGTH) {
printError("too much punctuation", "PunctuationError", j,
punctuationStartIndex);
punctuationStartIndex = 0;
punctuationAmount = 0;
}
// on a space
if (" ".indexOf(text[i]) > -1) {
if (((text[i + 1] == " ") || (text[i + 1] == ")"))) {
printError("too much whitespace", "WhitespaceError", j, i);
}
}
// found a quote that didn't end after sentence
if ((text[i + 1] == undefined) && inQuote) {
inQuote = false;
printError("unclosed quote", "QuoteError", j, quoteStartIndex);
}
// okay, we found a quote
if (quotes.indexOf(text[i]) > -1) {
// proper contraction detection using xor
var a = ((contractionEndings.indexOf(text[i + 1]) == -1));
var b = text[i] == "'";
var xor = (a ? !b : b);
if ((!inQuote) && !xor) {
quoteStartIndex = i;
inQuote = true;
} else {
if ((i - quoteStartIndex) > MAX_QUOTE_LENGTH) {
printError("quote too long", "QuoteError", j, quoteStartIndex);
inQuote = false;
}
}
}
}
}
}
};
module.exports = pedant;