From 281a5df732de49d03e668346da89a8d5a6203fc4 Mon Sep 17 00:00:00 2001 From: djmaze Date: Thu, 23 Jul 2020 20:50:20 +0200 Subject: [PATCH] Cleanup custom Underscore.js --- README.md | 7 +- vendors/underscore/underscore-min.custom.js | 2 +- vendors/underscore/underscore.custom.js | 163 +++++++------------- 3 files changed, 64 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index 91de5b9cf..0555273fb 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,11 @@ This fork has the following changes: * Ongoing removal of jQuery and Underscore.js dependencies (things are native these days) ### slim jQuery and Underscore.js -js: 1.14.0 = 7960367 / native = 5127981 bytes -js/min: 1.14.0 = 1766594 / native = 1405850 bytes +js: 1.14.0 = 7960367 / native = 5131640 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 diff --git a/vendors/underscore/underscore-min.custom.js b/vendors/underscore/underscore-min.custom.js index 03ee65fa4..6a54ee2d5 100644 --- a/vendors/underscore/underscore-min.custom.js +++ b/vendors/underscore/underscore-min.custom.js @@ -3,4 +3,4 @@ // (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // 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];lt?(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{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}})(); diff --git a/vendors/underscore/underscore.custom.js b/vendors/underscore/underscore.custom.js index 287e2df09..7faaf9bba 100644 --- a/vendors/underscore/underscore.custom.js +++ b/vendors/underscore/underscore.custom.js @@ -14,40 +14,11 @@ _.throttle // -------------- // Create a safe reference to the Underscore object for use below. - var _ = function(obj) { - if (obj instanceof _) return obj; - if (!(this instanceof _)) return new _(obj); - }; + var _ = function() { }; - // Export the Underscore object for **Node.js**, with - // backwards-compatibility for their old module API. If we're in - // the browser, add `_` as a global object. - // (`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); - }; + // Establish the root object, `window` (`self`) in the browser. + // We use `self`instead of `window` for `WebWorker` support. + (typeof self == 'object' && self.self === self && self || {})._ = _; // Function (ahem) Functions // ------------------ @@ -56,21 +27,22 @@ _.throttle // or a normal function with the provided arguments. var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) { if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args); - var self = baseCreate(sourceFunc.prototype); - var result = sourceFunc.apply(self, args); + var fp = sourceFunc.prototype, + self = (typeof fp !== 'object') ? {} : Object.create(fp), + result = sourceFunc.apply(self, args); return (typeof result === 'object') ? result : self; }; // Defers a function, scheduling it to run after the current call stack has // cleared. _.defer = (() => { - var func = (func, wait, ...args) => setTimeout(() => func.apply(null, args), wait); - var bound = function(...params) { - var position = 0; - var args = [params[position++], 1]; - while (position < params.length) args.push(params[position++]); - return executeBound(func, bound, this, this, args); - }; + var func = (func, wait, ...args) => setTimeout(() => func.apply(null, args), wait), + bound = function(...params) { + var pos = 1, + args = [params[0], 1]; + while (pos < params.length) args.push(params[pos++]); + return executeBound(func, bound, this, this, args); + }; return bound; })(); @@ -80,44 +52,41 @@ _.throttle // but if you'd like to disable the execution on the leading edge, pass // `{leading: false}`. To disable execution on the trailing edge, ditto. _.throttle = function(func, wait, options) { - var timeout, context, args, result; - var previous = 0; if (!options) options = {}; - - var later = function() { - previous = options.leading === false ? 0 : Date.now(); - timeout = null; - result = func.apply(context, args); - 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; + var timeout, context, args, result, + previous = 0, + later = function() { + previous = options.leading === false ? 0 : Date.now(); + timeout = null; result = func.apply(context, args); if (!timeout) context = args = null; - } else if (!timeout && options.trailing !== false) { - timeout = setTimeout(later, remaining); - } - return result; - }; + }, + throttle = (...args) => { + var now = Date.now(); + 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); previous = 0; timeout = context = args = null; }; - return throttled; + return throttle; }; // 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 // leading edge, instead of the trailing. _.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) { - timeout = null; - if (args) result = func.apply(context, args); - }; + return result; + }; - var debounced = 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); - } - - return result; - }; - - debounced.cancel = function() { + debounce.cancel = () => { clearTimeout(timeout); 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 _; - }); - } })();