make Ox a function, allow for Ox(obj).fn() instead of Ox.fn(obj)

This commit is contained in:
rlx 2011-04-19 19:06:37 +00:00
parent 6784b07d24
commit 0e9432076d
2 changed files with 61 additions and 45 deletions

View file

@ -1,29 +1,8 @@
// todo: check http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ // todo: check http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
/* Ox = function(val) {
(function() { return Ox.wrap(val);
var methods = { };
'array': ['pop', 'push', 'shift', 'unshift'],
'string': [
'charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf',
'match', 'replace', 'search', 'slice', 'split',
'substr', 'substring', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase',
'toString', 'toUpperCase', 'trim', 'trimLeft, 'trimRight',
'valueOf'
]
},
global = this;
return function Ox(val) {
var ret,
type = Ox.typeof(val);
if (type == 'array') {
}
}
})();
*/
Ox = {};
/* /*
================================================================================ ================================================================================
@ -180,6 +159,36 @@ Ox.uid = (function() {
}; };
}()); }());
Ox.wrap = function(val, chained) {
/***
>>> Ox.wrap('foobar').reverse()
'raboof'
>>> Ox.wrap('foobar').chain().reverse().reverse().value()
'foobar'
***/
var wrapper = {
chain: function() {
wrapper.chained = true;
return wrapper;
},
chained: chained || false,
value: function() {
return val;
}
};
Object.getOwnPropertyNames(Ox).forEach(function(name) {
if (name[0] == name[0].toLowerCase() && Ox.isFunction(Ox[name])) {
wrapper[name] = function() {
var args = Array.prototype.slice.call(arguments), ret;
args.unshift(val);
ret = Ox[name].apply(Ox, args);
return wrapper.chained ? Ox.wrap(ret, true) : ret;
}
}
});
return wrapper;
};
/* /*
================================================================================ ================================================================================
Array and Object functions Array and Object functions
@ -194,7 +203,7 @@ Ox.avg = function(obj) {
>>> Ox.avg({a: 1, b: 2, c: 3}) >>> Ox.avg({a: 1, b: 2, c: 3})
2 2
***/ ***/
return Ox.sum(obj) / Ox.length(obj); return Ox.sum(obj) / Ox.len(obj);
}; };
Ox.clone = function(obj) { Ox.clone = function(obj) {
@ -269,7 +278,7 @@ Ox.every = function(obj, fn) {
*/ */
return Ox.filter(Ox.values(obj), fn || function(v) { return Ox.filter(Ox.values(obj), fn || function(v) {
return v; return v;
}).length == Ox.length(obj); }).length == Ox.len(obj);
}; };
Ox.extend = function() { Ox.extend = function() {
@ -395,7 +404,7 @@ Ox.getPositionById = function(arr, id) {
}; };
Ox.isEmpty = function(val) { Ox.isEmpty = function(val) {
return Ox.length(val) == 0; return Ox.len(val) == 0;
}; };
Ox.isEqual = function(obj0, obj1) { Ox.isEqual = function(obj0, obj1) {
@ -441,12 +450,13 @@ Ox.isEqual = function(obj0, obj1) {
Ox.keys = function(obj) { Ox.keys = function(obj) {
/* /*
>>> Ox.keys('123') works for arrays, objects and strings, unlike Object.keys()
[0, 1, 2]
>>> Ox.keys([1, 2, 3]) >>> Ox.keys([1, 2, 3])
[0, 1, 2] [0, 1, 2]
>>> Ox.keys({a: 1, b: 2, c: 3}) >>> Ox.keys({a: 1, b: 2, c: 3})
["a", "b", "c"] ["a", "b", "c"]
>>> Ox.keys('abc')
[0, 1, 2]
>>> Ox.keys([,]) >>> Ox.keys([,])
[0] [0]
*/ */
@ -457,16 +467,18 @@ Ox.keys = function(obj) {
return keys.sort(); return keys.sort();
}; };
Ox.length = function(obj) { Ox.len = function(obj) {
/* /*
>>> Ox.length([1, 2, 3]) >>> Ox.len([1, 2, 3])
3 3
>>> Ox.length({"a": 1, "b": 2, "c": 3}) >>> Ox.len({a: 1, b: 2, c: 3})
3 3
>>> Ox.length([,]) >>> Ox.len('abc')
3
>>> Ox.len([,])
1 1
*/ */
return Ox.isArray(obj) ? obj.length : Ox.values(obj).length; return Ox.isObject(obj) ? Ox.values(obj).length : obj.length;
}; };
Ox.makeArray = function(arg) { Ox.makeArray = function(arg) {
@ -734,12 +746,12 @@ Ox.unserialize = function(str) {
Ox.values = function(obj) { Ox.values = function(obj) {
/* /*
>>> Ox.values('123')
['1', '2', '3']
>>> Ox.values([1, 2, 3]) >>> Ox.values([1, 2, 3])
[1, 2, 3] [1, 2, 3]
>>> Ox.values({a: 1, b: 2, c: 3}) >>> Ox.values({a: 1, b: 2, c: 3})
[1, 2, 3] [1, 2, 3]
>>> Ox.values('abc')
['a', 'b', 'c']
>>> Ox.values([1,]) >>> Ox.values([1,])
[1] [1]
*/ */
@ -1843,10 +1855,11 @@ Geo functions
}; };
Ox.getDegreesPerMeter = function(lat) { Ox.getDegreesPerMeter = function(lat) {
/* /***
>>> Ox.EARTH_CIRCUMFERENCE = 360 / Ox.getDegreesPerMeter(0) return degrees per meter at a given latitude
>>> Ox.EARTH_CIRCUMFERENCE == 360 / Ox.getDegreesPerMeter(0)
true true
*/ ***/
return 360 / Ox.EARTH_CIRCUMFERENCE / Math.cos(lat * Math.PI / 180); return 360 / Ox.EARTH_CIRCUMFERENCE / Math.cos(lat * Math.PI / 180);
}; };
@ -1879,10 +1892,11 @@ Geo functions
}; };
Ox.getMetersPerDegree = function(lat) { Ox.getMetersPerDegree = function(lat) {
/* /***
returns meters per degree at a given latitude
>>> Ox.EARTH_CIRCUMFERENCE == Ox.getMetersPerDegree(0) * 360 >>> Ox.EARTH_CIRCUMFERENCE == Ox.getMetersPerDegree(0) * 360
true true
*/ ***/
return Math.cos(lat * Math.PI / 180) * Ox.EARTH_CIRCUMFERENCE / 360; return Math.cos(lat * Math.PI / 180) * Ox.EARTH_CIRCUMFERENCE / 360;
}; };

View file

@ -561,7 +561,6 @@ requires
event[k] && keys.push(v); event[k] && keys.push(v);
}); });
// avoid pushing modifier twice // avoid pushing modifier twice
//Ox.print('keys', keys)
if (keyNames[event.keyCode] && keys.indexOf(keyNames[event.keyCode]) == -1) { if (keyNames[event.keyCode] && keys.indexOf(keyNames[event.keyCode]) == -1) {
keys.push(keyNames[event.keyCode]); keys.push(keyNames[event.keyCode]);
} }
@ -606,6 +605,7 @@ requires
***/ ***/
var cache = {}, var cache = {},
//dfd = $.Deferred(),
pending = {}, pending = {},
requests = {}, requests = {},
self = { self = {
@ -644,7 +644,7 @@ requires
}, },
requests: function() { requests: function() {
return Ox.length(requests); return Ox.len(requests);
}, },
send: function(options) { send: function(options) {
@ -691,7 +691,7 @@ requires
function callback(data) { function callback(data) {
delete requests[options.id]; delete requests[options.id];
//Ox.length(requests) == 0 && $body.trigger('requestStop'); //Ox.len(requests) == 0 && $body.trigger('requestStop');
options.callback && options.callback(data); options.callback && options.callback(data);
} }
@ -801,6 +801,8 @@ requires
callback(data); callback(data);
} }
// return dfd.promise();
return options.id; return options.id;
} }
@ -6579,7 +6581,7 @@ requires
}); });
*/ */
// fixme: without this, horizontal lists don't get their full width ... weird // fixme: without this, horizontal lists don't get their full width
self.options.orientation == 'horizontal' && that.$content.css({height: '1px'}); self.options.orientation == 'horizontal' && that.$content.css({height: '1px'});
$.extend(self, { $.extend(self, {