Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions packages/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
/**
* base.js
* This file contains all global functions provided by js.io.
*/

exports.log = jsio.__env.log;
exports.GLOBAL = jsio.__env.global;

/**
* Various polyfill methods to ensure js.io implementations provide
* a baseline of JavaScript functionality. Feature compatibility (localStorage,
* etc.) should be provided elsewhere.
*/

// Array.isArray
// Not available before ECMAScript 5.
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray

if (!Array.isArray) {
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
}
};

// Function.prototype.bind
// Not available before ECMAScript 5.
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}

/**
* DEPRECATED. Old js.io polyfills.
*/

var SLICE = Array.prototype.slice;

/* Use native isArray if available
Expand Down Expand Up @@ -38,6 +91,10 @@ exports.bind = function(context, method /*, VARGS*/) {
}
}

/**
* Class constructor.
*/

exports.Class = function(name, parent, proto) {
return exports.__class__(function() { return this.init && this.init.apply(this, arguments); }, name, parent, proto);
}
Expand Down Expand Up @@ -100,6 +157,10 @@ var ErrorParentClass = exports.__class__(function ErrorCls() {
}
}, function() {});

/**
* Merge two objects together.
*/

exports.Class.defaults =
exports.merge = function(base, extra) {
base = base || {};
Expand All @@ -116,6 +177,10 @@ exports.merge = function(base, extra) {
return base;
}

/**
* Create a timer delay.
*/

exports.delay = function(orig, timeout) {
var _timer = null;
var ctx, args;
Expand All @@ -128,7 +193,10 @@ exports.delay = function(orig, timeout) {
}
}

// keep logging local variables out of other closures in this file!
/**
* Log constructor and default "logger".
*/

exports.logging = (function() {

// logging namespace, this is what is exported
Expand Down Expand Up @@ -187,4 +255,4 @@ exports.logging = (function() {
return logging;
})();

var logger = exports.logging.get('jsiocore');
var logger = exports.logging.get('jsiocore');