angular / assert Goto Github PK
View Code? Open in Web Editor NEWA runtime type assertion library.
A runtime type assertion library.
E.g. the following code would report a type error:
static assert(obj) {
assert(obj).is(assert.structureOf({
createRootView: Function
}));
}
The real problem was the typo: It needs to be assert.structure
instead of assert.structureOf
. However, the error that assert generated was misleading.
There should be a dist file that exports the assert function on window. This is useful if you want to use this library for type assertions with traceur, but don't want to migrate your whole angular-project to use requirejs :)
var res:ArrayOfClass = [];
When using assert.is
, I'm running into a Maximum Call Stack Exceeded RangeError when the value is of type Document
. All other types work fine. This is because prettyprint
is seeing it as typeof "object" and is trying to recurse over its properties to print them. I can submit a fix for this later.
var doc = document.implementation.createDocument(null, 'doc');
assert(doc).is(DataView, Blob, Document);
Reproduction:
assert(obj).is(assert.string, Object)
karma start
I should be able to require that a value is exactly the type I specify, and expect it to throw if given null
.
var val = null;
assert.type(val, assert.string);
That assertion passes in the current implementation. It would be nice be able to indicate that a value is nullable: assert.type(val, assert.string, true);
Or if we wanted to extend the language to support this: assert.type(?val, assert.string);
or non-nullable: assert.type(!val, assert.string);
.
Trying to apply Type Assertions for a function that takes optional arguments. when the optional arguments are not provided, it throws errors. will it be nice if assert.argumentTypes
check and only apply type checking if optional argument is not undefined
?
in the example below, value
argument is optional and type is number
.
My suggestion is, validate type only if an optional argument is supplied.
export class EnumItem {
// constructor(name: string, {value} = {}, value: number) { //TODO want to use this
constructor(name: string, {value = Infinity}, value: number) { //workaround
this.name = name;
// this.value = (value) ? value: Symbol(name); //want to use this
this.value = (value === Infinity) ? Symbol(name) : value; //workaround
delete arguments[1].value;
Object.assign(this, arguments[1]);
Object.freeze(this);
}
toString() {
return this.name;
}
valueOf() {
return this.value;
}
}
console.log('EnumItem1', new EnumItem('xname',{value:2}));
console.log('EnumItem2', new EnumItem('xname',{ description:'ssss'}));
Is there an easy way to assert a structure that has some required fields and some optional fields. And have it fail if there are any other unknown fields.
This works but I'm just wondering if there is a better way:
export default class Directive {
constructor(options: DirectiveOptions) {
this.selector = options.selector;
this.bind = options.bind;
}
}
var DirectiveOptions = assert.define('DirectiveOptions', function(options) {
//Required fields
assert(options).is(assert.structure({
selector: assert.string
}));
//Optional fields
if(options.bind) {
assert.type(options.bind, Object);
}
for(var key in options) {
if (options.hasOwnProperty(key)) {
if(key !== 'selector' && key !== 'bind') {
assert.fail(`${key} is not a valid directive field`);
}
}
}
});
On an unrelated note any idea what I need to use assert.string
instead of just string
. I'm transpiling with Traceur 0.0.74.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.