Code coverage report for check-more-types/check-more-types.js

Statements: 97.12% (101 / 104)      Branches: 92.19% (59 / 64)      Functions: 96.55% (28 / 29)      Lines: 97.12% (101 / 104)      Ignored: none     

All files » check-more-types/ » check-more-types.js
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 3171                       1                 1                 1 23               1 54             1 15                 1 44               1 10                 1 50 50 15   35                 1 17 27                                   1 19 17 16 21   15             1 6             1 25 25 25   23 18   5 5         2             1 5   5 3   2 1   1             1 6   6 4   2 1   1             1 14       1         1 14               1 1 5 5 7 7 12 12 10 3       4         1     1 21 21 1   21   1 84 84 84   84 69                     1 21 15 7   8                   1 21 13                   1 21 22 22 10 10         21 21 21 21         1                                   1 15        
(function checkMoreTypes(check) {
  'use strict';
 
  /**
    Custom assertions and predicates for https://github.com/philbooth/check-types.js
    Created by Kensho https://github.com/kensho
    Copyright @ 2014 Kensho https://www.kensho.com/
    License: MIT
 
    @module check
  */
 
  Iif (!check) {
    throw new Error('Cannot find check-types library, has it been loaded?');
  }
 
  /**
  Checks if argument is defined or not
 
  @method defined
  */
  function defined(value) {
    return typeof value !== 'undefined';
  }
 
  /**
  Checks if given value is 0 or 1
 
  @method bit
  */
  function bit(value) {
    return value === 0 || value === 1;
  }
 
  /**
  Checks if given value is true of false
 
  @method bool
  */
  function bool(value) {
    return typeof value === 'boolean';
  }
 
  /**
  Checks if given object has a property
  @method has
  */
  function has(o, property) {
    return Boolean(o && property &&
      typeof property === 'string' &&
      typeof o[property] !== 'undefined');
  }
 
  /**
  Checks if given string is already in lower case
  @method lowerCase
  */
  function lowerCase(str) {
    return check.string(str) &&
      str.toLowerCase() === str;
  }
 
  /**
  Returns true if the argument is an array with at least one value
  @method unemptyArray
  */
  function unemptyArray(a) {
    return check.array(a) && a.length > 0;
  }
 
  /**
  Returns true if given array only has strings
  @method arrayOfStrings
  @param a Array to check
  @param checkLowerCase Checks if all strings are lowercase
  */
  function arrayOfStrings(a, checkLowerCase) {
    var v = check.array(a) && a.every(check.string);
    if (v && check.bool(checkLowerCase) && checkLowerCase) {
      return a.every(check.lowerCase);
    }
    return v;
  }
 
  /**
  Returns true if given argument is array of arrays of strings
  @method arrayOfArraysOfStrings
  @param a Array to check
  @param checkLowerCase Checks if all strings are lowercase
  */
  function arrayOfArraysOfStrings(a, checkLowerCase) {
    return check.array(a) && a.every(function (arr) {
      return check.arrayOfStrings(arr, checkLowerCase);
    });
  }
 
  /**
    Checks if object passes all rules in predicates.
 
    check.all({ foo: 'foo' }, { foo: check.string }, 'wrong object');
 
    This is a composition of check.every(check.map ...) calls
    https://github.com/philbooth/check-types.js#batch-operations
 
    @method all
    @param {object} object object to check
    @param {object} predicates rules to check. Usually one per property.
    @public
    @returns true or false
  */
  function all(obj, predicates) {
    check.verify.object(obj, 'missing object to check');
    check.verify.object(predicates, 'missing predicates object');
    Object.keys(predicates).forEach(function (property) {
      check.verify.fn(predicates[property], 'not a predicate function for ' + property);
    });
    return check.every(check.map(obj, predicates));
  }
 
  /**
    Checks given object against predicates object
    @method schema
  */
  function schema(predicates, obj) {
    return all(obj, predicates);
  }
 
  /** Checks if given function raises an error
 
    @method raises
  */
  function raises(fn, errorValidator) {
    check.verify.fn(fn, 'expected function that raises');
    try {
      fn();
    } catch (err) {
      if (typeof errorValidator === 'undefined') {
        return true;
      }
      Eif (typeof errorValidator === 'function') {
        return errorValidator(err);
      }
      return false;
    }
    // error has not been raised
    return false;
  }
 
  /**
    Returns true if given value is [], {} or ''
    @method empty
  */
  function empty(a) {
    var hasLength = typeof a === 'string' ||
      Array.isArray(a);
    if (hasLength) {
      return !a.length;
    }
    if (a instanceof Object) {
      return !Object.keys(a).length;
    }
    return false;
  }
 
  /**
    Returns true if given value has .length and it is not zero, or has properties
    @method unempty
  */
  function unempty(a) {
    var hasLength = typeof a === 'string' ||
      Array.isArray(a);
    if (hasLength) {
      return a.length;
    }
    if (a instanceof Object) {
      return Object.keys(a).length;
    }
    return true;
  }
 
  /**
    Returns true if 0 <= value <= 1
    @method unit
  */
  function unit(value) {
    return check.number(value) &&
      value >= 0.0 && value <= 1.0;
  }
 
  var rgb = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
  /**
    Returns true if value is hex RGB between '#000000' and '#FFFFFF'
    @method hexRgb
  */
  function hexRgb(value) {
    return check.string(value) &&
      rgb.test(value);
  }
 
  //
  // helper methods
  //
 
  Eif (!check.defend) {
    check.defend = function defend(fn) {
      var predicates = Array.prototype.slice.call(arguments, 1);
      return function () {
        var n = arguments.length;
        while (n--) {
          var predicate = predicates[n];
          if (check.fn(predicate)) {
            if (!predicate.call(null, arguments[n])) {
              throw new Error('Argument ' + (n + 1) + ' ' + arguments[n] + ' does not pass predicate');
            }
          }
        }
        return fn.apply(null, arguments);
      };
    };
  }
 
  Eif (!check.mixin) {
    /** Adds new predicate to all objects
    @method mixin */
    check.mixin = function mixin(fn, name) {
      check.verify.fn(fn, 'expected predicate function');
      if (!check.unemptyString(name)) {
        name = fn.name;
      }
      check.verify.unemptyString(name, 'predicate function missing name\n' + fn.toString());
 
      function registerPredicate(obj, name, fn) {
        check.verify.object(obj, 'missing object');
        check.verify.unemptyString(name, 'missing name');
        check.verify.fn(fn, 'missing function');
 
        if (!obj[name]) {
          obj[name] = fn;
        }
      }
 
      /**
       * Public modifier `maybe`.
       *
       * Returns `true` if `predicate` is  `null` or `undefined`,
       * otherwise propagates the return value from `predicate`.
       * copied from check-types.js
       */
      function maybeModifier(predicate) {
        return function () {
          if (!check.defined(arguments[0]) || check.nulled(arguments[0])) {
            return true;
          }
          return predicate.apply(null, arguments);
        };
      }
 
      /**
      * Public modifier `not`.
      *
      * Negates `predicate`.
      * copied from check-types.js
      */
      function notModifier(predicate) {
        return function () {
          return !predicate.apply(null, arguments);
        };
      }
 
      /**
       * Public modifier `verify`.
       *
       * Throws if `predicate` returns `false`.
       * copied from check-types.js
       */
      function verifyModifier(predicate, defaultMessage) {
        return function () {
        var message;
        if (predicate.apply(null, arguments) === false) {
          message = arguments[arguments.length - 1];
            throw new Error(check.unemptyString(message) ? message : defaultMessage);
          }
        };
      }
 
      registerPredicate(check, name, fn);
      registerPredicate(check.maybe, name, maybeModifier(fn));
      registerPredicate(check.not, name, notModifier(fn));
      registerPredicate(check.verify, name, verifyModifier(fn, name + ' failed'));
    };
  }
 
  // new predicates to be added to check object. Use object to preserve names
  var predicates = {
    defined: defined,
    bit: bit,
    bool: bool,
    has: has,
    lowerCase: lowerCase,
    unemptyArray: unemptyArray,
    arrayOfStrings: arrayOfStrings,
    arrayOfArraysOfStrings: arrayOfArraysOfStrings,
    all: all,
    schema: schema,
    raises: raises,
    empty: empty,
    unempty: unempty,
    unit: unit,
    hexRgb: hexRgb
  };
 
  Object.keys(predicates).forEach(function (name) {
    check.mixin(predicates[name], name);
  });
 
}(typeof window === 'object' ? window.check : global.check));