Ghost/core/frontend/helpers/plural.js
Hannah Wolfe 52b924638d
Removed core @tryghost pkg usage from f/e proxy
- The frontend proxy is meant to be a way to pass critical internal pieces of Ghost core into the frontend
- These fundamental @tryghost packages are shared and can be required directly, hence there's no need to pass them via the proxy
- Reducing the surface area of the proxy reduces the proxies API
- This makes it easier to see what's left in terms of decoupling the frontend, and what will always need to be passed (e.g. api)

Note on @tryghost/social-urls:
- this is a small utility that helps create URLs for social profiles, it's a util for working with data on the frontend aka part of the sdk
- I think there should be many of these small helpers and we'll probably want to bundle them for the frontend at some point
- for now, I'm leaving these as part of the proxy, as need to figure out where they belong
2021-09-28 12:19:02 +01:00

38 lines
1.6 KiB
JavaScript

// # Plural Helper
// Usage example: `{{plural ../pagination.total empty='No posts' singular='1 post' plural='% posts'}}`
// or for translatable themes, with (t) translation helper's subexpressions:
// `{{plural ../pagination.total empty=(t "No posts") singular=(t "1 post") plural=(t "% 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+
const {SafeString} = require('../services/proxy');
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const isUndefined = require('lodash/isUndefined');
const messages = {
valuesMustBeDefined: 'All values must be defined for empty, singular and plural'
};
module.exports = function plural(number, options) {
if (isUndefined(options.hash) || isUndefined(options.hash.empty) ||
isUndefined(options.hash.singular) || isUndefined(options.hash.plural)) {
throw new errors.IncorrectUsageError({
message: tpl(messages.valuesMustBeDefined)
});
}
if (number === 0) {
return new SafeString(options.hash.empty.replace('%', number));
} else if (number === 1) {
return new SafeString(options.hash.singular.replace('%', number));
} else if (number >= 2) {
return new SafeString(options.hash.plural.replace('%', number));
}
};