You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

4 years ago
  1. 'use strict';
  2. var assert = require('assert');
  3. /**
  4. * Reducer function that converts a codec list to a hash.
  5. * @throws Error on bad codec
  6. * @param {{name:string, decode:function, encode:function, root:function}} candidate A possible codec
  7. * @returns True where an error is not thrown
  8. */
  9. function testCodec(candidate) {
  10. assert(
  11. !!candidate && (typeof candidate === 'object'),
  12. 'Codec must be an object'
  13. );
  14. assert(
  15. (typeof candidate.name === 'string') && /^[\w-]+$/.test(candidate.name),
  16. 'Codec.name must be a kebab-case string'
  17. );
  18. assert(
  19. (typeof candidate.decode === 'function') && (candidate.decode.length === 1),
  20. 'Codec.decode must be a function that accepts a single source string'
  21. );
  22. assert(
  23. (typeof candidate.encode === 'undefined') ||
  24. ((typeof candidate.encode === 'function') && (candidate.encode.length === 1)),
  25. 'Codec.encode must be a function that accepts a single absolute path string, or else be omitted'
  26. );
  27. assert(
  28. (typeof candidate.root === 'undefined') ||
  29. (typeof candidate.root === 'function') && (candidate.root.length === 0),
  30. 'Codec.root must be a function that accepts no arguments, or else be omitted'
  31. );
  32. return true;
  33. }
  34. module.exports = testCodec;