All files assertions.js

84.16% Statements 85/101
78.5% Branches 84/107
77.78% Functions 14/18
85.54% Lines 71/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                1x   1x 1x 1x 1x 1x     5443x 5443x 5359x     1x           1x           1x   1x   1x 377x 377x 376x     1x 334x 333x 329x     1x           1x           1x 12x 12x 11x     1x 223x 220x 219x     1x 1450x 1438x 1433x     1x 571x 560x 551x     1x 286x 280x 275x     1x 573x 567x 565x     1x 33x 29x 27x     1x 923x 903x 872x     1x 228x 225x 224x 222x 219x 217x     1x 578x 566x 554x     1x 537x 533x 533x 532x      
"use strict";
 
/*
 * datona-lib assertions.  Used to encourage strong typing of parameters.
 *
 * Copyright (C) 2020 Datona Labs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
 
 
const errors = require('./errors');
 
const VALID_BLOCKCHAIN_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
const VALID_PRIVATE_KEY_REGEX = /^[0-9a-fA-F]{64}$/;
const VALID_HASH_REGEX = /^[0-9a-fA-F]{64}$/;
const VALID_URL_SCHEME = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
const VALID_URL_HOSTNAME = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
 
function isNotEmpty(value, name) {
  const result = (value !== undefined) && !/^\s*$/.test(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + " is missing or empty");
  else return result;
}
 
exports.equals = function(value, expected, name) {
  const result = (value === expected);
  if (name !== undefined && !result) throw new errors.DatonaError(name + ": expected '" + value + "' to equal '" + expected + "'");
  else return result;
};
 
exports.notEquals = function(value, expected, name) {
  const result = (value !== expected);
  if (name !== undefined && !result) throw new errors.DatonaError(name + ": expected '" + value + "' to not equal '" + expected + "'");
  else return result;
};
 
exports.isNotEmpty = isNotEmpty;
 
exports.isPresent = isNotEmpty;
 
exports.isNotNull = function(value, name) {
  const result = (value !== undefined);
  if (name !== undefined && !result) throw new errors.TypeError(name + " is null");
  else return result;
};
 
exports.isArray = function(value, name) {
  const result = exports.isNotNull(value, name) && Array.isArray(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected Array");
  else return result;
};
 
exports.isBoolean = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object Boolean]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected Boolean");
  else return result;
};
 
exports.isBuffer = function(value, name) {
  const result = isNotEmpty(value, name) && Buffer.isBuffer(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected Buffer");
  else return result;
};
 
exports.isFunction = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object Function]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected function");
  else return result;
};
 
exports.isNumber = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object Number]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected number");
  else return result;
};
 
exports.isString = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object String]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected string");
  else return result;
};
 
exports.isObject = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object Object]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected Object");
  else return result;
};
 
exports.isHexString = function(value, name) {
  const result = isNotEmpty(value, name) && (toString.call(value) === '[object String]') && value.match('^[0-9a-fA-F]*$');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected hex string");
  else return result;
};
 
exports.isHash = function(value, name) {
  const result = isNotEmpty(value, name) && VALID_HASH_REGEX.test(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected hex string of length 64");
  else return result;
};
 
exports.isPrivateKey = function(value, name) {
  const result = isNotEmpty(value, name) && VALID_PRIVATE_KEY_REGEX.test(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected hex string of length 64");
  else return result;
};
 
exports.isAddress = function(value, name) {
  const result = isNotEmpty(value, name) && VALID_BLOCKCHAIN_ADDRESS_REGEX.test(value);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected address");
  else return result;
};
 
exports.isUrl = function(value, name) {
  var result = isNotEmpty(value, name) && (toString.call(value) === '[object Object]');
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected Object");
  result &= exports.matches(value.scheme, VALID_URL_SCHEME, name ? name + " scheme" : undefined);
  result &= exports.matches(value.host, VALID_URL_HOSTNAME, name ? name + " host" : undefined);
  result &= exports.isNumber(value.port, name ? name + " port" : undefined);
  return result;
};
 
exports.isInstanceOf = function(value, name, type) {
  const result = isNotEmpty(value, name) && (value instanceof type);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected " + type.name);
  else return result;
};
 
exports.matches = function(value, regex, name) {
  exports.isString(value, name);
  const result = value.match(regex);
  if (name !== undefined && !result) throw new errors.TypeError(name + ": invalid type. Expected to match '"+regex+"'");
  else return result;
};