From 67c0e61c1656c0c51c2f218c5b70bfa11dea1bce Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 25 Jun 2013 22:43:09 -0700 Subject: [PATCH] updated sugar.js, lodash --- UI/JsLibraries/lodash.underscore.js | 14 +++++- UI/JsLibraries/sugar.js | 73 +++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/UI/JsLibraries/lodash.underscore.js b/UI/JsLibraries/lodash.underscore.js index 5662358f6..e2df3e4f0 100644 --- a/UI/JsLibraries/lodash.underscore.js +++ b/UI/JsLibraries/lodash.underscore.js @@ -3528,13 +3528,21 @@ * @returns {Function} Returns the new debounced function. * @example * - * var lazyLayout = _.debounce(calculateLayout, 300); + * // avoid costly calculations while the window size is in flux + * var lazyLayout = _.debounce(calculateLayout, 150); * jQuery(window).on('resize', lazyLayout); * - * jQuery('#postbox').on('click', _.debounce(sendMail, 200, { + * // execute `sendMail` when the click event is fired, debouncing subsequent calls + * jQuery('#postbox').on('click', _.debounce(sendMail, 300, { * 'leading': true, * 'trailing': false * }); + * + * // ensure `batchLog` is executed once after 1 second of debounced calls + * var source = new EventSource('/stream'); + * source.addEventListener('message', _.debounce(batchLog, 250, { + * 'maxWait': 1000 + * }, false); */ function debounce(func, wait, immediate) { var args, @@ -3713,9 +3721,11 @@ * @returns {Function} Returns the new throttled function. * @example * + * // avoid excessively updating the position while scrolling * var throttled = _.throttle(updatePosition, 100); * jQuery(window).on('scroll', throttled); * + * // execute `renewToken` when the click event is fired, but not more than once every 5 minutes * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, { * 'trailing': false * })); diff --git a/UI/JsLibraries/sugar.js b/UI/JsLibraries/sugar.js index 5d44832c2..0e9dd46bd 100644 --- a/UI/JsLibraries/sugar.js +++ b/UI/JsLibraries/sugar.js @@ -20,7 +20,7 @@ var internalToString = object.prototype.toString; // Internal hasOwnProperty - var hasOwnProperty = object.hasOwnProperty; + var internalHasOwnProperty = object.prototype.hasOwnProperty; // The global context var globalContext = typeof global !== 'undefined' ? global : this; @@ -103,7 +103,7 @@ initializeClass(klass); iterateOverObject(methods, function(name, method) { var original = extendee[name]; - var existed = hasOwnProperty.call(extendee, name); + var existed = hasOwnProperty(extendee, name); if(typeof override === 'function') { method = wrapNative(extendee[name], method, override); } @@ -191,9 +191,13 @@ // Object helpers + function hasOwnProperty(obj, prop) { + return !!obj && internalHasOwnProperty.call(obj, prop); + } + function isObjectPrimitive(obj) { // Check for null - return obj && typeof obj === 'object'; + return !!obj && typeof obj === 'object'; } function isPlainObject(obj, klass) { @@ -202,8 +206,8 @@ // Not own constructor property must be Object // This code was borrowed from jQuery.isPlainObject if (obj.constructor && - !hasOwnProperty.call(obj, 'constructor') && - !hasOwnProperty.call(obj.constructor.prototype, 'isPrototypeOf')) { + !hasOwnProperty(obj, 'constructor') && + !hasOwnProperty(obj.constructor.prototype, 'isPrototypeOf')) { return false; } } catch (e) { @@ -219,7 +223,7 @@ function iterateOverObject(obj, fn) { var key; for(key in obj) { - if(!hasOwnProperty.call(obj, key)) continue; + if(!hasOwnProperty(obj, key)) continue; if(fn.call(obj, key, obj[key], obj) === false) break; } } @@ -1064,6 +1068,10 @@ return result; } + function isArrayLike(obj) { + return hasOwnProperty(obj, 'length') && !isArray(obj) && !isString(obj) && !isPlainObject(obj); + } + function flatArguments(args) { var result = []; multiArgs(args, function(arg) { @@ -1242,15 +1250,10 @@ * ***/ 'create': function() { - var result = [], tmp; + var result = []; multiArgs(arguments, function(a) { - if(isObjectPrimitive(a)) { - try { - tmp = array.prototype.slice.call(a, 0); - if(tmp.length > 0) { - a = tmp; - } - } catch(e) {}; + if(isArrayLike(a)) { + a = array.prototype.slice.call(a, 0); } result = result.concat(a); }); @@ -3953,6 +3956,36 @@ return getWeekNumber(this); }, + /*** + * @method beginningOfISOWeek() + * @returns Date + * @short Set the date to the beginning of week as defined by this ISO-8601 standard. + * @extra Note that this standard places Monday at the start of the week. + */ + 'beginningOfISOWeek': function() { + var day = this.getDay(); + if(day === 0) { + day = -6; + } else if(day !== 1) { + day = 1; + } + this.setWeekday(day); + return this.reset(); + }, + + /*** + * @method endOfISOWeek() + * @returns Date + * @short Set the date to the end of week as defined by this ISO-8601 standard. + * @extra Note that this standard places Sunday at the end of the week. + */ + 'endOfISOWeek': function() { + if(this.getDay() !== 0) { + this.setWeekday(7); + } + return this.endOfDay() + }, + /*** * @method getUTCOffset([iso]) * @returns String @@ -5551,7 +5584,7 @@ allKeys.forEach(function(k) { paramIsArray = !k || k.match(/^\d+$/); if(!key && isArray(obj)) key = obj.length; - if(!hasOwnProperty.call(obj, key)) { + if(!hasOwnProperty(obj, key)) { obj[key] = paramIsArray ? [] : {}; } obj = obj[key]; @@ -5598,7 +5631,7 @@ if(isRegExp(match)) { return match.test(key); } else if(isObjectPrimitive(match)) { - return hasOwnProperty.call(match, key); + return hasOwnProperty(match, key); } else { return key === string(match); } @@ -5788,7 +5821,7 @@ // their properties not being enumerable in < IE8. if(target && typeof source != 'string') { for(key in source) { - if(!hasOwnProperty.call(source, key) || !target) continue; + if(!hasOwnProperty(source, key) || !target) continue; val = source[key]; // Conflict! if(isDefined(target[key])) { @@ -5953,7 +5986,7 @@ * ***/ 'has': function (obj, key) { - return hasOwnProperty.call(obj, key); + return hasOwnProperty(obj, key); }, /*** @@ -6976,7 +7009,7 @@ } }); return this.replace(/\{([^{]+?)\}/g, function(m, key) { - return hasOwnProperty.call(assign, key) ? assign[key] : m; + return hasOwnProperty(assign, key) ? assign[key] : m; }); } @@ -7345,7 +7378,7 @@ var str = runReplacements(this, humans), acronym; str = str.replace(/_id$/g, ''); str = str.replace(/(_)?([a-z\d]*)/gi, function(match, _, word){ - acronym = hasOwnProperty.call(acronyms, word) ? acronyms[word] : null; + acronym = hasOwnProperty(acronyms, word) ? acronyms[word] : null; return (_ ? ' ' : '') + (acronym || word.toLowerCase()); }); return capitalize(str);