Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0ee76bc4516a7225c13fc314337602fd774ce321 David Luecke committed Jun 23, 2011
Showing with 578 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +190 −0 lib/class.js
  3. +156 −0 lib/util.js
  4. +21 −0 package.json
  5. 0 readme.md
  6. +164 −0 test/class.test.js
  7. +45 −0 test/util.test.js
@@ -0,0 +1,2 @@
+.settings
+.project
@@ -0,0 +1,190 @@
+// if we are initializing a new class
+var util = require('./util'),
+initializing = false, makeArray = util.makeArray, isFunction = util.isFunction, isArray = util.isArray,
+concatArgs = function(arr, args)
+{
+ return arr.concat(makeArray(args));
+},
+// tests if we can get super in .toString()
+fnTest = /xyz/.test(function()
+{
+ xyz;
+}) ? /\b_super\b/ : /.*/,
+
+// overwrites an object with methods, sets up _super
+inheritProps = function(newProps, oldProps, addTo)
+{
+ addTo = addTo || newProps;
+ for ( var name in newProps)
+ {
+ // Check if we're overwriting an existing function
+ addTo[name] = isFunction(newProps[name]) && isFunction(oldProps[name])
+ && fnTest.test(newProps[name]) ? (function(name, fn)
+ {
+ return function()
+ {
+ var tmp = this._super, ret;
+
+ // Add a new ._super() method that is the same method
+ // but on the super-class
+ this._super = oldProps[name];
+
+ // The method only need to be bound temporarily, so we
+ // remove it when we're done executing
+ ret = fn.apply(this, arguments);
+ this._super = tmp;
+ return ret;
+ };
+ })(name, newProps[name]) : newProps[name];
+ }
+};
+
+var clss = exports.Class = function()
+{
+ if (arguments.length)
+ {
+ clss.extend.apply(clss, arguments);
+ }
+};
+
+util.extend(
+ clss,
+ {
+ callback : function(funcs)
+ {
+ // args that should be curried
+ var args = makeArray(arguments), self;
+
+ funcs = args.shift();
+
+ if (!isArray(funcs))
+ {
+ funcs = [ funcs ];
+ }
+
+ self = this;
+ for ( var i = 0; i < funcs.length; i++)
+ {
+ if (typeof funcs[i] == "string"
+ && !isFunction(this[funcs[i]]))
+ {
+ throw ("class.js "
+ + (this.fullName || this.Class.fullName)
+ + " does not have a " + funcs[i] + "method!");
+ }
+ }
+ return function class_cb()
+ {
+ var cur = concatArgs(args, arguments), isString, length = funcs.length, f = 0, func;
+ for (; f < length; f++)
+ {
+ func = funcs[f];
+ if (!func)
+ {
+ continue;
+ }
+
+ isString = typeof func == "string";
+ if (isString && self._set_called)
+ {
+ self.called = func;
+ }
+
+ cur = (isString ? self[func] : func).apply(self, cur
+ || []);
+ if (f < length - 1)
+ {
+ cur = !isArray(cur) || cur._use_call ? [ cur ]
+ : cur;
+ }
+ }
+ return cur;
+ };
+ },
+
+ newInstance: function() {
+ var inst = this.rawInstance(),
+ args;
+ if ( inst.setup ) {
+ args = inst.setup.apply(inst, arguments);
+ }
+ if ( inst.init ) {
+ inst.init.apply(inst, isArray(args) ? args : arguments);
+ }
+ return inst;
+ },
+
+ setup: function( baseClass, fullName ) {
+ this.defaults = util.extend(true, {}, baseClass.defaults, this.defaults);
+ return arguments;
+ },
+
+ rawInstance: function() {
+ initializing = true;
+ var inst = new this();
+ initializing = false;
+ return inst;
+ },
+
+ extend: function(klass, proto ) {
+ // figure out what was passed
+ if (!proto ) {
+ proto = klass;
+ klass = null;
+ }
+
+ proto = proto || {};
+ var _super_class = this, _super = this.prototype, prototype;
+
+ // Instantiate a base class (but only create the instance,
+ // don't run the init constructor)
+ initializing = true;
+ prototype = new this();
+ initializing = false;
+ // Copy the properties over onto the new prototype
+ inheritProps(proto, _super, prototype);
+
+ // The dummy class constructor
+
+ function Class() {
+ // All construction is actually done in the init method
+ if ( initializing ) return;
+
+ if ( this.constructor !== Class && arguments.length ) { //we are being called w/o new
+ return arguments.callee.extend.apply(arguments.callee, arguments);
+ } else { //we are being called w/ new
+ return this.Class.newInstance.apply(this.Class, arguments);
+ }
+ }
+
+ // Copy old stuff onto class
+ for ( name in this ) {
+ if ( this.hasOwnProperty(name) && ['prototype', 'defaults'].indexOf(name) == -1 ) {
+ Class[name] = this[name];
+ }
+ }
+
+ // do static inheritance
+ inheritProps(klass, this, Class);
+
+ // set things that can't be overwritten
+ util.extend(Class, {
+ prototype: prototype,
+ constructor: Class
+ });
+
+ //make sure our prototype looks nice
+ Class.prototype.Class = Class.prototype.constructor = Class;
+
+ var args = Class.setup.apply(Class, concatArgs([_super_class],arguments));
+
+ if ( Class.init ) {
+ Class.init.apply(Class, args || []);
+ }
+
+ /* @Prototype*/
+ return Class;
+ }
+ });
+
+clss.prototype.callback = clss.callback;
@@ -0,0 +1,156 @@
+var merge = exports.merge = function(first, second)
+{
+ var i = first.length, j = 0;
+
+ if (typeof second.length === "number")
+ {
+ for ( var l = second.length; j < l; j++)
+ {
+ first[i++] = second[j];
+ }
+
+ } else
+ {
+ while (second[j] !== undefined)
+ {
+ first[i++] = second[j++];
+ }
+ }
+
+ first.length = i;
+
+ return first;
+};
+
+var makeArray = exports.makeArray = function(array, results)
+{
+ var ret = results || [];
+
+ if (array != null)
+ {
+ var type = typeof array;
+ if ( array.length == null || type === "string" || type === "function" || type === "regexp")
+ {
+ ret.push(array);
+ } else
+ {
+ merge(ret, array);
+ }
+ }
+
+ return ret;
+};
+
+var isFunction = exports.isFunction = function(fn)
+{
+ return typeof fn === 'function';
+};
+
+var isArray = exports.isArray = function(arr)
+{
+ return Array.isArray(arr);
+};
+
+var concatArgs = exports.concatArgs = function(arr, args)
+{
+ return arr.concat(makeArray(args));
+};
+
+var isPlainObject = exports.isPlainObject = function(obj)
+{
+ // Must be an Object.
+ // Because of IE, we also have to check the presence of the constructor
+ // property.
+ // Make sure that DOM nodes and window objects don't pass through, as
+ // well
+ if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType
+ || obj.setInterval)
+ {
+ return false;
+ }
+
+ var has_own_constructor = hasOwnProperty.call(obj, "constructor");
+ var has_is_property_of_method = hasOwnProperty.call(
+ obj.constructor.prototype, "isPrototypeOf");
+ // Not own constructor property must be Object
+ if (obj.constructor && !has_own_constructor
+ && !has_is_property_of_method)
+ {
+ return false;
+ }
+
+ // Own properties are enumerated firstly, so to speed up,
+ // if last one is own, then all properties are own.
+
+ var last_key;
+ for (key in obj)
+ {
+ last_key = key;
+ }
+
+ return typeof last_key === "undefined"
+ || hasOwnProperty.call(obj, last_key);
+};
+
+var extend = exports.extend = function()
+{
+ // copy reference to target object
+ var target = arguments[0] ||
+ {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
+
+ // Handle a deep copy situation
+ if (typeof target === "boolean")
+ {
+ deep = target;
+ target = arguments[1] ||
+ {};
+ // skip the boolean and the target
+ i = 2;
+ }
+
+ // Handle case when target is a string or something (possible in deep copy)
+ if (typeof target !== "object" && !typeof target === 'function')
+ {
+ target =
+ {};
+ }
+
+ for (; i < length; i++)
+ {
+ // Only deal with non-null/undefined values
+ if ((options = arguments[i]) !== null)
+ {
+ // Extend the base object
+ for (name in options)
+ {
+ src = target[name];
+ copy = options[name];
+
+ // Prevent never-ending loop
+ if (target === copy)
+ {
+ continue;
+ }
+
+ // Recurse if we're merging object literal values or arrays
+ if (deep && copy && (isPlainObject(copy) || isArray(copy)))
+ {
+ var clone = src && (isPlainObject(src) || isArray(src)) ? src
+ : isArray(copy) ? [] :
+ {};
+
+ // Never move original objects, clone them
+ target[name] = extend(deep, clone, copy);
+
+ // Don't bring in undefined values
+ } else if (typeof copy !== "undefined")
+ {
+ target[name] = copy;
+ }
+ }
+ }
+ }
+
+ // Return the modified object
+ return target;
+};
@@ -0,0 +1,21 @@
+{
+ "name": "JS.Class",
+ "description": "A class framework bridging the gap between functional and Object Oriented programming styles based on JavaScriptMVC $.Class and John Resig's Simple JavaScript inheritance.",
+ "version": "0.8.0",
+ "homepage": "http://github.com/daffl/js.class",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/daffl/js.class.git"
+ },
+ "author": "David Luecke <daff@neyeon.de> (http://neyeon.com)",
+ "main": "lib/class",
+ "directories": {
+ "lib": "lib"
+ },
+ "scripts": {
+ "test": "nodeunit test"
+ },
+ "engines": {
+ "node": "*"
+ }
+}
No changes.
Oops, something went wrong.

0 comments on commit 0ee76bc

Please sign in to comment.