react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / http-signature / node_modules / assert-plus / assert.js
81151 views// Copyright (c) 2012, Mark Cavage. All rights reserved.12var assert = require('assert');3var Stream = require('stream').Stream;4var util = require('util');5678///--- Globals910var NDEBUG = process.env.NODE_NDEBUG || false;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}$/;12131415///--- Messages1617var ARRAY_TYPE_REQUIRED = '%s ([%s]) required';18var TYPE_REQUIRED = '%s (%s) is required';19202122///--- Internal2324function capitalize(str) {25return (str.charAt(0).toUpperCase() + str.slice(1));26}2728function uncapitalize(str) {29return (str.charAt(0).toLowerCase() + str.slice(1));30}3132function _() {33return (util.format.apply(util, arguments));34}353637function _assert(arg, type, name, stackFunc) {38if (!NDEBUG) {39name = name || type;40stackFunc = stackFunc || _assert.caller;41var t = typeof (arg);4243if (t !== type) {44throw new assert.AssertionError({45message: _(TYPE_REQUIRED, name, type),46actual: t,47expected: type,48operator: '===',49stackStartFunction: stackFunc50});51}52}53}545556function _instanceof(arg, type, name, stackFunc) {57if (!NDEBUG) {58name = name || type;59stackFunc = stackFunc || _instanceof.caller;6061if (!(arg instanceof type)) {62throw new assert.AssertionError({63message: _(TYPE_REQUIRED, name, type.name),64actual: _getClass(arg),65expected: type.name,66operator: 'instanceof',67stackStartFunction: stackFunc68});69}70}71}7273function _getClass(object) {74return (Object.prototype.toString.call(object).slice(8, -1));75};76777879///--- API8081function array(arr, type, name) {82if (!NDEBUG) {83name = name || type;8485if (!Array.isArray(arr)) {86throw new assert.AssertionError({87message: _(ARRAY_TYPE_REQUIRED, name, type),88actual: typeof (arr),89expected: 'array',90operator: 'Array.isArray',91stackStartFunction: array.caller92});93}9495for (var i = 0; i < arr.length; i++) {96_assert(arr[i], type, name, array);97}98}99}100101102function bool(arg, name) {103_assert(arg, 'boolean', name, bool);104}105106107function buffer(arg, name) {108if (!Buffer.isBuffer(arg)) {109throw new assert.AssertionError({110message: _(TYPE_REQUIRED, name || '', 'Buffer'),111actual: typeof (arg),112expected: 'buffer',113operator: 'Buffer.isBuffer',114stackStartFunction: buffer115});116}117}118119120function func(arg, name) {121_assert(arg, 'function', name);122}123124125function number(arg, name) {126_assert(arg, 'number', name);127if (!NDEBUG && (isNaN(arg) || !isFinite(arg))) {128throw new assert.AssertionError({129message: _(TYPE_REQUIRED, name, 'number'),130actual: arg,131expected: 'number',132operator: 'isNaN',133stackStartFunction: number134});135}136}137138139function object(arg, name) {140_assert(arg, 'object', name);141}142143144function stream(arg, name) {145_instanceof(arg, Stream, name);146}147148149function date(arg, name) {150_instanceof(arg, Date, name);151}152153function regexp(arg, name) {154_instanceof(arg, RegExp, name);155}156157158function string(arg, name) {159_assert(arg, 'string', name);160}161162163function uuid(arg, name) {164string(arg, name);165if (!NDEBUG && !UUID_REGEXP.test(arg)) {166throw new assert.AssertionError({167message: _(TYPE_REQUIRED, name, 'uuid'),168actual: 'string',169expected: 'uuid',170operator: 'test',171stackStartFunction: uuid172});173}174}175176177///--- Exports178179module.exports = {180bool: bool,181buffer: buffer,182date: date,183func: func,184number: number,185object: object,186regexp: regexp,187stream: stream,188string: string,189uuid: uuid190};191192193Object.keys(module.exports).forEach(function (k) {194if (k === 'buffer')195return;196197var name = 'arrayOf' + capitalize(k);198199if (k === 'bool')200k = 'boolean';201if (k === 'func')202k = 'function';203module.exports[name] = function (arg, name) {204array(arg, k, name);205};206});207208Object.keys(module.exports).forEach(function (k) {209var _name = 'optional' + capitalize(k);210var s = uncapitalize(k.replace('arrayOf', ''));211if (s === 'bool')212s = 'boolean';213if (s === 'func')214s = 'function';215216if (k.indexOf('arrayOf') !== -1) {217module.exports[_name] = function (arg, name) {218if (!NDEBUG && arg !== undefined) {219array(arg, s, name);220}221};222} else {223module.exports[_name] = function (arg, name) {224if (!NDEBUG && arg !== undefined) {225_assert(arg, s, name);226}227};228}229});230231232// Reexport built-in assertions233Object.keys(assert).forEach(function (k) {234if (k === 'AssertionError') {235module.exports[k] = assert[k];236return;237}238239module.exports[k] = function () {240if (!NDEBUG) {241assert[k].apply(assert[k], arguments);242}243};244});245246247