didYouMean.mjs 798 B

1234567891011121314151617181920212223242526272829303132
  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. suggestions = _ref[1];
  10. var message = ' Did you mean ';
  11. if (subMessage) {
  12. message += subMessage + ' ';
  13. }
  14. switch (suggestions.length) {
  15. case 0:
  16. return '';
  17. case 1:
  18. return message + suggestions[0] + '?';
  19. case 2:
  20. return message + suggestions[0] + ' or ' + suggestions[1] + '?';
  21. }
  22. var selected = suggestions.slice(0, MAX_SUGGESTIONS);
  23. var lastItem = selected.pop();
  24. return message + selected.join(', ') + ', or ' + lastItem + '?';
  25. }