Skip to content

Instantly share code, notes, and snippets.

@arian
Created April 5, 2013 16:44
Show Gist options
  • Save arian/5320763 to your computer and use it in GitHub Desktop.
Save arian/5320763 to your computer and use it in GitHub Desktop.
(function () {
var modules = {}, cache = {};
if (typeof define == 'undefined') {
window.define = function (id, factory) {
modules[id] = factory;
};
window.require = function (id) {
var module = cache[id];
if (!module) {
module = cache[id] = {};
var exports = module.exports = {};
modules[id].call(exports, require, module, exports, window);
}
return module.exports;
};
}
}());
define('prime/index', function (require, module, exports, global) {
'use strict';
var hasOwn = require('prime/object/hasOwn'), forIn = require('prime/object/forIn'), mixIn = require('prime/object/mixIn'), filter = require('prime/object/filter'), create = require('prime/object/create'), type = require('prime/type');
var defineProperty = Object.defineProperty, getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
try {
defineProperty({}, '~', {});
getOwnPropertyDescriptor({}, '~');
} catch (e) {
defineProperty = null;
getOwnPropertyDescriptor = null;
}
var define = function (value, key, from) {
defineProperty(this, key, getOwnPropertyDescriptor(from, key) || {
writable: true,
enumerable: true,
configurable: true,
value: value
});
};
var copy = function (value, key) {
this[key] = value;
};
var implement = function (proto) {
forIn(proto, defineProperty ? define : copy, this.prototype);
return this;
};
var verbs = /^constructor|inherits|mixin$/;
var prime = function (proto) {
if (type(proto) === 'function')
proto = { constructor: proto };
var superprime = proto.inherits;
var constructor = hasOwn(proto, 'constructor') ? proto.constructor : superprime ? function () {
return superprime.apply(this, arguments);
} : function () {
};
if (superprime) {
mixIn(constructor, superprime);
var superproto = superprime.prototype;
var cproto = constructor.prototype = create(superproto);
constructor.parent = superproto;
cproto.constructor = constructor;
}
if (!constructor.implement)
constructor.implement = implement;
var mixins = proto.mixin;
if (mixins) {
if (type(mixins) !== 'array')
mixins = [mixins];
for (var i = 0; i < mixins.length; i++)
constructor.implement(create(mixins[i].prototype));
}
return constructor.implement(filter(proto, function (value, key) {
return !key.match(verbs);
}));
};
module.exports = prime;
});
define('prime/object/hasOwn', function (require, module, exports, global) {
'use strict';
var hasOwn = function (self, key) {
return Object.hasOwnProperty.call(self, key);
};
module.exports = hasOwn;
});
define('prime/object/forIn', function (require, module, exports, global) {
'use strict';
var has = require('prime/object/hasOwn');
var forIn = function (object, method, context) {
for (var key in object)
if (method.call(context, object[key], key, object) === false)
break;
return object;
};
if (!{ valueOf: 0 }.propertyIsEnumerable('valueOf')) {
var buggy = 'constructor,toString,valueOf,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString'.split(',');
var proto = Object.prototype;
forIn = function (object, method, context) {
for (var key in object)
if (method.call(context, object[key], key, object) === false)
return object;
for (var i = 0; key = buggy[i]; i++) {
var value = object[key];
if ((value !== proto[key] || has(object, key)) && method.call(context, value, key, object) === false)
break;
}
return object;
};
}
module.exports = forIn;
});
define('prime/object/mixIn', function (require, module, exports, global) {
'use strict';
var forOwn = require('prime/object/forOwn');
var copy = function (value, key) {
this[key] = value;
};
var mixIn = function (self) {
for (var i = 1, l = arguments.length; i < l; i++)
forOwn(arguments[i], copy, self);
return self;
};
module.exports = mixIn;
});
define('prime/object/forOwn', function (require, module, exports, global) {
'use strict';
var forIn = require('prime/object/forIn'), hasOwn = require('prime/object/hasOwn');
var forOwn = function (self, method, context) {
forIn(self, function (value, key) {
if (hasOwn(self, key))
return method.call(context, value, key);
});
return self;
};
module.exports = forOwn;
});
define('prime/object/filter', function (require, module, exports, global) {
'use strict';
var forIn = require('prime/object/forIn');
var filter = function (self, method, context) {
var results = {};
forIn(self, function (value, key) {
if (method.call(context, value, key, self))
results[key] = value;
});
return results;
};
module.exports = filter;
});
define('prime/object/create', function (require, module, exports, global) {
'use strict';
var create = function (self) {
var constructor = function () {
};
constructor.prototype = self;
return new constructor();
};
module.exports = create;
});
define('prime/type', function (require, module, exports, global) {
'use strict';
var toString = Object.prototype.toString, types = /number|object|array|string|function|date|regexp|boolean/;
var type = function (object) {
if (object == null)
return 'null';
var string = toString.call(object).slice(8, -1).toLowerCase();
if (string === 'number' && isNaN(object))
return 'null';
if (types.test(string))
return string;
return 'object';
};
module.exports = type;
});
define('prime/shell', function (require, module, exports, global) {
'use strict';
var prime = require('prime/index'), type = require('prime/type'), forIn = require('prime/object/forIn');
var slice = Array.prototype.slice, push = Array.prototype.push;
var ghost = prime({
constructor: function ghost(self) {
this.valueOf = function () {
return self;
};
this.toString = function () {
return self + '';
};
this.is = function (object) {
return self === object;
};
}
});
var shell = function (self) {
if (self == null || self instanceof ghost)
return self;
var g = shell[type(self)];
return g ? new g(self) : self;
};
var register = function (methods) {
var g = prime({ inherits: ghost });
var s = prime(function (self) {
return new g(self);
});
s.extend = function (object) {
var self = this;
forIn(object, function (method, key) {
this[key] = method;
this.prototype[key] = function () {
if (!arguments.length)
return method.call(self, this);
var args = [this];
push.apply(args, arguments);
return method.apply(self, args);
};
g.prototype[key] = function () {
var value = this.valueOf();
if (!arguments.length)
return method.call(self, value);
var args = [value];
push.apply(args, arguments);
return method.apply(self, args);
};
}, this);
return this;
};
s.implement = function (object) {
forIn(object, function (method, key) {
this[key] = function (self) {
return arguments.length > 1 ? method.apply(self, slice.call(arguments, 1)) : method.call(self);
};
g.prototype[key] = function () {
return shell(method.apply(this.valueOf(), arguments));
};
this.prototype[key] = method;
}, this);
return this;
};
};
for (var types = 'string,number,array,object,date,function,regexp'.split(','), i = types.length; i--;)
shell[types[i]] = register();
module.exports = shell;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment