Utils.Conforms Class
Helper to test if a instance implements a certain interface
Item Index
Methods
Methods
conforms
(
Boolean
-
iDescriptor -
instance
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:
-
iDescriptorObjectKey-Value pairs describing the interface
-
instanceObjectThe 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
