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+
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
errors = proxy.errors,
|
|
|
|
i18n = proxy.i18n,
|
|
|
|
SafeString = proxy.SafeString;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function plural(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-06 15:27:35 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 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) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(options.hash.empty.replace('%', number));
|
2016-02-21 21:48:44 +03:00
|
|
|
} else if (number === 1) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(options.hash.singular.replace('%', number));
|
2016-02-21 21:48:44 +03:00
|
|
|
} else if (number >= 2) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(options.hash.plural.replace('%', number));
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|