Skip to content

Commit

Permalink
fix(wording): testing plural messages, close #105
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jul 6, 2017
1 parent 282c95e commit 02b9966
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions __snapshots__/stats-spec.js.snap-shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports['formats stats message 1'] = "stats: foo 1.0.0 -> 2.0.0 success probability 50% 5 successes 5 failures"

exports['formats stats message with singular failure 1'] = "stats: foo 1.0.0 -> 2.0.0 success probability 50% 1 success 1 failure"

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"chdir-promise": "0.4.0",
"check-more-types": "2.24.0",
"cli-color": "1.2.0",
"common-tags": "^1.4.0",
"console.json": "0.2.1",
"console.table": "0.8.0",
"debug": "2.6.8",
Expand Down
30 changes: 30 additions & 0 deletions src/stats-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @ts-check
const snapShot = require('snap-shot')

/* global describe, it */
describe('stats', () => {
describe('formatStats', () => {
const {formatStats} = require('./stats')
it('formats stats message', () => {
const message = formatStats({}, {
name: 'foo',
from: '1.0.0',
to: '2.0.0',
success: 5,
failure: 5
})
snapShot(message)
})

it('formats stats message with singular failure', () => {
const message = formatStats({}, {
name: 'foo',
from: '1.0.0',
to: '2.0.0',
success: 1,
failure: 1
})
snapShot(message)
})
})
})
19 changes: 15 additions & 4 deletions src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var request = require('request')
var Q = require('q')
var colors = require('cli-color')
var colorAvailable = process.stdout.isTTY
const pluralize = require('pluralize')
const {oneLine} = require('common-tags')

var nextUpdateStatsUrl = require('../package.json')['next-update-stats'] ||
'http://next-update.herokuapp.com'
Expand Down Expand Up @@ -80,7 +82,7 @@ function colorProbability (probability, options) {
return probabilityStr
}

function printStats (options, stats) {
function formatStats (options, stats) {
verify.object(stats, 'missing stats object')
verify.unemptyString(stats.name, 'missing name')
verify.unemptyString(stats.from, 'missing from version')
Expand All @@ -90,14 +92,23 @@ function printStats (options, stats) {
var total = stats.success + stats.failure
var probability = (total > 0 ? stats.success / total : 0)
var probabilityStr = colorProbability(probability, options)
console.log('stats:', stats.name, stats.from, '->', stats.to,
'success probability', probabilityStr,
stats.success, 'success(es)', stats.failure, 'failure(s)')
return oneLine`
stats: ${stats.name} ${stats.from} -> ${stats.to}
success probability ${probabilityStr}
${stats.success} ${pluralize('success', stats.success)}
${stats.failure} ${pluralize('failure', stats.failure)}
`
}

function printStats (options, stats) {
const message = formatStats(options, stats)
console.log(message)
}

module.exports = {
sendUpdateResult: sendUpdateResult,
getSuccessStats: getSuccessStats,
formatStats: formatStats,
printStats: printStats,
colorProbability: colorProbability,
url: nextUpdateStatsUrl
Expand Down

0 comments on commit 02b9966

Please sign in to comment.