Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 3, 2018
1 parent f961c6e commit ee1cceb
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
vfile-statistics.js
vfile-statistics.min.js
34 changes: 17 additions & 17 deletions index.js
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)]++
}
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,38 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"vfile": "^2.0.0",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s vfileStatistics > vfile-statistics.js",
"build-mangle": "esmangle vfile-statistics.js > vfile-statistics.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ npm install vfile-statistics
## Usage

```js
var vfile = require('vfile');
var statistics = require('vfile-statistics');
var vfile = require('vfile')
var statistics = require('vfile-statistics')

var file = vfile({path: '~/example.md'});
var file = vfile({path: '~/example.md'})

file.message('This could be better');
file.message('That could be better');
file.message('This could be better')
file.message('That could be better')

try {
file.fail('This is terribly wrong');
file.fail('This is terribly wrong')
} catch (err) {}

file.info('This is perfect');
file.info('This is perfect')

console.log(statistics(file));
console.log(statistics(file))
```

Yields:
Expand Down
94 changes: 71 additions & 23 deletions test.js
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()
})

0 comments on commit ee1cceb

Please sign in to comment.