+++ /dev/null
-(function(){
-
-this.Y = Y;
-
-var globals = this
+var undefined
+, globals = (function(){ return this; })()
, _Object = globals.Object
, _Function = globals.Function
, _Array = globals.Array
, _Number = globals.Number
, slice = _Array.prototype.slice
-, toString = _Object.prototype.toString
, hasOwn = _Object.prototype.hasOwnProperty
, getProto = _Object.getPrototypeOf
;
// Inspired by John Resig's "Simple Class Inheritence" -- http://ejohn.org/blog/simple-javascript-inheritance/
-var KNOWN_CLASSES = {}
+var Y = require('Y').Y
+, type = require('Y/type')
+, KNOWN_CLASSES = type.type.KNOWN_CLASSES
, classToString = function toString(){ return this.className+"()"; }
;
, instance = this;
// Not subclassing
- if ( cls.caller !== Y.Class.fabricate ) {
+ if ( cls.caller !== Class.fabricate ) {
if ( instance.init ){
var result = instance.init.apply(instance, arguments);
if (result) instance = result;
prototype[k] = members[k];
}
- if (prototype.init) NewClass.init = prototype.init;
+ if (prototype.init) NewClass.init = Y(prototype.init);
KNOWN_CLASSES[className] = NewClass;
Class.className = Class.fn.className = "Class";
-Y.Class =
-Y.subclass = Class;
-
/* Class Methods */
/**
/**
* Root-class for all Y-objects.
*/
-function YBase(){}
-YBase = Y.YBase = new Class("YBase", {
+var YBase = new Class("YBase", {
__y__ : true
});
function bindName(v, k){
if ( isFunction(v) )
- this[k] = Y(this[k]).bind(this);
+ this[k] = Y(v).bind(this);
}
// bindName = Y(bindName).curry();
-Y.bindAll = bindAll;
function bindAll(o, names){
var names = new Y(arguments, 1);
Y(names.size() ? names.generate(Y.op.get(o)) : o).forEach( bindName, o );
-
- // if ( names.size() ){
- // names.forEach(binder);
- // } else
- // for (var k in o) binder(k);
-
return o;
}
return o;
}
+exports['Class'] =
+exports['subclass'] = Class;
+exports['instantiate'] = Class.instantiate.bind(Class);
+exports['fabricate'] = Class.fabricate.bind(Class);
+
+exports['YBase'] = YBase;
+exports['bindAll'] = bindAll;
+exports['chainDelegates'] = chainDelegates;
+exports['mixinNames'] = mixinNames;
function forEach(o, fn){
map(o, fn, cxt);
+ return o;
}
function filter(o, fn){
}
function extend( A, B ){
- return slice.call(arguments,1).reduce(extend._extendall, A);
+ return slice.call(arguments,1).reduce(extendall, A);
+}
+function extendall(A, donor){ return reduce(donor, attrvk, A); }
+function attrvk(o, v, k){ return attr(o, k, v, o[k]); }
+
+function dextend( A, B ){
+ return slice.call(arguments,1).reduce(dextendall, A);
}
-extend._extendall = function _extendall(A, donor){
- return reduce(donor, extend._set, A);
-};
-extend._set = function _set(o, v, k){
- return dattr(o, k, v, o[k]);
-};
+function dextendall(A, donor){ return reduce(donor, dattrvk, A); }
+function dattrvk(o, v, k){ return dattr(o, k, v, o[k]); }
+
+
+exports['reduce'] = reduce;
+exports['map'] = map;
+exports['forEach'] = forEach;
+exports['filter'] = filter;
+
+exports['set'] = set;
+exports['attr'] = attr;
+exports['extend'] = extend;
+exports['dset'] = dset;
+exports['dattr'] = dattr;
+exports['dextend'] = dextend;
--- /dev/null
+
+/// Import our external deps first ///
+require('lessly/future');
+require('functional/to-function');
+
+/// Set up core and utilities ///
+var core = require('Y/core')
+, type = require('Y/type')
+, Y = require('Y/y').Y
+;
+
+exports['Y'] = Y;
+
+// Copy all our type utils onto the type function, as it has the
+// same name as its namespace
+core.extend(type.type, type);
+
+// Attach core & type to Y
+core.extend(Y, core, type);
+
+// Make top-level setters refer to the delegating versions
+Y['core'] = core;
+Y['set'] = core.dset;
+Y['attr'] = core.dattr;
+Y['extend'] = core.dextend;
+
+
+/// Patch modules that weren't available earlier ///
+
+// Attach YFunction methods to library functions
+var yfn = require('Y/types/function')
+, YFunction = Y['YFunction'] = yfn.YFunction ;
+
+YFunction(Y);
+core.forEach(core, YFunction);
+YFunction(Y.type);
+Y['is'] = YFunction(Y.is).curry();
+
+addNames('curry methodize genericize compose chain memoize', yfn);
+
+// Curry all operators
+var op = require('Y/op');
+core.forEach(op, YFunction);
+Y['op'] = op.extend({}, core.map(yfn.curry, op));
+// Y['op'] = core.reduce(op, function(Yop, fn, k){
+// Yop[k] = yfn.curry(fn);
+// return Yop;
+// }, {});
+
+// var yclass = require('Y/types/class');
+addNames('Class subclass instantiate fabricate YBase',
+ require('Y/class'));
+
+
+/// Now start assembling the normal sub-modules ///
+addNames('YCollection', require('Y/types/collection'));
+addNames('YArray', require('Y/types/array'));
+addNames('YObject', require('Y/types/object'));
+addNames('YString', require('Y/types/string'));
+addNames('YNumber range', require('Y/types/number'));
+
+// var ycollection = require('Y/types/collection')
+// , yarray = require('Y/types/array')
+// , yobject = require('Y/types/object')
+// , ystring = require('Y/types/string')
+// , ynumber = require('Y/types/number')
+// ;
+
+
+function addName(name){ Y[name] = this[name]; }
+function addNames(names, ns){ names.split(' ').forEach(addName, ns); }
-
/* A subset of the functional operators, used in Y's core */
-Y.op = {
+
+var Y = require('Y').Y
+, core = require('Y/core')
+, op = { // XXX: Make these function statements?
// comparison
cmp: function(x,y){ return x == y ? 0 : (x > y ? 1 : -1); },
has: function(o,k){ return k in o; },
get: function(o,k){ return o[k] },
getdef: function(o,k,def){ return (k in o ? o[k] : def); },
- set: set, // set( o, key, value, def )
- attr: attr, // attr( o, key, value, def )
+ set: core.set, // set( o, key, value, def )
+ attr: core.attr, // attr( o, key, value, def )
method: function(name){
var args = Y(arguments,1);
return function(obj){
};
},
extend : function extend(A,B){
- return slice.call(arguments,1).reduce('A donor -> Y.reduce(donor, Y.op.vkset, A)'.lambda(), A);
+ return slice.call(arguments,1).reduce(extender, A);
}
};
+function extender(target, donor){
+ return core.reduce(donor, op.vkset, target);
+}
-// Curry all operators
-Y.op = Y.reduce(Y.op, function(op, fn, k){
- op[k] = Y( Y(fn).curry() );
- return op;
-}, {});
-
+core.extend(exports, op);
// Type Utilities //
// Much borrowed from jQuery
+var undefined
+, globals = (function(){ return this; })()
+, _Object = globals.Object
+, _Function = globals.Function
+, _Array = globals.Array
+, _String = globals.String
+, _Number = globals.Number
+
+, FN = "constructor"
+, PT = "prototype"
+, OP = _Object[PT]
+
+, slice = _Array[PT].slice
+, getProto = _Object.getPrototypeOf
+, hasOwn = OP.hasOwnProperty
+, toString = OP.toString
+, hasOwn = OP.hasOwnProperty
+
+, KNOWN_CLASSES = {}
+;
+
var class2name =
"Boolean Number String Function Array Date RegExp Object"
.split(" ")
.reduce(function(class2name, name) {
class2name[ "[object "+name+"]" ] = name.toLowerCase();
return class2name;
- }, {})
-;
+ }, {});
function type_of(obj){
return obj == null ?
class2name[ toString.call(obj) ] || "object";
}
+isArray.types = [];
function isArray(obj) { return type_of(obj) === "array" || obj instanceof Y.YArray; }
function isFunction(obj) { return type_of(obj) === "function"; }
function isString(obj) { return type_of(obj) === "string"; }
return false;
// Not own constructor property must be Object
- if ( obj.constructor &&
- !hasOwn.call(obj, "constructor") &&
- !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") )
+ if ( obj[FN] &&
+ !hasOwn.call(obj, FN) &&
+ !hasOwn.call(obj[FN][PT], "isPrototypeOf") )
return false;
function type( o ) {
switch ( typeof(o) ) {
case "undefined" : return undefined;
- case "string" : return String;
- case "number" : return Number; // Note: NaN and Infinity are Number literals
- case "boolean" : return Boolean;
+ case "string" : return _String;
+ case "number" : return _Number; // Note: NaN and Infinity are Number literals
+ case "boolean" : return _Boolean;
case "function" :
// If the function has a user-specified prototype, we can probably assume
// it's meant to be a class constructor (and therefore, a type)
- if ( o.prototype && o.prototype !== Function.prototype )
+ if ( o[PT] && o[PT] !== _Function[PT] )
return o;
else
- return Function;
+ return _Function;
case "object" :
default :
return null;
return KNOWN_CLASSES[o.className] || o.__class__
- || (o.constructor && o.constructor !== Object) ? o.constructor : Object;
+ || (o[FN] && o[FN] !== _Object) ? o[FN] : _Object;
}
}
return (A instanceof BT || B instanceof AT || AT === BT);
}
}
+
+exports['is'] = is;
+exports['type'] = type;
+exports['type_of'] = type_of;
+
+exports['isArray'] = isArray;
+exports['isFunction'] = isFunction;
+exports['isString'] = isString;
+exports['isNumber'] = isNumber;
+exports['isWindow'] = isWindow;
+exports['isPlainObject'] = isPlainObject;
+
+type['KNOWN_CLASSES'] = KNOWN_CLASSES;
+
-var
-YArray =
-Y.YArray =
-YCollection.subclass('YArray', function(YArray){
+var Y = require('Y').Y;
+
+exports['YArray'] =
+Y.YCollection.subclass('YArray', function(YArray){
+ var yclass = require('Y/class');
+
+ yclass.chainDelegates(YArray, 'push', 'unshift', 'sort', 'splice', 'reverse');
+ yclass.mixinNames(YArray, Array, ['reduce', 'map', 'forEach', 'filter', 'slice', 'some', 'every'], true, true);
+ yclass.mixinNames(YArray, Array, ['indexOf', 'lastIndexOf', 'shift', 'pop', 'join'], true, false);
+
+
+
+
this.init = function(o){
// YCollection.init.call(this, o || []);
this._o = o || [];
function concat( donor ){
var A = this._o;
new Y(arguments).forEach(function( donor ){
- A = A.concat(donor instanceof Y.YArray ? donor.end() : donor);
+ A = A.concat(donor instanceof YArray ? donor.end() : donor);
});
return Y(A);
};