didYouMean.mjs 895 B

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