{purejs}
effortless. javascript.
The missing API for Javascript.
{constructors}
Reliably handle constructors and the prototype.
Create a constructor ...
var MyConstructor = Pure.constructor.create(function () {
// This is the constructor.
// Here is where we initialize the instance.
// All instance-level members are defined here.
},{
// Here is where we define prototype-level members.
});
Create a constructor inheriting a prototype ...
var MyOtherConstructor = Pure.constructor.create(function () {
// Call the base constructor.
MyConstructor.call(this);
}, {
// Empty prototype members.
}, MyConstructor.prototype);
Create a constructor inheriting a prototype, but cache the base prototype ...
var MyOtherConstructor2 = (function(base) {
return Pure.constructor.create(function () {
// Call the base constructor.
base.call(this);
}, {
// Empty prototype members.
}, base.prototype);
}(MyConstructor));
Create a constructor inheriting a constructor's prototype ...
var MyOtherConstructor3 = (function(base) {
return Pure.constructor.create(function () {
// Call the base constructor.
base.call(this);
}, {
// Empty prototype members.
}, base); // You can pass a constructor function directly not just objects
}(MyConstructor));
{interface testing}
Perform interface adherence tests on any object.
Ensure an object has a specific interface ...
var Rect = Pure.constructor.create({
init: function(w, h) {
this._width = w;
this._height = h;
},
width: function(value) {
if (arguments.length === 1) this._width = value;
return this._width;
},
height: function(value) {
if (arguments.length === 1) this._height = value;
return this._height;
}
});
var r = {
width: 40,
height: 40
};
var rect = Rect(10, 10);
// true since all objects adhere to their own interface.
Pure.adheresTo(r, r);
// false since width and height are not functions and init is not present.
Pure.adheresTo(r, Rect.prototype);
// false since width and height are not functions and init is not present
// and _width and _height are not present.
Pure.adheresTo(r, rect);
// true since we test r against an interface specification.
Pure.adheresTo(r, {
width: 'number',
height: 'number'
});
// true since we test r against an interface specification with
// the 'any type' specifier for both width and height.
Pure.adheresTo(r, {
width: '*',
height: '*'
});
{mixins}
Share properties with mixin support.
Copy all own properties from one object to another ...
var a = {name: 'a'}, b = {name: 'b', sex: 'm'};
// Copy all own properties from 'b' into 'a',
// overwriting properties with the same name.
// The result: a={name: 'b', sex: 'm'}
Pure.mixin(a, b);
Copy all own properties from many objects to another ...
var a = {name: 'a'}, b = {name: 'b', sex: 'm'}, c = {name: 'c', age: 29};
// Copy all own properties from 'b' and 'c' into 'a',
// overwriting properties with the same name.
// The result: a={name: 'c', sex: 'm, age: 29'}
Pure.mixin(a, b, c);
{type testing}
Intuitive tests for all Javascript types.
Test if an object is an array ...
var a = [], b = new Array(), c = {}, d = 1;
Pure.isArray(a); // true
Pure.isArray(b); // true
Pure.isArray(c); // false
Pure.isArray(d); // false
Get the true type of an object ...
var a = null, b = [], c = 3, d = {}, e = 'hello world', f = new Number(34);
Pure.typeOf(a); // 'null'
Pure.typeOf(b); // 'array'
Pure.typeOf(c); // 'number'
Pure.typeOf(d); // 'object'
Pure.typeOf(e); // 'string'
Pure.typeOf(f); // 'object'
Intelligently check the type of an object ...
var a = 1, b = new Number(1), c = 'hello', d = new String('hello');
Pure.isNumber(a); // true
Pure.isNumber(b); // true
Pure.isString(c); // true
Pure.isString(d); // true