didYouMean.js 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = didYouMean;
  6. var MAX_SUGGESTIONS = 5;
  7. /**
  8. * Given [ A, B, C ] return ' Did you mean A, B, or C?'.
  9. */
  10. // eslint-disable-next-line no-redeclare
  11. function didYouMean(firstArg, secondArg) {
  12. var _ref = typeof firstArg === 'string' ? [firstArg, secondArg] : [undefined, firstArg],
  13. subMessage = _ref[0],
  14. suggestionsArg = _ref[1];
  15. var message = ' Did you mean ';
  16. if (subMessage) {
  17. message += subMessage + ' ';
  18. }
  19. var suggestions = suggestionsArg.map(function (x) {
  20. return "\"".concat(x, "\"");
  21. });
  22. switch (suggestions.length) {
  23. case 0:
  24. return '';
  25. case 1:
  26. return message + suggestions[0] + '?';
  27. case 2:
  28. return message + suggestions[0] + ' or ' + suggestions[1] + '?';
  29. }
  30. var selected = suggestions.slice(0, MAX_SUGGESTIONS);
  31. var lastItem = selected.pop();
  32. return message + selected.join(', ') + ', or ' + lastItem + '?';
  33. }