Socket
Socket
Sign inDemoInstall

dbc.js

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dbc.js

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


Version published
Weekly downloads
41
increased by156.25%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 08 Jul 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc