Created
June 30, 2017 12:56
-
-
Save hcodes/83e5da73caf118b8363689048ab106e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
const errorCodes = [ | |
{ | |
title: 'Typos', | |
code: 1 | |
}, | |
{ | |
title: 'Repeat words', | |
code: 2 | |
}, | |
{ | |
title: 'Capitalization', | |
code: 3 | |
}, | |
{ | |
code: 100, | |
title: 'Letter Ё (Yo)' | |
} | |
]; | |
function getTyposByCode(code, data) { | |
let typos = []; | |
let num = 1; | |
data.forEach(function(el) { | |
if (el.code !== code) { | |
return; | |
} | |
const comment = []; | |
const pos = el.position; | |
if (el.count > 1) { | |
comment.push('count: ' + el.count); | |
} | |
if (el.suggest) { | |
comment.push('suggest: ' + el.suggest.join(', ')); | |
} | |
if (pos.length) { | |
comment.push(pos[0].line + ':' + pos[0].column); | |
} | |
typos.push(num + '. ' + el.word + (comment.length ? ' (' + comment.join(', ') + ')' : '')); | |
num++; | |
}); | |
return typos; | |
} | |
module.exports = { | |
oneach(err, data) { | |
const errors = []; | |
if (err) { | |
console.error(data); | |
} else { | |
if (data.data.some(el => el.code === 4)) { | |
errors.push('Too many errors'); | |
} | |
errorCodes.forEach(function(el) { | |
const typos = getTyposByCode(el.code, data.data); | |
if (typos.length) { | |
errors.push(el.title + ': ' + | |
typos.length + '\n' + | |
typos.join('\n') + '\n'); | |
} | |
}); | |
const time = data.time ? ' ' + data.time + ' ms' : ''; | |
const separator = '-----\n'; | |
let res = data.resource; | |
if (errors.length) { | |
console.error('[ERR] ' + res + time + '\n' + | |
separator + errors.join('\n') + separator); | |
} else { | |
console.log('[OK] ' + res + time); | |
} | |
} | |
}, | |
onend(data, stats) { | |
if (stats.total) { | |
if (!stats.errors) { | |
console.log('No errors.'); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment