utils.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const _ = require('lodash');
  2. exports.toString = require('../utils/to-string');
  3. exports.formatParamOutput = require('../utils/format-param-output');
  4. exports.mapAndExtend = function mapAndExtend(src, dest, mapFunction) {
  5. Object.keys(src).forEach(function (name) {
  6. dest[name] = mapFunction(name, src);
  7. });
  8. };
  9. exports.makeSanitizer = function makeSanitizer(methodName, container) {
  10. return function() {
  11. var _arguments = arguments;
  12. var result;
  13. this.values.forEach(function(value, i) {
  14. if (value != null) {
  15. var args = [value];
  16. args = args.concat(Array.prototype.slice.call(_arguments));
  17. result = container[methodName].apply(container, args);
  18. _.set(this.req[this.locations[i]], this.param, result);
  19. this.values[i] = result;
  20. }
  21. }.bind(this));
  22. return result;
  23. };
  24. };
  25. exports.replaceArgs = function replaceArgs(msg, args) {
  26. if (typeof msg !== 'string') {
  27. return msg;
  28. }
  29. return args.reduce((msg, arg, index) => msg.replace('%' + index, arg), msg);
  30. };