invalid_key_input.js 972 B

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