Ghost/core/server/helpers/plural.js
Katharina Irrgang d81bc91bd2 Error creation (#7477)
refs #7116, refs #2001

- Changes the way Ghost errors are implemented to benefit from proper inheritance
- Moves all error definitions into a single file
- Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order.
- Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed.

Summary of changes:

* 🐛  set NODE_ENV in config handler
*   add GhostError implementation (core/server/errors.js)
  - register all errors in one file
  - inheritance from GhostError
  - option pattern
* 🔥  remove all error files
*   wrap all errors into GhostError in case of HTTP
* 🎨  adaptions
  - option pattern for errors
  - use GhostError when needed
* 🎨  revert debug deletion and add TODO for error id's
2016-10-06 13:27:35 +01:00

35 lines
1.4 KiB
JavaScript

// # 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'),
i18n = require('../i18n'),
_ = require('lodash'),
plural;
plural = function (number, options) {
if (_.isUndefined(options.hash) || _.isUndefined(options.hash.empty) ||
_.isUndefined(options.hash.singular) || _.isUndefined(options.hash.plural)) {
throw new errors.IncorrectUsageError({
message: i18n.t('warnings.helpers.plural.valuesMustBeDefined')
});
}
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));
}
};
module.exports = plural;