Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arguments and context for formatters #159

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ See hmpo-template-mixins or hmpo-components for additional field options.
* `journeyKey` - Name of the cross-wizard field storage name
* `default` - Default value for this field
* `multiple` - Allow multiple incomming values for a field. The result is presented as an array
* `formater` - Array of formatter names for this field in addition to the default formatter set
* `formater` - Array of formatter names for this field in addition to the default formatter set, or formatter objects
* `type` - Formatter name
* `fn` - Formatter function
* `arguments` - Array of formatter arguments, eg. `{ type: 'truncate', arguments: [24] }`
* `ignore-defaults` - Disabled the default set of formatters for this field
* `validate` - An array of validator names, or validator objects
* `type` - Validator name
* `fn` - Validator function
* `arguments` - Array of validator arguments, eg. `{ type: 'minlength', arguments: [24] }`
* `items` - Array of select box or radio button options
* `value` - Item value
Expand Down
10 changes: 8 additions & 2 deletions lib/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,21 @@ class BaseController {
let values = req.form.values = req.form.values || {};
let defaultFormatters = req.form.options.defaultFormatters;

let context = {
sessionModel: req.sessionModel,
fields,
values: req.body
};

_.each(fields, (field, key) => {
let value = req.body[key];
req.form.values[key] = formatting.format(fields, key, value, defaultFormatters);
req.form.values[key] = formatting.format(fields, key, value, defaultFormatters, context);
});

// set values to formatted '' if the field is not allowed
_.each(fields, (field, key) => {
if (!validation.isAllowedDependent(fields, key, values)) {
req.form.values[key] = formatting.format(fields, key, null, defaultFormatters);
req.form.values[key] = formatting.format(fields, key, null, defaultFormatters, context);
}
});
this.process(req, res, next);
Expand Down
30 changes: 21 additions & 9 deletions lib/formatting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ const _ = require('underscore');
const debug = require('debug')('hmpo:formatting');
const formatters = require('./formatters');

function applyFormatters(fieldFormatters, key, value) {
function applyFormatters(fieldFormatters, key, value, context) {
if (!Array.isArray(fieldFormatters)) {
fieldFormatters = [ fieldFormatters ];
}
_.each(fieldFormatters, formatter => {
if (typeof formatter === 'string') {
formatter = formatters[formatter];
if (typeof formatter === 'string') formatter = { type: formatter };
if (typeof formatter === 'function') formatter = { fn: formatter };

if (typeof formatter.fn !== 'function') {
formatter.fn = formatters[formatter.type];
}
if (typeof formatter === 'function') {
debug('Formatting field %s value %s with formatter %s', key, value, formatter.name);
value = formatter(value);

if (typeof formatter.fn === 'function') {
formatter.type = formatter.type || formatter.fn.name;

if (formatter.arguments === undefined) {
formatter.arguments = [];
} else if (!_.isArray(formatter.arguments)) {
formatter.arguments = [ formatter.arguments ];
}

debug('Applying %s formatter to %s with value "%s"', formatter.type, key, value, formatter.arguments);
value = formatter.fn.apply(context, [value].concat(formatter.arguments));
}
});
return value;
}

function format(fields, key, value, defaultFormatters) {
function format(fields, key, value, defaultFormatters, context) {
let field = fields[key];
if (!Array.isArray(value)) {
value = [ value ];
Expand All @@ -34,13 +46,13 @@ function format(fields, key, value, defaultFormatters) {
// apply default formatters first
if (defaultFormatters && !(field && field['ignore-defaults'])) {
debug('Formatting field %s with default formatters:', key, defaultFormatters);
value = value.map(item => applyFormatters(defaultFormatters, key, item));
value = value.map(item => applyFormatters(defaultFormatters, key, item, context));
}

// apply configured formatters
if (field && field.formatter) {
debug('Formatting field %s with formatters:', key, field.formatter);
value = value.map(item => applyFormatters(field.formatter, key, item));
value = value.map(item => applyFormatters(field.formatter, key, item, context));
}

if (!field.multiple) return value[0];
Expand Down
Loading