"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/assert-plus/assert.js" (8 Mar 2017, 5330 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright (c) 2012, Mark Cavage. All rights reserved.
    2 
    3 var assert = require('assert');
    4 var Stream = require('stream').Stream;
    5 var util = require('util');
    6 
    7 
    8 
    9 ///--- Globals
   10 
   11 var NDEBUG = process.env.NODE_NDEBUG || false;
   12 var UUID_REGEXP = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/;
   13 
   14 
   15 
   16 ///--- Messages
   17 
   18 var ARRAY_TYPE_REQUIRED = '%s ([%s]) required';
   19 var TYPE_REQUIRED = '%s (%s) is required';
   20 
   21 
   22 
   23 ///--- Internal
   24 
   25 function capitalize(str) {
   26         return (str.charAt(0).toUpperCase() + str.slice(1));
   27 }
   28 
   29 function uncapitalize(str) {
   30         return (str.charAt(0).toLowerCase() + str.slice(1));
   31 }
   32 
   33 function _() {
   34         return (util.format.apply(util, arguments));
   35 }
   36 
   37 
   38 function _assert(arg, type, name, stackFunc) {
   39         if (!NDEBUG) {
   40                 name = name || type;
   41                 stackFunc = stackFunc || _assert.caller;
   42                 var t = typeof (arg);
   43 
   44                 if (t !== type) {
   45                         throw new assert.AssertionError({
   46                                 message: _(TYPE_REQUIRED, name, type),
   47                                 actual: t,
   48                                 expected: type,
   49                                 operator: '===',
   50                                 stackStartFunction: stackFunc
   51                         });
   52                 }
   53         }
   54 }
   55 
   56 
   57 function _instanceof(arg, type, name, stackFunc) {
   58         if (!NDEBUG) {
   59                 name = name || type;
   60                 stackFunc = stackFunc || _instanceof.caller;
   61 
   62                 if (!(arg instanceof type)) {
   63                         throw new assert.AssertionError({
   64                                 message: _(TYPE_REQUIRED, name, type.name),
   65                                 actual: _getClass(arg),
   66                                 expected: type.name,
   67                                 operator: 'instanceof',
   68                                 stackStartFunction: stackFunc
   69                         });
   70                 }
   71         }
   72 }
   73 
   74 function _getClass(object) {
   75         return (Object.prototype.toString.call(object).slice(8, -1));
   76 };
   77 
   78 
   79 
   80 ///--- API
   81 
   82 function array(arr, type, name) {
   83         if (!NDEBUG) {
   84                 name = name || type;
   85 
   86                 if (!Array.isArray(arr)) {
   87                         throw new assert.AssertionError({
   88                                 message: _(ARRAY_TYPE_REQUIRED, name, type),
   89                                 actual: typeof (arr),
   90                                 expected: 'array',
   91                                 operator: 'Array.isArray',
   92                                 stackStartFunction: array.caller
   93                         });
   94                 }
   95 
   96                 for (var i = 0; i < arr.length; i++) {
   97                         _assert(arr[i], type, name, array);
   98                 }
   99         }
  100 }
  101 
  102 
  103 function bool(arg, name) {
  104         _assert(arg, 'boolean', name, bool);
  105 }
  106 
  107 
  108 function buffer(arg, name) {
  109         if (!Buffer.isBuffer(arg)) {
  110                 throw new assert.AssertionError({
  111                         message: _(TYPE_REQUIRED, name || '', 'Buffer'),
  112                         actual: typeof (arg),
  113                         expected: 'buffer',
  114                         operator: 'Buffer.isBuffer',
  115                         stackStartFunction: buffer
  116                 });
  117         }
  118 }
  119 
  120 
  121 function func(arg, name) {
  122         _assert(arg, 'function', name);
  123 }
  124 
  125 
  126 function number(arg, name) {
  127         _assert(arg, 'number', name);
  128         if (!NDEBUG && (isNaN(arg) || !isFinite(arg))) {
  129                 throw new assert.AssertionError({
  130                         message: _(TYPE_REQUIRED, name, 'number'),
  131                         actual: arg,
  132                         expected: 'number',
  133                         operator: 'isNaN',
  134                         stackStartFunction: number
  135                 });
  136         }
  137 }
  138 
  139 
  140 function object(arg, name) {
  141         _assert(arg, 'object', name);
  142 }
  143 
  144 
  145 function stream(arg, name) {
  146         _instanceof(arg, Stream, name);
  147 }
  148 
  149 
  150 function date(arg, name) {
  151         _instanceof(arg, Date, name);
  152 }
  153 
  154 function regexp(arg, name) {
  155         _instanceof(arg, RegExp, name);
  156 }
  157 
  158 
  159 function string(arg, name) {
  160         _assert(arg, 'string', name);
  161 }
  162 
  163 
  164 function uuid(arg, name) {
  165         string(arg, name);
  166         if (!NDEBUG && !UUID_REGEXP.test(arg)) {
  167                 throw new assert.AssertionError({
  168                         message: _(TYPE_REQUIRED, name, 'uuid'),
  169                         actual: 'string',
  170                         expected: 'uuid',
  171                         operator: 'test',
  172                         stackStartFunction: uuid
  173                 });
  174         }
  175 }
  176 
  177 
  178 ///--- Exports
  179 
  180 module.exports = {
  181         bool: bool,
  182         buffer: buffer,
  183         date: date,
  184         func: func,
  185         number: number,
  186         object: object,
  187         regexp: regexp,
  188         stream: stream,
  189         string: string,
  190         uuid: uuid
  191 };
  192 
  193 
  194 Object.keys(module.exports).forEach(function (k) {
  195         if (k === 'buffer')
  196                 return;
  197 
  198         var name = 'arrayOf' + capitalize(k);
  199 
  200         if (k === 'bool')
  201                 k = 'boolean';
  202         if (k === 'func')
  203                 k = 'function';
  204         module.exports[name] = function (arg, name) {
  205                 array(arg, k, name);
  206         };
  207 });
  208 
  209 Object.keys(module.exports).forEach(function (k) {
  210         var _name = 'optional' + capitalize(k);
  211         var s = uncapitalize(k.replace('arrayOf', ''));
  212         if (s === 'bool')
  213                 s = 'boolean';
  214         if (s === 'func')
  215                 s = 'function';
  216 
  217         if (k.indexOf('arrayOf') !== -1) {
  218           module.exports[_name] = function (arg, name) {
  219                   if (!NDEBUG && arg !== undefined) {
  220                           array(arg, s, name);
  221                   }
  222           };
  223         } else {
  224           module.exports[_name] = function (arg, name) {
  225                   if (!NDEBUG && arg !== undefined) {
  226                           _assert(arg, s, name);
  227                   }
  228           };
  229         }
  230 });
  231 
  232 
  233 // Reexport built-in assertions
  234 Object.keys(assert).forEach(function (k) {
  235         if (k === 'AssertionError') {
  236                 module.exports[k] = assert[k];
  237                 return;
  238         }
  239 
  240         module.exports[k] = function () {
  241                 if (!NDEBUG) {
  242                         assert[k].apply(assert[k], arguments);
  243                 }
  244         };
  245 });