-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
5 changed files
with
112 additions
and
53 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
vfile-statistics.js | ||
vfile-statistics.min.js |
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,50 +1,50 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = statistics; | ||
module.exports = statistics | ||
|
||
/* Get stats for a file, list of files, or list of messages. */ | ||
function statistics(files) { | ||
var result = {true: 0, false: 0, null: 0}; | ||
var result = {true: 0, false: 0, null: 0} | ||
|
||
count(files); | ||
count(files) | ||
|
||
return { | ||
fatal: result.true, | ||
nonfatal: result.false + result.null, | ||
warn: result.false, | ||
info: result.null, | ||
total: result.true + result.false + result.null | ||
}; | ||
} | ||
|
||
function count(value) { | ||
if (value) { | ||
/* Multiple vfiles */ | ||
if (value[0] && value[0].messages) { | ||
countInAll(value); | ||
/* One vfile / messages */ | ||
/* Multiple vfiles */ | ||
countInAll(value) | ||
} else { | ||
countAll(value.messages || value); | ||
/* One vfile / messages */ | ||
countAll(value.messages || value) | ||
} | ||
} | ||
} | ||
|
||
function countInAll(files) { | ||
var length = files.length; | ||
var index = -1; | ||
var length = files.length | ||
var index = -1 | ||
|
||
while (++index < length) { | ||
count(files[index].messages); | ||
count(files[index].messages) | ||
} | ||
} | ||
|
||
function countAll(messages) { | ||
var length = messages.length; | ||
var index = -1; | ||
var fatal; | ||
var length = messages.length | ||
var index = -1 | ||
var fatal | ||
|
||
while (++index < length) { | ||
fatal = messages[index].fatal; | ||
result[fatal === null || fatal === undefined ? null : Boolean(fatal)]++; | ||
fatal = messages[index].fatal | ||
result[fatal === null || fatal === undefined ? null : Boolean(fatal)]++ | ||
} | ||
} | ||
} |
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
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
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,34 +1,82 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var test = require('tape'); | ||
var vfile = require('vfile'); | ||
var statistics = require('.'); | ||
var test = require('tape') | ||
var vfile = require('vfile') | ||
var statistics = require('.') | ||
|
||
test('statistics()', function (t) { | ||
var file = vfile(); | ||
var other = vfile(); | ||
test('statistics()', function(t) { | ||
var file = vfile() | ||
var other = vfile() | ||
|
||
t.deepEqual(statistics(), {fatal: 0, nonfatal: 0, warn: 0, info: 0, total: 0}); | ||
t.deepEqual(statistics(file), {fatal: 0, nonfatal: 0, warn: 0, info: 0, total: 0}); | ||
t.deepEqual(statistics([file, other]), {fatal: 0, nonfatal: 0, warn: 0, info: 0, total: 0}); | ||
t.deepEqual(statistics(), {fatal: 0, nonfatal: 0, warn: 0, info: 0, total: 0}) | ||
t.deepEqual(statistics(file), { | ||
fatal: 0, | ||
nonfatal: 0, | ||
warn: 0, | ||
info: 0, | ||
total: 0 | ||
}) | ||
t.deepEqual(statistics([file, other]), { | ||
fatal: 0, | ||
nonfatal: 0, | ||
warn: 0, | ||
info: 0, | ||
total: 0 | ||
}) | ||
|
||
file.message('This'); | ||
t.deepEqual(statistics(file.messages), {fatal: 0, nonfatal: 1, warn: 1, info: 0, total: 1}); | ||
t.deepEqual(statistics(file), {fatal: 0, nonfatal: 1, warn: 1, info: 0, total: 1}); | ||
t.deepEqual(statistics([file, other]), {fatal: 0, nonfatal: 1, warn: 1, info: 0, total: 1}); | ||
file.message('This') | ||
t.deepEqual(statistics(file.messages), { | ||
fatal: 0, | ||
nonfatal: 1, | ||
warn: 1, | ||
info: 0, | ||
total: 1 | ||
}) | ||
t.deepEqual(statistics(file), { | ||
fatal: 0, | ||
nonfatal: 1, | ||
warn: 1, | ||
info: 0, | ||
total: 1 | ||
}) | ||
t.deepEqual(statistics([file, other]), { | ||
fatal: 0, | ||
nonfatal: 1, | ||
warn: 1, | ||
info: 0, | ||
total: 1 | ||
}) | ||
|
||
file.message('That'); | ||
t.deepEqual(statistics(file), {fatal: 0, nonfatal: 2, warn: 2, info: 0, total: 2}); | ||
file.message('That') | ||
t.deepEqual(statistics(file), { | ||
fatal: 0, | ||
nonfatal: 2, | ||
warn: 2, | ||
info: 0, | ||
total: 2 | ||
}) | ||
|
||
var message = file.message('Info'); | ||
message.fatal = null; | ||
t.deepEqual(statistics(file), {fatal: 0, nonfatal: 3, warn: 2, info: 1, total: 3}); | ||
var message = file.message('Info') | ||
message.fatal = null | ||
t.deepEqual(statistics(file), { | ||
fatal: 0, | ||
nonfatal: 3, | ||
warn: 2, | ||
info: 1, | ||
total: 3 | ||
}) | ||
|
||
try { | ||
file.fail('Again'); | ||
file.fail('Again') | ||
} catch (err) {} | ||
|
||
t.deepEqual(statistics(file), {fatal: 1, nonfatal: 3, warn: 2, info: 1, total: 4}); | ||
t.deepEqual(statistics(file), { | ||
fatal: 1, | ||
nonfatal: 3, | ||
warn: 2, | ||
info: 1, | ||
total: 4 | ||
}) | ||
|
||
t.end(); | ||
}); | ||
t.end() | ||
}) |