API Docs for:
Show:

Utils.Conforms Class

Helper to test if a instance implements a certain interface

Item Index

Methods

Methods

conforms

(
  • iDescriptor
  • instance
)
Boolean

Accepts two objects, of which the first one defines the shape the second object must be conform to. The conforms() function is especially useful if you have a function which only accepts a certain kind of input object (for example: a Gallery accepts only GalleryItems) to ensure the input has a matching interface

Parameters:

  • iDescriptor Object

    Key-Value pairs describing the interface

  • instance Object

    The instance to check for conformity

Returns:

Boolean:

True if conform, false if not

Example:

import {conforms} from './utils/conforms'; import {isString, isFunction} from './utils/type';

var iDesc = { name: isString, sayHi: isFunction };

var testeeA = { name: 'John Doe', sayHi: function () { return 'Hi, I am ' + this.name; } };

var testeeB = {
   name: null,
   sayHi: true

};

conforms(iDesc, testeeA); // -> true
conforms(iDesc, testeeB); // -> false


// optional alternative, produces a function which only awaits the
//   object to test
var conformsIDesc = conforms(iDesc);

conformsIDesc(testeeA);
// -> true