Sha256: 4e0a684a6c869a9a40e100d7feea2c61c08a29621368fb294326c00fd3fb246d

Contents?: true

Size: 1.39 KB

Versions: 26

Compression:

Stored size: 1.39 KB

Contents

/*!
 * Chai - expectTypes utility
 * Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
 * MIT Licensed
 */

import {AssertionError} from 'assertion-error';
import {flag} from './flag.js';
import {type} from './type-detect.js';

/**
 * ### .expectTypes(obj, types)
 *
 * Ensures that the object being tested against is of a valid type.
 *
 *     utils.expectTypes(this, ['array', 'object', 'string']);
 *
 * @param {unknown} obj constructed Assertion
 * @param {Array} types A list of allowed types for this assertion
 * @namespace Utils
 * @name expectTypes
 * @public
 */
export function expectTypes(obj, types) {
  var flagMsg = flag(obj, 'message');
  var ssfi = flag(obj, 'ssfi');

  flagMsg = flagMsg ? flagMsg + ': ' : '';

  obj = flag(obj, 'object');
  types = types.map(function (t) { return t.toLowerCase(); });
  types.sort();

  // Transforms ['lorem', 'ipsum'] into 'a lorem, or an ipsum'
  var str = types.map(function (t, index) {
    var art = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(t.charAt(0)) ? 'an' : 'a';
    var or = types.length > 1 && index === types.length - 1 ? 'or ' : '';
    return or + art + ' ' + t;
  }).join(', ');

  var objType = type(obj).toLowerCase();

  if (!types.some(function (expected) { return objType === expected; })) {
    throw new AssertionError(
      flagMsg + 'object tested must be ' + str + ', but ' + objType + ' given',
      undefined,
      ssfi
    );
  }
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.25 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.24 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.23 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.22 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.21 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.20 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.19 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.18 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.17 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.16 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.15 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.14 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.13 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.12 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.11 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.10 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.9 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.8 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js
clapton-0.0.7 lib/clapton/javascripts/node_modules/chai/lib/chai/utils/expectTypes.js