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