Jemima
A naïve little (puddle) duck typing library for Node.js
Installation
Just install with npm install jemima
or add it to your
package.json
.
If you use a bundler like Browserify, then Jemima will work just fine in-browser too.
Usage
You can use Jemima with JavaScript or CoffeeScript:
var jemima = ;
jemima = require 'jemima'
Jemima exposes a few functions to help duck-type:
jemima.hasProperty
This function tests to see whether an object has the specified property. You call it with an object (what we're testing) and a string (the property we're testing for); it will return a boolean value indicating whether the object has the property:
var obj = foo: 123; jemima; // truejemima; // false
jemima.hasProperties
This function tests to see whether an object has the specified properties. You call it with an object (what we're testing) and an array of strings (the properties we're testing for); it will return a boolean value indicating whether the object has all of the properties:
var obj = foo: 123 bar: 456; jemima; // truejemima; // false
jemima.hasMethod
This function tests to see whether an object has the specified
method – it's similar to hasProperty
but also tests that the
property is a function. You call it with an object (what we're
testing) and a string (the method we're testing for); it will
return a boolean value indicating whether the object has the
method:
var obj = {} bar: 123; jemima; // truejemima; // false
jemima.hasMethods
This function tests to see whether an object has the specified
methods – it's similar to hasProperties
but also tests that
the properties are functions. You call it with an object (what
we're testing) and an array of strings (the methods we're
testing for); it will return a boolean value indicating whether
the object has all of the methods:
var obj = {} {} baz: 123; jemima; // truejemima; // false
jemima.matches
This function tests whether an object has the specified properties and that the type of object stored in each property is as expected. You call it with an object (what we're testing) and a 'duck' (an object which specifies the expected properties and types); it will return a boolean value indicating whether all of the properties match:
var duck = foo: 'string' bar: 'function'; jemima; // true jemima; // false jemima; // false
Development
In order to develop Jemima, you'll need to install the following npm modules globally like so:
npm install -g coffee-script
npm install -g jake
And then install development dependencies locally with:
npm install
Once you have these dependencies, you will be able to run the following commands:
jake build
: Build JavaScript from the CoffeeScript source.
jake lint
: Run CoffeeLint on the CoffeeScript source.
jake test
: Run all unit tests.
License
Dual licensed under the MIT or GPL Version 2 licenses.