config: consistency changes.

This commit is contained in:
Christopher Jeffrey 2017-08-06 03:31:58 -07:00
parent a290e6880d
commit 422d2d4133
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 9 additions and 6 deletions

View File

@ -463,9 +463,12 @@ Config.prototype.bool = function bool(key, fallback) {
* @returns {Buffer|null}
*/
Config.prototype.buf = function buf(key, fallback) {
Config.prototype.buf = function buf(key, fallback, enc) {
const value = this.get(key);
if (!enc)
enc = 'hex';
if (fallback === undefined)
fallback = null;
@ -478,9 +481,9 @@ Config.prototype.buf = function buf(key, fallback) {
return value;
}
const data = Buffer.from(value, 'hex');
const data = Buffer.from(value, enc);
if (data.length !== value.length / 2)
if (data.length !== Buffer.byteLength(value, enc))
throw new Error(`${fmt(key)} must be a hex string.`);
return data;
@ -559,7 +562,7 @@ Config.prototype.func = function func(key, fallback) {
if (value === null)
return fallback;
if (!value || typeof value !== 'function')
if (typeof value !== 'function')
throw new Error(`${fmt(key)} must be a function.`);
return value;

View File

@ -49,7 +49,7 @@ Validator.prototype.init = function init(data) {
Validator.prototype.has = function has(key) {
assert(typeof key === 'string' || typeof key === 'number',
'Key must be a string.');
'Key must be a string or number.');
for (const map of this.data) {
const value = map[key];
@ -82,7 +82,7 @@ Validator.prototype.get = function get(key, fallback) {
}
assert(typeof key === 'string' || typeof key === 'number',
'Key must be a string.');
'Key must be a string or number.');
for (const map of this.data) {
if (!map || typeof map !== 'object')