updated sugar.js, lodash

This commit is contained in:
kay.one 2013-06-25 22:43:09 -07:00
parent 3b80d59197
commit 67c0e61c16
2 changed files with 65 additions and 22 deletions

View File

@ -3528,13 +3528,21 @@
* @returns {Function} Returns the new debounced function. * @returns {Function} Returns the new debounced function.
* @example * @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(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, * 'leading': true,
* 'trailing': false * '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) { function debounce(func, wait, immediate) {
var args, var args,
@ -3713,9 +3721,11 @@
* @returns {Function} Returns the new throttled function. * @returns {Function} Returns the new throttled function.
* @example * @example
* *
* // avoid excessively updating the position while scrolling
* var throttled = _.throttle(updatePosition, 100); * var throttled = _.throttle(updatePosition, 100);
* jQuery(window).on('scroll', throttled); * 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, { * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
* 'trailing': false * 'trailing': false
* })); * }));

View File

@ -20,7 +20,7 @@
var internalToString = object.prototype.toString; var internalToString = object.prototype.toString;
// Internal hasOwnProperty // Internal hasOwnProperty
var hasOwnProperty = object.hasOwnProperty; var internalHasOwnProperty = object.prototype.hasOwnProperty;
// The global context // The global context
var globalContext = typeof global !== 'undefined' ? global : this; var globalContext = typeof global !== 'undefined' ? global : this;
@ -103,7 +103,7 @@
initializeClass(klass); initializeClass(klass);
iterateOverObject(methods, function(name, method) { iterateOverObject(methods, function(name, method) {
var original = extendee[name]; var original = extendee[name];
var existed = hasOwnProperty.call(extendee, name); var existed = hasOwnProperty(extendee, name);
if(typeof override === 'function') { if(typeof override === 'function') {
method = wrapNative(extendee[name], method, override); method = wrapNative(extendee[name], method, override);
} }
@ -191,9 +191,13 @@
// Object helpers // Object helpers
function hasOwnProperty(obj, prop) {
return !!obj && internalHasOwnProperty.call(obj, prop);
}
function isObjectPrimitive(obj) { function isObjectPrimitive(obj) {
// Check for null // Check for null
return obj && typeof obj === 'object'; return !!obj && typeof obj === 'object';
} }
function isPlainObject(obj, klass) { function isPlainObject(obj, klass) {
@ -202,8 +206,8 @@
// Not own constructor property must be Object // Not own constructor property must be Object
// This code was borrowed from jQuery.isPlainObject // This code was borrowed from jQuery.isPlainObject
if (obj.constructor && if (obj.constructor &&
!hasOwnProperty.call(obj, 'constructor') && !hasOwnProperty(obj, 'constructor') &&
!hasOwnProperty.call(obj.constructor.prototype, 'isPrototypeOf')) { !hasOwnProperty(obj.constructor.prototype, 'isPrototypeOf')) {
return false; return false;
} }
} catch (e) { } catch (e) {
@ -219,7 +223,7 @@
function iterateOverObject(obj, fn) { function iterateOverObject(obj, fn) {
var key; var key;
for(key in obj) { 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; if(fn.call(obj, key, obj[key], obj) === false) break;
} }
} }
@ -1064,6 +1068,10 @@
return result; return result;
} }
function isArrayLike(obj) {
return hasOwnProperty(obj, 'length') && !isArray(obj) && !isString(obj) && !isPlainObject(obj);
}
function flatArguments(args) { function flatArguments(args) {
var result = []; var result = [];
multiArgs(args, function(arg) { multiArgs(args, function(arg) {
@ -1242,15 +1250,10 @@
* *
***/ ***/
'create': function() { 'create': function() {
var result = [], tmp; var result = [];
multiArgs(arguments, function(a) { multiArgs(arguments, function(a) {
if(isObjectPrimitive(a)) { if(isArrayLike(a)) {
try { a = array.prototype.slice.call(a, 0);
tmp = array.prototype.slice.call(a, 0);
if(tmp.length > 0) {
a = tmp;
}
} catch(e) {};
} }
result = result.concat(a); result = result.concat(a);
}); });
@ -3953,6 +3956,36 @@
return getWeekNumber(this); 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]) * @method getUTCOffset([iso])
* @returns String * @returns String
@ -5551,7 +5584,7 @@
allKeys.forEach(function(k) { allKeys.forEach(function(k) {
paramIsArray = !k || k.match(/^\d+$/); paramIsArray = !k || k.match(/^\d+$/);
if(!key && isArray(obj)) key = obj.length; if(!key && isArray(obj)) key = obj.length;
if(!hasOwnProperty.call(obj, key)) { if(!hasOwnProperty(obj, key)) {
obj[key] = paramIsArray ? [] : {}; obj[key] = paramIsArray ? [] : {};
} }
obj = obj[key]; obj = obj[key];
@ -5598,7 +5631,7 @@
if(isRegExp(match)) { if(isRegExp(match)) {
return match.test(key); return match.test(key);
} else if(isObjectPrimitive(match)) { } else if(isObjectPrimitive(match)) {
return hasOwnProperty.call(match, key); return hasOwnProperty(match, key);
} else { } else {
return key === string(match); return key === string(match);
} }
@ -5788,7 +5821,7 @@
// their properties not being enumerable in < IE8. // their properties not being enumerable in < IE8.
if(target && typeof source != 'string') { if(target && typeof source != 'string') {
for(key in source) { for(key in source) {
if(!hasOwnProperty.call(source, key) || !target) continue; if(!hasOwnProperty(source, key) || !target) continue;
val = source[key]; val = source[key];
// Conflict! // Conflict!
if(isDefined(target[key])) { if(isDefined(target[key])) {
@ -5953,7 +5986,7 @@
* *
***/ ***/
'has': function (obj, key) { '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 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; var str = runReplacements(this, humans), acronym;
str = str.replace(/_id$/g, ''); str = str.replace(/_id$/g, '');
str = str.replace(/(_)?([a-z\d]*)/gi, function(match, _, word){ 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 (_ ? ' ' : '') + (acronym || word.toLowerCase());
}); });
return capitalize(str); return capitalize(str);