Cleanup custom Underscore.js

This commit is contained in:
djmaze 2020-07-23 20:50:20 +02:00
parent 134cb7d52a
commit 281a5df732
3 changed files with 64 additions and 108 deletions

View file

@ -52,10 +52,11 @@ This fork has the following changes:
* Ongoing removal of jQuery and Underscore.js dependencies (things are native these days) * Ongoing removal of jQuery and Underscore.js dependencies (things are native these days)
### slim jQuery and Underscore.js ### slim jQuery and Underscore.js
js: 1.14.0 = 7960367 / native = 5127981 bytes js: 1.14.0 = 7960367 / native = 5131640 bytes
js/min: 1.14.0 = 1766594 / native = 1405850 bytes
360.744 bytes is nut much, but it already feels faster. js/min: 1.14.0 = 1766594 / native = 1405919 bytes
360.675 bytes is not much, but it feels faster.
### PHP73 branch ### PHP73 branch

View file

@ -3,4 +3,4 @@
// (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license. // Underscore may be freely distributed under the MIT license.
(()=>{var e=function(t){return t instanceof e?t:this instanceof e?void 0:new e(t)};"undefined"==typeof exports||exports.nodeType?("object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this||{})._=e:("undefined"!=typeof module&&!module.nodeType&&module.exports&&(exports=module.exports=e),exports._=e);e.VERSION="1.9.2";var t=function(e,t,n,o,l){if(!(o instanceof t))return e.apply(n,l);var r,u="object"!=typeof(r=e.prototype)?{}:Object.create(r),i=e.apply(u,l);return"object"==typeof i?i:u};e.defer=(()=>{var e=(e,t,...n)=>setTimeout(()=>e.apply(null,n),t),n=function(...o){for(var l=0,r=[o[l++],1];l<o.length;)r.push(o[l++]);return t(e,n,this,this,r)};return n})(),e.throttle=function(e,t,n){var o,l,r,u,i=0;n||(n={});var a=function(){i=!1===n.leading?0:Date.now(),o=null,u=e.apply(l,r),o||(l=r=null)},f=function(){var f=Date.now();i||!1!==n.leading||(i=f);var p=t-(f-i);return l=this,r=arguments,p<=0||p>t?(o&&(clearTimeout(o),o=null),i=f,u=e.apply(l,r),o||(l=r=null)):o||!1===n.trailing||(o=setTimeout(a,p)),u};return f.cancel=function(){clearTimeout(o),i=0,o=l=r=null},f},e.debounce=function(e,t,n){var o,l,r=function(t,n){o=null,n&&(l=e.apply(t,n))},u=function(...u){if(o&&clearTimeout(o),n){var i=!o;o=setTimeout(r,t),i&&(l=e.apply(this,u))}else{var a=this;o=setTimeout(()=>r.apply(null,[a,u]),t)}return l};return u.cancel=function(){clearTimeout(o),o=null},u},"function"==typeof define&&define.amd&&define("underscore",[],function(){return e})})(); (()=>{var e=function(){};("object"==typeof self&&self.self===self&&self||{})._=e;e.defer=(()=>{var e=(e,t,...l)=>setTimeout(()=>e.apply(null,l),t),t=function(...l){for(var n=1,r=[l[0],1];n<l.length;)r.push(l[n++]);return function(e,t,l,n,r){if(!(n instanceof t))return e.apply(l,r);var u=e.prototype,a="object"!=typeof u?{}:Object.create(u),o=e.apply(a,r);return"object"==typeof o?o:a}(e,t,this,this,r)};return t})(),e.throttle=function(e,t,l){l||(l={});var n,r,u,a,o=0,i=function(){o=!1===l.leading?0:Date.now(),n=null,a=e.apply(r,u),n||(r=u=null)},c=(...u)=>{var c=Date.now();o||!1!==l.leading||(o=c);var p=t-(c-o);return r=this,p<=0||p>t?(n&&(clearTimeout(n),n=null),o=c,a=e.apply(r,u),n||(r=u=null)):n||!1===l.trailing||(n=setTimeout(i,p)),a};return c.cancel=(()=>{clearTimeout(n),o=0,n=r=u=null}),c},e.debounce=function(e,t,l){var n,r,u=(t,l)=>{n=null,l&&(r=e.apply(t,l))},a=function(...a){if(n&&clearTimeout(n),l){var o=!n;n=setTimeout(u,t),o&&(r=e.apply(this,a))}else{var i=this;n=setTimeout(()=>u.apply(null,[i,a]),t)}return r};return a.cancel=(()=>{clearTimeout(n),n=null}),a}})();

View file

@ -14,40 +14,11 @@ _.throttle
// -------------- // --------------
// Create a safe reference to the Underscore object for use below. // Create a safe reference to the Underscore object for use below.
var _ = function(obj) { var _ = function() { };
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
};
// Export the Underscore object for **Node.js**, with // Establish the root object, `window` (`self`) in the browser.
// backwards-compatibility for their old module API. If we're in // We use `self`instead of `window` for `WebWorker` support.
// the browser, add `_` as a global object. (typeof self == 'object' && self.self === self && self || {})._ = _;
// (`nodeType` is checked to ensure that `module`
// and `exports` are not HTML elements.)
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
// Establish the root object, `window` (`self`) in the browser, `global`
// on the server, or `this` in some virtual machines. We use `self`
// instead of `window` for `WebWorker` support.
var root = typeof self == 'object' && self.self === self && self ||
typeof global == 'object' && global.global === global && global ||
this ||
{};
root._ = _;
}
// Current version.
_.VERSION = '1.9.2';
// An internal function for creating a new object that inherits from another.
var baseCreate = function(prototype) {
if (typeof prototype !== 'object') return {};
return Object.create(prototype);
};
// Function (ahem) Functions // Function (ahem) Functions
// ------------------ // ------------------
@ -56,21 +27,22 @@ _.throttle
// or a normal function with the provided arguments. // or a normal function with the provided arguments.
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) { var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args); if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype); var fp = sourceFunc.prototype,
var result = sourceFunc.apply(self, args); self = (typeof fp !== 'object') ? {} : Object.create(fp),
result = sourceFunc.apply(self, args);
return (typeof result === 'object') ? result : self; return (typeof result === 'object') ? result : self;
}; };
// Defers a function, scheduling it to run after the current call stack has // Defers a function, scheduling it to run after the current call stack has
// cleared. // cleared.
_.defer = (() => { _.defer = (() => {
var func = (func, wait, ...args) => setTimeout(() => func.apply(null, args), wait); var func = (func, wait, ...args) => setTimeout(() => func.apply(null, args), wait),
var bound = function(...params) { bound = function(...params) {
var position = 0; var pos = 1,
var args = [params[position++], 1]; args = [params[0], 1];
while (position < params.length) args.push(params[position++]); while (pos < params.length) args.push(params[pos++]);
return executeBound(func, bound, this, this, args); return executeBound(func, bound, this, this, args);
}; };
return bound; return bound;
})(); })();
@ -80,44 +52,41 @@ _.throttle
// but if you'd like to disable the execution on the leading edge, pass // but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto. // `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) { _.throttle = function(func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {}; if (!options) options = {};
var timeout, context, args, result,
var later = function() { previous = 0,
previous = options.leading === false ? 0 : Date.now(); later = function() {
timeout = null; previous = options.leading === false ? 0 : Date.now();
result = func.apply(context, args); timeout = null;
if (!timeout) context = args = null;
};
var throttled = function() {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args); result = func.apply(context, args);
if (!timeout) context = args = null; if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) { },
timeout = setTimeout(later, remaining); throttle = (...args) => {
} var now = Date.now();
return result; if (!previous && options.leading === false) previous = now;
}; var remaining = wait - (now - previous);
context = this;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function() { throttle.cancel = () => {
clearTimeout(timeout); clearTimeout(timeout);
previous = 0; previous = 0;
timeout = context = args = null; timeout = context = args = null;
}; };
return throttled; return throttle;
}; };
// Returns a function, that, as long as it continues to be invoked, will not // Returns a function, that, as long as it continues to be invoked, will not
@ -125,45 +94,31 @@ _.throttle
// N milliseconds. If `immediate` is passed, trigger the function on the // N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing. // leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) { _.debounce = function(func, wait, immediate) {
var timeout, result; var timeout, result,
later = (context, args) => {
timeout = null;
if (args) result = func.apply(context, args);
},
debounce = function(...args) {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} else {
var obj = this;
timeout = setTimeout(() => later.apply(null, [obj, args]), wait);
}
var later = function(context, args) { return result;
timeout = null; };
if (args) result = func.apply(context, args);
};
var debounced = function(...args) { debounce.cancel = () => {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} else {
var obj = this;
timeout = setTimeout(() => later.apply(null, [obj, args]), wait);
}
return result;
};
debounced.cancel = function() {
clearTimeout(timeout); clearTimeout(timeout);
timeout = null; timeout = null;
}; };
return debounced; return debounce;
}; };
// AMD registration happens at the end for compatibility with AMD loaders
// that may not enforce next-turn semantics on modules. Even though general
// practice for AMD registration is to be anonymous, underscore registers
// as a named module because, like jQuery, it is a base library that is
// popular enough to be bundled in a third party lib, but not be part of
// an AMD load request. Those cases could generate an error when an
// anonymous define() is called outside of a loader request.
if (typeof define == 'function' && define.amd) {
define('underscore', [], function() {
return _;
});
}
})(); })();