invalid_key_input.js 974 B

123456789101112131415161718192021222324252627
  1. function message(msg, actual, ...types) {
  2. types = types.filter(Boolean);
  3. if (types.length > 2) {
  4. const last = types.pop();
  5. msg += `one of type ${types.join(', ')}, or ${last}.`;
  6. }
  7. else if (types.length === 2) {
  8. msg += `one of type ${types[0]} or ${types[1]}.`;
  9. }
  10. else {
  11. msg += `of type ${types[0]}.`;
  12. }
  13. if (actual == null) {
  14. msg += ` Received ${actual}`;
  15. }
  16. else if (typeof actual === 'function' && actual.name) {
  17. msg += ` Received function ${actual.name}`;
  18. }
  19. else if (typeof actual === 'object' && actual != null) {
  20. if (actual.constructor?.name) {
  21. msg += ` Received an instance of ${actual.constructor.name}`;
  22. }
  23. }
  24. return msg;
  25. }
  26. export const invalidKeyInput = (actual, ...types) => message('Key must be ', actual, ...types);
  27. export const withAlg = (alg, actual, ...types) => message(`Key for the ${alg} algorithm must be `, actual, ...types);