An open API service indexing awesome lists of open source software.

https://github.com/nrkn/objectionablejs


https://github.com/nrkn/objectionablejs

Last synced: 4 days ago
JSON representation

Awesome Lists containing this project

README

        

```javascript
/*
objectionable-js

example code
*/

//interfaces like static languages but duck testing + primitives, enums etc.
var interfaces = {
//properties are primitives
Point: {
x: 'Number',
y: 'Number'
},

//properties are interfaces
Line: {
start: 'Point',
end: 'Point'
},

//you can mix properties as primitives/interfaces, example elided for tersity

//enum
Color: [
'red',
'green',
'blue'
],

//extending primative
HexColor: 'String',

//alt syntax for point as used by jQuery et al.
Position: {
left: 'Number',
top: 'Number'
}
};

var mixins = {
//mixin on primitive
Number: {
square: function( num ){
return num * num;
},
add: function( num, value ){
return num + value;
}
},
//mixin on interface
Point: {
square: function( point ){
return {
x: _( point.x ).square(),
y: _( point.y ).square()
};
}
},
//mixin with same name as existing mixins but on specific interface
Line: {
square: function( line ){
return {
start: _( line.start ).square(),
end: _( line.end ).square()
};
}
}
};

var mappings = {
Point: {
Position: function( point ){
return {
left: point.x,
top: point.y
};
},
Line: function( point ){
return {
start: { x: 0, y: 0 },
end: point
};
}
},
Line: {
Point: function( line ){
return {
x: line.end.x - line.start.x,
y: line.end.y - line.start.y
};
}
},
Position: {
Point: function( position ){
return {
x: position.left,
y: position.top
};
}
},
Color: {
Hex: function( color ){
return {
red: '#f00',
green: '#0f0',
blue: '#00f'
}[ color ];
}
}
};

//standard args constructor - all args optional
var obj = new Objectionable( interfaces, mixins, mappings );

//object constructor, all properties optional
var obj2 = new Objectionable({
interfaces: {
//...
},
mixins: {
//...
},
mappings: {
//...
}
});

//get underscore reference with mixins added
var _ = obj._;

//alias autogenerated constructors
var Point = obj.Point;
var Line = obj.Line;
var Color = obj.Color;

//standard constructor syntax
var point1 = new Point( 5, 1.5 );

//square method is on Point
console.log( point1.square() ); // { x: 25, y: 2.25 }

//literal syntax
var point2 = { x: 5, y: 1.5 };

//square method on literal via mixin
console.log( _( point2 ).square() ); // { x: 25, y: 2.25 }

//interface test on Point
point1.isPoint(); //true

//interface test via mixin
_( point2 ).isPoint()

//there is a mapping from Point to Line
point1.mapsToLine(); //true

//what interfaces?
console.log( obj.has( point1 ) ); // [ 'Point', 'Line', 'Object' ]

//standard constructor syntax
var line1 = new Line( 5, 1.5, 25, 2.25 );

console.log( line1 ); // { start: { x: 5, y: 1.5 }, end: { x: 25, y: 2.25 } }

//literal syntax
var line2 = { start: point1, end: _( point2 ).square() };

console.log( line2 ); // { start: { x: 5, y: 1.5 }, end: { x: 25, y: 2.25 } }

//autogenerated converter method on Point
var line3 = point1.toLine();

console.log( line3 ); // { start: { x: 0, y: 0 }, end: { x: 5, y: 1.5 } }

//autogenerated converter method on Line
var point3 = _( line1 ).toPoint();

console.log( point3 ); // { x: 20, y: 0.75 }

//standard syntax, enum
var color1 = new Color( 'red' );

//maps to string, but requires weak testing via ==
var color1IsRed1 = color1 == 'red'; // true
var color1IsRed2 = color1 === 'red'; // false

//enums also have a valueOf method for strict testing
var color1IsRed3 = color1.valueOf() === 'red'; // true

//test interface for enum from literal via mixin
_( 'red' ).isColor(); // true

//toHexColor converter is on Color
console.log( color1.toHexColor() ); // '#f00'

//throws:
try {
var color2 = new Color( 42 );
} catch( e ){}

//mixin on third party object
if( window && jQuery ){
//offset method return value matches interface Position which maps to Point:
var point4 = _( jQuery( 'html' ).offset() ).toPoint();

//probably it's 0,0
console.log( _( point4 ).toPoint() ); // { x: 0, y: 0 }
}
```