All files / src/util is.js

100% Statements 37/37
100% Branches 32/32
100% Functions 6/6
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                1x 1x       13x 13x                     630x 469x       161x 161x                 530x 32x   498x 29x   469x 2x   467x 467x             466x     1x                             132x 1x   131x 530x 530x 530x 168x 1x   362x 9x     121x 424x 2x                       8x           7x                         8x         8x 8x       1x  
'use strict';
/**
 * Utility for checking and validating JavaScript values.
 *
 * @private
 * @module util/is
 */
 
const hasOwn = Object.prototype.hasOwnProperty;
const objToString = Object.prototype.toString;
 
class Validation extends Error {
	constructor( message, inputName ) {
		super( message );
		this.name = `Validation${( inputName ? ` of ${inputName}` : '' )}`;
	}
}
 
/**
 * @ignore
 * @param {Mixed} value
 * @return {boolean}
 */
function plainObject( value ) {
	// Reject null, undefined, and various built-in types
	if ( !value || objToString.call( value ) !== '[object Object]' ) {
		return false;
	}
	// Reject instances of anything other than direct Object.prototype
	// (e.g. from an object literal) or Object.create( null ).
	const proto = Object.getPrototypeOf( value );
	return ( proto === null || proto === Object.prototype );
}
 
/**
 * @ignore
 * @param {Mixed} value
 * @return {string}
 */
function type( value ) {
	if ( Array.isArray( value ) ) {
		return 'array';
	}
	if ( plainObject( value ) ) {
		return 'object';
	}
	if ( value === null ) {
		return 'null';
	}
	const of = typeof value;
	switch ( of ) {
		case 'undefined':
		case 'boolean':
		case 'number':
		case 'string':
		case 'symbol':
		case 'function':
			return of;
		case 'object':
		default:
			return 'unknown';
	}
}
 
/**
 * Validate an object's shape.
 *
 * Supports primitives and JSON-compatible structures (array, plain object).
 *
 * @param {Object|undefined} value
 * @param {Object} shape
 * @param {string} [name]
 * @throws {Error} If invalid
 */
function like( value, shape, name ) {
	if ( !plainObject( value ) ) {
		throw new Validation( 'Non-object', name );
	}
	for ( const key in shape ) {
		const expected = shape[ key ];
		const actual = type( value[ key ] );
		if ( Array.isArray( expected ) ) {
			if ( expected.indexOf( actual ) === -1 ) {
				throw new Validation( `Expected "${key}" as ${expected.join( '|' )}, got ${actual}`, name );
			}
		} else if ( actual !== expected ) {
			throw new Validation( `Expected "${key}" as ${expected}, got ${actual}`, name );
		}
	}
	for ( const key in value ) {
		if ( !hasOwn.call( shape, key ) ) {
			throw new Validation( `Unexpected key "${key}"`, name );
		}
	}
}
 
/**
 * Whether a given value is a valid Fresnel scenario.
 *
 * @param {Object} value
 * @throws {Error} If invalid
 */
function scenario( value ) {
	like( value, {
		url: 'string',
		viewport: 'object',
		reports: [ 'array', 'undefined' ],
		probes: [ 'array', 'undefined' ]
	}, 'scenario' );
	like( value.viewport, {
		height: 'number',
		width: 'number'
	}, 'scenario.viewport' );
}
 
/**
 * Whether a given value is a valid Fresnel configuration object.
 *
 * @param {Object} value
 * @throws {Error} If invalid
 */
function config( value ) {
	like( value, {
		warmup: 'boolean',
		runs: 'number',
		scenarios: [ 'object', 'array' ]
	}, 'config' );
	for ( const key in value.scenarios ) {
		scenario( value.scenarios[ key ] );
	}
}
 
module.exports = { like, scenario, config };