Path: blob/master/node_modules/assert-plus/assert.js
2591 views
// Copyright (c) 2012, Mark Cavage. All rights reserved.1// Copyright 2015 Joyent, Inc.23var assert = require('assert');4var Stream = require('stream').Stream;5var util = require('util');678///--- Globals910/* JSSTYLED */11var 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}$/;121314///--- Internal1516function _capitalize(str) {17return (str.charAt(0).toUpperCase() + str.slice(1));18}1920function _toss(name, expected, oper, arg, actual) {21throw new assert.AssertionError({22message: util.format('%s (%s) is required', name, expected),23actual: (actual === undefined) ? typeof (arg) : actual(arg),24expected: expected,25operator: oper || '===',26stackStartFunction: _toss.caller27});28}2930function _getClass(arg) {31return (Object.prototype.toString.call(arg).slice(8, -1));32}3334function noop() {35// Why even bother with asserts?36}373839///--- Exports4041var types = {42bool: {43check: function (arg) { return typeof (arg) === 'boolean'; }44},45func: {46check: function (arg) { return typeof (arg) === 'function'; }47},48string: {49check: function (arg) { return typeof (arg) === 'string'; }50},51object: {52check: function (arg) {53return typeof (arg) === 'object' && arg !== null;54}55},56number: {57check: function (arg) {58return typeof (arg) === 'number' && !isNaN(arg);59}60},61finite: {62check: function (arg) {63return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);64}65},66buffer: {67check: function (arg) { return Buffer.isBuffer(arg); },68operator: 'Buffer.isBuffer'69},70array: {71check: function (arg) { return Array.isArray(arg); },72operator: 'Array.isArray'73},74stream: {75check: function (arg) { return arg instanceof Stream; },76operator: 'instanceof',77actual: _getClass78},79date: {80check: function (arg) { return arg instanceof Date; },81operator: 'instanceof',82actual: _getClass83},84regexp: {85check: function (arg) { return arg instanceof RegExp; },86operator: 'instanceof',87actual: _getClass88},89uuid: {90check: function (arg) {91return typeof (arg) === 'string' && UUID_REGEXP.test(arg);92},93operator: 'isUUID'94}95};9697function _setExports(ndebug) {98var keys = Object.keys(types);99var out;100101/* re-export standard assert */102if (process.env.NODE_NDEBUG) {103out = noop;104} else {105out = function (arg, msg) {106if (!arg) {107_toss(msg, 'true', arg);108}109};110}111112/* standard checks */113keys.forEach(function (k) {114if (ndebug) {115out[k] = noop;116return;117}118var type = types[k];119out[k] = function (arg, msg) {120if (!type.check(arg)) {121_toss(msg, k, type.operator, arg, type.actual);122}123};124});125126/* optional checks */127keys.forEach(function (k) {128var name = 'optional' + _capitalize(k);129if (ndebug) {130out[name] = noop;131return;132}133var type = types[k];134out[name] = function (arg, msg) {135if (arg === undefined || arg === null) {136return;137}138if (!type.check(arg)) {139_toss(msg, k, type.operator, arg, type.actual);140}141};142});143144/* arrayOf checks */145keys.forEach(function (k) {146var name = 'arrayOf' + _capitalize(k);147if (ndebug) {148out[name] = noop;149return;150}151var type = types[k];152var expected = '[' + k + ']';153out[name] = function (arg, msg) {154if (!Array.isArray(arg)) {155_toss(msg, expected, type.operator, arg, type.actual);156}157var i;158for (i = 0; i < arg.length; i++) {159if (!type.check(arg[i])) {160_toss(msg, expected, type.operator, arg, type.actual);161}162}163};164});165166/* optionalArrayOf checks */167keys.forEach(function (k) {168var name = 'optionalArrayOf' + _capitalize(k);169if (ndebug) {170out[name] = noop;171return;172}173var type = types[k];174var expected = '[' + k + ']';175out[name] = function (arg, msg) {176if (arg === undefined || arg === null) {177return;178}179if (!Array.isArray(arg)) {180_toss(msg, expected, type.operator, arg, type.actual);181}182var i;183for (i = 0; i < arg.length; i++) {184if (!type.check(arg[i])) {185_toss(msg, expected, type.operator, arg, type.actual);186}187}188};189});190191/* re-export built-in assertions */192Object.keys(assert).forEach(function (k) {193if (k === 'AssertionError') {194out[k] = assert[k];195return;196}197if (ndebug) {198out[k] = noop;199return;200}201out[k] = assert[k];202});203204/* export ourselves (for unit tests _only_) */205out._setExports = _setExports;206207return out;208}209210module.exports = _setExports(process.env.NODE_NDEBUG);211212213