123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- goog.provide('goog.functions');
- goog.functions.constant = function(retValue) {
- return function() { return retValue; };
- };
- goog.functions.FALSE = goog.functions.constant(false);
- goog.functions.TRUE = goog.functions.constant(true);
- goog.functions.NULL = goog.functions.constant(null);
- goog.functions.identity = function(opt_returnValue, var_args) {
- return opt_returnValue;
- };
- goog.functions.error = function(message) {
- return function() { throw Error(message); };
- };
- goog.functions.fail = function(err) {
- return function() { throw err; };
- };
- goog.functions.lock = function(f, opt_numArgs) {
- opt_numArgs = opt_numArgs || 0;
- return function() {
- return f.apply(this, Array.prototype.slice.call(arguments, 0, opt_numArgs));
- };
- };
- goog.functions.nth = function(n) {
- return function() { return arguments[n]; };
- };
- goog.functions.partialRight = function(fn, var_args) {
- var rightArgs = Array.prototype.slice.call(arguments, 1);
- return function() {
- var newArgs = Array.prototype.slice.call(arguments);
- newArgs.push.apply(newArgs, rightArgs);
- return fn.apply(this, newArgs);
- };
- };
- goog.functions.withReturnValue = function(f, retValue) {
- return goog.functions.sequence(f, goog.functions.constant(retValue));
- };
- goog.functions.equalTo = function(value, opt_useLooseComparison) {
- return function(other) {
- return opt_useLooseComparison ? (value == other) : (value === other);
- };
- };
- goog.functions.compose = function(fn, var_args) {
- var functions = arguments;
- var length = functions.length;
- return function() {
- var result;
- if (length) {
- result = functions[length - 1].apply(this, arguments);
- }
- for (var i = length - 2; i >= 0; i--) {
- result = functions[i].call(this, result);
- }
- return result;
- };
- };
- goog.functions.sequence = function(var_args) {
- var functions = arguments;
- var length = functions.length;
- return function() {
- var result;
- for (var i = 0; i < length; i++) {
- result = functions[i].apply(this, arguments);
- }
- return result;
- };
- };
- goog.functions.and = function(var_args) {
- var functions = arguments;
- var length = functions.length;
- return function() {
- for (var i = 0; i < length; i++) {
- if (!functions[i].apply(this, arguments)) {
- return false;
- }
- }
- return true;
- };
- };
- goog.functions.or = function(var_args) {
- var functions = arguments;
- var length = functions.length;
- return function() {
- for (var i = 0; i < length; i++) {
- if (functions[i].apply(this, arguments)) {
- return true;
- }
- }
- return false;
- };
- };
- goog.functions.not = function(f) {
- return function() { return !f.apply(this, arguments); };
- };
- goog.functions.create = function(constructor, var_args) {
-
- var temp = function() {};
- temp.prototype = constructor.prototype;
- // obj will have constructor's prototype in its chain and
- // 'obj instanceof constructor' will be true.
- var obj = new temp();
- // obj is initialized by constructor.
- // arguments is only array-like so lacks shift(), but can be used with
- // the Array prototype function.
- constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
- return obj;
- };
- /**
- * @define {boolean} Whether the return value cache should be used.
- * This should only be used to disable caches when testing.
- */
- goog.define('goog.functions.CACHE_RETURN_VALUE', true);
- /**
- * Gives a wrapper function that caches the return value of a parameterless
- * function when first called.
- *
- * When called for the first time, the given function is called and its
- * return value is cached (thus this is only appropriate for idempotent
- * functions). Subsequent calls will return the cached return value. This
- * allows the evaluation of expensive functions to be delayed until first used.
- *
- * To cache the return values of functions with parameters, see goog.memoize.
- *
- * @param {function():T} fn A function to lazily evaluate.
- * @return {function():T} A wrapped version the function.
- * @template T
- */
- goog.functions.cacheReturnValue = function(fn) {
- var called = false;
- var value;
- return function() {
- if (!goog.functions.CACHE_RETURN_VALUE) {
- return fn();
- }
- if (!called) {
- value = fn();
- called = true;
- }
- return value;
- };
- };
- goog.functions.once = function(f) {
-
-
- var inner = f;
- return function() {
- if (inner) {
- var tmp = inner;
- inner = null;
- tmp();
- }
- };
- };
- goog.functions.debounce = function(f, interval, opt_scope) {
- var timeout = 0;
- return (function(var_args) {
- goog.global.clearTimeout(timeout);
- var args = arguments;
- timeout = goog.global.setTimeout(function() {
- f.apply(opt_scope, args);
- }, interval);
- });
- };
- goog.functions.throttle = function(f, interval, opt_scope) {
- var timeout = 0;
- var shouldFire = false;
- var args = [];
- var handleTimeout = function() {
- timeout = 0;
- if (shouldFire) {
- shouldFire = false;
- fire();
- }
- };
- var fire = function() {
- timeout = goog.global.setTimeout(handleTimeout, interval);
- f.apply(opt_scope, args);
- };
- return (function(var_args) {
- args = arguments;
- if (!timeout) {
- fire();
- } else {
- shouldFire = true;
- }
- });
- };
- goog.functions.rateLimit = function(f, interval, opt_scope) {
- var timeout = 0;
- var handleTimeout = function() {
- timeout = 0;
- };
- return (function(var_args) {
- if (!timeout) {
- timeout = goog.global.setTimeout(handleTimeout, interval);
- f.apply(opt_scope, arguments);
- }
- });
- };
|