-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,73 @@ | ||
var pedant = {validate : function(text) { | ||
var MAX_QUOTE_LENGTH = 20; | ||
var ERROR_SEPERATOR = "\n"; | ||
var punctuation = [ ',', '.', '!', ';', ';', '"' ]; | ||
var quotes = [ '"', "'" ]; | ||
for (var i = 0; i < text.length; i++) { | ||
// check for punctuation | ||
if (punctuation.indexOf(text[i]) > -1) { | ||
// no space after punctuation | ||
if ((text[i + 1] != " ") && (i != text.length - 1)) { | ||
console.log("PunctuationError: no space after punctuation at index " + | ||
i + ":"); | ||
var error = showPrettyError(text, i); | ||
console.log(error[0]); | ||
console.log(error[1]); | ||
console.log(ERROR_SEPERATOR); | ||
} | ||
var pedant = { | ||
validate : function(text) { | ||
var MAX_QUOTE_LENGTH = 20; | ||
var punctuation = [ ',', '.', '!', ';', ';', '"' ]; | ||
var quotes = [ '"', "'" ]; | ||
for (var i = 0; i < text.length; i++) { | ||
// check for punctuation | ||
if (punctuation.indexOf(text[i]) > -1) { | ||
// no space after punctuation | ||
if ((text[i + 1] != " ") && (i != text.length - 1)) { | ||
showPrettyError( | ||
text, i, | ||
"PunctuationError: no space after punctuation at index " + i + | ||
":"); | ||
} | ||
|
||
// check for too much punctuation, except for periods | ||
if ((punctuation.indexOf(text[i + 1]) > -1) && (i != text.length - 1) && | ||
(text[i] != ".")) { | ||
console.log("PunctuationError: too much punctuation found at index " + | ||
i + ":"); | ||
var error = showPrettyError(text, i); | ||
console.log(error[0]); | ||
console.log(error[1]); | ||
console.log(ERROR_SEPERATOR); | ||
// check for too much punctuation, except for periods | ||
if ((punctuation.indexOf(text[i + 1]) > -1) && (i != text.length - 1) && | ||
(text[i] != ".")) { | ||
showPrettyError( | ||
text, i, | ||
"PunctuationError: too much punctuation at index " + i + ":"); | ||
} | ||
} | ||
} | ||
|
||
// check for missing quotes | ||
if (quotes.indexOf(text[i]) > -1) { | ||
var k = 0; | ||
for (j = 1; j < text.length; j++) { | ||
if (quotes.indexOf(text[j]) == -1) { | ||
k++; | ||
// check for missing quotes | ||
if (quotes.indexOf(text[i]) > -1) { | ||
var k = 0; | ||
for (j = 1; j < text.length; j++) { | ||
if (quotes.indexOf(text[j]) == -1) { | ||
k++; | ||
} | ||
} | ||
} | ||
|
||
if (k > MAX_QUOTE_LENGTH) { | ||
var error = showPrettyError(text, i); | ||
console.log(error[0]); | ||
console.log(error[1]); | ||
console.log("QuoteError: max quote length of " + MAX_QUOTE_LENGTH + | ||
" exceeded at index " + j + " (quote started at index " + | ||
i + "):"); | ||
console.log(ERROR_SEPERATOR); | ||
if (k > MAX_QUOTE_LENGTH) { | ||
var error = showPrettyError(text, i); | ||
console.log(error[0]); | ||
console.log(error[1]); | ||
console.log("QuoteError: max quote length of " + MAX_QUOTE_LENGTH + | ||
" exceeded at index " + j + " (quote started at index " + | ||
i + "):"); | ||
console.log(ERROR_SEPERATOR); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function showPrettyError(text, index) { | ||
var contextAmount = 8; | ||
var showContextSegment = "..."; | ||
return [ | ||
showContextSegment + | ||
text.substr(index - contextAmount, index + contextAmount) + | ||
showContextSegment, | ||
pad(Math.max(index + 1 - contextAmount, index + 1), "^", " ") | ||
.substr(index - contextAmount - showContextSegment.length) | ||
]; | ||
function showPrettyError(text, index, errorMessage) { | ||
var ERROR_SEPERATOR = "\n"; | ||
|
||
var contextAmount = 8; | ||
var showContextSegment = "..."; | ||
var errorContext = [ | ||
showContextSegment + | ||
text.substr(index - contextAmount, index + contextAmount) + | ||
showContextSegment, | ||
pad(Math.max(index + 1 - contextAmount, index + 1), "^", " ") | ||
.substr(index - contextAmount - showContextSegment.length) | ||
]; | ||
console.log(errorContext[0]); | ||
console.log(errorContext[1]); | ||
|
||
// http://stackoverflow.com/a/15660515 | ||
function pad(width, string, padding) { | ||
return (width <= string.length) ? string | ||
: pad(width, padding + string, padding); | ||
console.log(errorMessage); | ||
console.log(ERROR_SEPERATOR); | ||
return errorContext; | ||
// http://stackoverflow.com/a/15660515 | ||
function pad(width, string, padding) { | ||
return (width <= string.length) ? string | ||
: pad(width, padding + string, padding); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
module.exports = pedant; | ||
module.exports = pedant; |