2014-10-10 18:54:07 +04:00
|
|
|
// # Plural Helper
|
|
|
|
// Usage: `{{plural 0 empty='No posts' singular='% post' plural='% posts'}}`
|
|
|
|
//
|
|
|
|
// pluralises strings depending on item count
|
|
|
|
//
|
|
|
|
// The 1st argument is the numeric variable which the helper operates on
|
|
|
|
// The 2nd argument is the string that will be output if the variable's value is 0
|
|
|
|
// The 3rd argument is the string that will be output if the variable's value is 1
|
|
|
|
// The 4th argument is the string that will be output if the variable's value is 2+
|
|
|
|
|
|
|
|
var hbs = require('express-hbs'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
_ = require('lodash'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-10-10 18:54:07 +04:00
|
|
|
plural;
|
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
plural = function (number, options) {
|
2014-10-10 18:54:07 +04:00
|
|
|
if (_.isUndefined(options.hash) || _.isUndefined(options.hash.empty) ||
|
|
|
|
_.isUndefined(options.hash.singular) || _.isUndefined(options.hash.plural)) {
|
2016-10-04 18:33:43 +03:00
|
|
|
throw new errors.IncorrectUsage(i18n.t('warnings.helpers.plural.valuesMustBeDefined'));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
if (number === 0) {
|
|
|
|
return new hbs.handlebars.SafeString(options.hash.empty.replace('%', number));
|
|
|
|
} else if (number === 1) {
|
|
|
|
return new hbs.handlebars.SafeString(options.hash.singular.replace('%', number));
|
|
|
|
} else if (number >= 2) {
|
|
|
|
return new hbs.handlebars.SafeString(options.hash.plural.replace('%', number));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = plural;
|