bigbuffet-rw/app/soapbox/utils/errors.js
2022-03-21 13:14:26 -05:00

21 lines
510 B
JavaScript

import camelCase from 'lodash/camelCase';
import startCase from 'lodash/startCase';
const toSentence = (arr) => arr
.reduce(
(prev, curr, i) => prev + curr + (i === arr.length - 2 ? ' and ' : ', '),
'',
)
.slice(0, -2);
const buildErrorMessage = (errors) => {
const individualErrors = Object.keys(errors).map(
(attribute) => `${startCase(camelCase(attribute))} ${toSentence(
errors[attribute],
)}`,
);
return toSentence(individualErrors);
};
export { buildErrorMessage };