This package has been deprecated

Author message:

this package has been depricated

dbc.js

1.0.1 • Public • Published

dbc.js Build Status

A compact design-by-contract helper for nodejs and the browser.

Installation

node.js

$ npm install dbc.js

To use in a browser:

 <script src="../releases/dbc.js.min-1.0.js"></script> 

!! Be sure to fix the path relative to your static media assets and the referring page.

Example

var dbc = require('dbc.js')
, expect = require('expect.js')
;
 
// from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
var emailRe = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
 
function validateEmail(email) {
    return emailRe.test(email);
}
 
function Person(email, handle, last, first) {
    dbc([validateEmail(email)], 'email address must appear valid.')
    dbc([handle], 'a handle is required');
    dbc([typeof first === 'undefined' || typeof last === 'string'],
        'if you supply a first name you must also supply a last name');
 
    this.email = email;
    this.handle = handle;
    this.last = last;
    this.first = first;
}
 
expect(new Person('me@home.me', 'me')).to.be.a(Person);
 
expect(function() {
 
    // create a Person with a clearly invalid email address...
    new Person('it.doesn`t@compute')
 
}).to.throwError(/email address must appear valid/);
 
expect(function() {
 
    // create a Person without a handle
    new Person('jo@bob.me');
 
}).to.throwError(/a handle is required/);

Tests

Tests use mocha and expect.js, so if you clone the github repository you'll need to run:

npm install

... followed by ...

npm test

... or ...

mocha -R spec

To run the same tests from a browser, open the test/test.html file directly from your file system.

Readme

Keywords

none

Package Sidebar

Install

npm i dbc.js

Weekly Downloads

4

Version

1.0.1

License

MIT

Last publish

Collaborators

  • cerebralkungfu