oxjs/source/Ox/js/Function.js

71 lines
2 KiB
JavaScript
Raw Normal View History

'use strict';
2012-01-07 07:20:02 +00:00
/*@
Ox.cache <f> Memoize a function
<script>
Ox.test.fn = Ox.cache(function(n) { return n * Math.random(); });
2012-01-07 07:20:02 +00:00
</script>
> Ox.test.fn(10) == Ox.test.fn(10);
2012-01-07 07:20:02 +00:00
true
> Ox.test.fn(10) == Ox.test.fn.clear()(10);
2012-01-07 07:20:02 +00:00
false
@*/
Ox.cache = function(fn, options) {
options = Ox.extend({
async: false,
key: JSON.stringify
}, options || {})
2012-01-07 07:20:02 +00:00
var cache = {},
ret = function() {
var args = Ox.toArray(arguments),
callback,
key = options.key(args);
function callback() {
// cache all arguments passed to callback
cache[key] = Ox.toArray(arguments);
// call the original callback
Ox.last(args).apply(this, arguments);
}
if (options.async) {
if (!(key in cache)) {
// call function with patched callback
2012-05-24 07:45:33 +00:00
fn.apply(this, args.slice(0, -1).concat(callback));
} else {
// call callback with cached arguments
callback.apply(this, cache[key])
}
} else {
if (!(key in cache)) {
cache[key] = fn.apply(this, args);
}
return cache[key];
}
2012-01-07 07:20:02 +00:00
};
ret.clear = function() {
if (arguments.length == 0) {
cache = {};
} else {
Ox.makeArray(arguments).forEach(function(key) {
2012-01-07 07:20:02 +00:00
delete cache[key];
});
}
return ret;
}
return ret;
};
2012-04-08 12:19:53 +00:00
2012-05-24 16:20:22 +00:00
/*@
Ox.identity <f> Returns its first argument
This can be used as a default iterator
@*/
2012-05-19 08:09:48 +00:00
Ox.identity = function(val) {
return val;
};
2012-04-08 12:19:53 +00:00
/*@
2012-05-24 16:20:22 +00:00
Ox.noop <f> Returns undefined and calls optional callback without arguments
This can be used to combine a synchronous and an asynchronous code path.
2012-04-08 12:19:53 +00:00
@*/
2012-05-24 16:20:22 +00:00
// IE 8 doesn't like `Ox.void`
Ox.noop = function(callback) {
2012-04-08 12:19:53 +00:00
Ox.isFunction(callback) && callback();
};