angular-cn/modules/rtts_assert
Jeff Cross 55bf0e554f feat(http): refactor library to work in dart
Mostly internal refactoring needed to make ts2dart and DartAnalyzer happy.

Fixes #2415
2015-06-30 15:21:36 -07:00
..
src feat(http): refactor library to work in dart 2015-06-30 15:21:36 -07:00
test refactor(rtts_assert): Ts'ify rtts_assert 2015-06-03 18:12:08 -07:00
README.js.md chore(build): rename .es6 files to .js 2015-06-11 17:22:19 -07:00
package.json fix(build): finally publish to `rtts_assert` on npm 2015-02-18 12:52:27 -08:00
rtts_assert.ts refactor(rtts_assert): Ts'ify rtts_assert 2015-06-03 18:12:08 -07:00

README.js.md

Rtts-Assert

A run-time type assertion library for JavaScript. Designed to be used with Traceur.

All sources are in ES6 format. They don't depend on any runtime and can be used by any modern ES6 -> ES5 transpiler.

As a convenience, we provide you with es5build.js, a script to transpile the sources into es5 using Google Traceur.

License: Apache MIT 2.0

API

// Asserting APIs:
// - generated by Traceur (based on type annotations)
// - can be also used in tests for instance
assert.type(something, Type);
assert.returnType(returnValue, Type);
assert.argumentTypes(firstArg, Type, secondArg, Type);

// this can be used anywhere in the code
// (useful inside test, when we don't wanna define an interface)
assert(value).is(...)


// Custom type assert:
// - i have a custom type
// - adding an assert methos
assert.define(MyUser, function(value) {
  assert(value).is(Type, Type2); // or
  assert(value, 'name').is(assert.string);
  assert(value, 'contact').is(assert.structure({
    email: assert.string,
    cell: assert.string
  }));
  assert(value, 'contacts').is(assert.arrayOf(assert.structure({email: assert.string})));
});


// Define interface (an empty type with assert method)
// - returns an empty class with assert method
var Email = assert.define('IEmail', function(value) {
  assert(value).is(String);

  if (value.indexOf('@') !== -1) {
    assert.fail('has to contain "@"');
  }
});


// Predefined types
assert.string
assert.number
assert.boolean
assert.arrayOf(...types)
assert.structure(object)