|
| 1 | +// Taken from Docsy PR https://github.com/google/docsy/pull/506 |
| 2 | +// Derived from https://unpkg.com/lodash.debounce@4.0.8/index.js |
| 3 | +/** |
| 4 | + * lodash (Custom Build) <https://lodash.com/> |
| 5 | + * Build: `lodash modularize exports="npm" -o ./` |
| 6 | + * Copyright jQuery Foundation and other contributors <https://jquery.org/> |
| 7 | + * Released under MIT license <https://lodash.com/license> |
| 8 | + * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> |
| 9 | + * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors |
| 10 | + */ |
| 11 | + |
| 12 | +/** Used as the `TypeError` message for "Functions" methods. */ |
| 13 | +var FUNC_ERROR_TEXT = 'Expected a function'; |
| 14 | + |
| 15 | +/** Used as references for various `Number` constants. */ |
| 16 | +var NAN = 0 / 0; |
| 17 | + |
| 18 | +/** `Object#toString` result references. */ |
| 19 | +var symbolTag = '[object Symbol]'; |
| 20 | + |
| 21 | +/** Used to match leading and trailing whitespace. */ |
| 22 | +var reTrim = /^\s+|\s+$/g; |
| 23 | + |
| 24 | +/** Used to detect bad signed hexadecimal string values. */ |
| 25 | +var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; |
| 26 | + |
| 27 | +/** Used to detect binary string values. */ |
| 28 | +var reIsBinary = /^0b[01]+$/i; |
| 29 | + |
| 30 | +/** Used to detect octal string values. */ |
| 31 | +var reIsOctal = /^0o[0-7]+$/i; |
| 32 | + |
| 33 | +/** Built-in method references without a dependency on `root`. */ |
| 34 | +var freeParseInt = parseInt; |
| 35 | + |
| 36 | +/** Detect free variable `global` from Node.js. */ |
| 37 | +var freeGlobal = typeof global == 'object' && global && global.Object === Object && global; |
| 38 | + |
| 39 | +/** Detect free variable `self`. */ |
| 40 | +var freeSelf = typeof self == 'object' && self && self.Object === Object && self; |
| 41 | + |
| 42 | +/** Used as a reference to the global object. */ |
| 43 | +var root = freeGlobal || freeSelf || Function('return this')(); |
| 44 | + |
| 45 | +/** Used for built-in method references. */ |
| 46 | +var objectProto = Object.prototype; |
| 47 | + |
| 48 | +/** |
| 49 | + * Used to resolve the |
| 50 | + * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) |
| 51 | + * of values. |
| 52 | + */ |
| 53 | +var objectToString = objectProto.toString; |
| 54 | + |
| 55 | +/* Built-in method references for those with the same name as other `lodash` methods. */ |
| 56 | +var nativeMax = Math.max, |
| 57 | + nativeMin = Math.min; |
| 58 | + |
| 59 | +/** |
| 60 | + * Gets the timestamp of the number of milliseconds that have elapsed since |
| 61 | + * the Unix epoch (1 January 1970 00:00:00 UTC). |
| 62 | + * |
| 63 | + * @static |
| 64 | + * @memberOf _ |
| 65 | + * @since 2.4.0 |
| 66 | + * @category Date |
| 67 | + * @returns {number} Returns the timestamp. |
| 68 | + * @example |
| 69 | + * |
| 70 | + * _.defer(function(stamp) { |
| 71 | + * console.log(_.now() - stamp); |
| 72 | + * }, _.now()); |
| 73 | + * // => Logs the number of milliseconds it took for the deferred invocation. |
| 74 | + */ |
| 75 | +var now = function() { |
| 76 | + return root.Date.now(); |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Creates a debounced function that delays invoking `func` until after `wait` |
| 81 | + * milliseconds have elapsed since the last time the debounced function was |
| 82 | + * invoked. The debounced function comes with a `cancel` method to cancel |
| 83 | + * delayed `func` invocations and a `flush` method to immediately invoke them. |
| 84 | + * Provide `options` to indicate whether `func` should be invoked on the |
| 85 | + * leading and/or trailing edge of the `wait` timeout. The `func` is invoked |
| 86 | + * with the last arguments provided to the debounced function. Subsequent |
| 87 | + * calls to the debounced function return the result of the last `func` |
| 88 | + * invocation. |
| 89 | + * |
| 90 | + * **Note:** If `leading` and `trailing` options are `true`, `func` is |
| 91 | + * invoked on the trailing edge of the timeout only if the debounced function |
| 92 | + * is invoked more than once during the `wait` timeout. |
| 93 | + * |
| 94 | + * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred |
| 95 | + * until to the next tick, similar to `setTimeout` with a timeout of `0`. |
| 96 | + * |
| 97 | + * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) |
| 98 | + * for details over the differences between `_.debounce` and `_.throttle`. |
| 99 | + * |
| 100 | + * @static |
| 101 | + * @memberOf _ |
| 102 | + * @since 0.1.0 |
| 103 | + * @category Function |
| 104 | + * @param {Function} func The function to debounce. |
| 105 | + * @param {number} [wait=0] The number of milliseconds to delay. |
| 106 | + * @param {Object} [options={}] The options object. |
| 107 | + * @param {boolean} [options.leading=false] |
| 108 | + * Specify invoking on the leading edge of the timeout. |
| 109 | + * @param {number} [options.maxWait] |
| 110 | + * The maximum time `func` is allowed to be delayed before it's invoked. |
| 111 | + * @param {boolean} [options.trailing=true] |
| 112 | + * Specify invoking on the trailing edge of the timeout. |
| 113 | + * @returns {Function} Returns the new debounced function. |
| 114 | + * @example |
| 115 | + * |
| 116 | + * // Avoid costly calculations while the window size is in flux. |
| 117 | + * jQuery(window).on('resize', _.debounce(calculateLayout, 150)); |
| 118 | + * |
| 119 | + * // Invoke `sendMail` when clicked, debouncing subsequent calls. |
| 120 | + * jQuery(element).on('click', _.debounce(sendMail, 300, { |
| 121 | + * 'leading': true, |
| 122 | + * 'trailing': false |
| 123 | + * })); |
| 124 | + * |
| 125 | + * // Ensure `batchLog` is invoked once after 1 second of debounced calls. |
| 126 | + * var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); |
| 127 | + * var source = new EventSource('/stream'); |
| 128 | + * jQuery(source).on('message', debounced); |
| 129 | + * |
| 130 | + * // Cancel the trailing debounced invocation. |
| 131 | + * jQuery(window).on('popstate', debounced.cancel); |
| 132 | + */ |
| 133 | +function debounce(func, wait, options) { |
| 134 | + var lastArgs, |
| 135 | + lastThis, |
| 136 | + maxWait, |
| 137 | + result, |
| 138 | + timerId, |
| 139 | + lastCallTime, |
| 140 | + lastInvokeTime = 0, |
| 141 | + leading = false, |
| 142 | + maxing = false, |
| 143 | + trailing = true; |
| 144 | + |
| 145 | + if (typeof func != 'function') { |
| 146 | + throw new TypeError(FUNC_ERROR_TEXT); |
| 147 | + } |
| 148 | + wait = toNumber(wait) || 0; |
| 149 | + if (isObject(options)) { |
| 150 | + leading = !!options.leading; |
| 151 | + maxing = 'maxWait' in options; |
| 152 | + maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait; |
| 153 | + trailing = 'trailing' in options ? !!options.trailing : trailing; |
| 154 | + } |
| 155 | + |
| 156 | + function invokeFunc(time) { |
| 157 | + var args = lastArgs, |
| 158 | + thisArg = lastThis; |
| 159 | + |
| 160 | + lastArgs = lastThis = undefined; |
| 161 | + lastInvokeTime = time; |
| 162 | + result = func.apply(thisArg, args); |
| 163 | + return result; |
| 164 | + } |
| 165 | + |
| 166 | + function leadingEdge(time) { |
| 167 | + // Reset any `maxWait` timer. |
| 168 | + lastInvokeTime = time; |
| 169 | + // Start the timer for the trailing edge. |
| 170 | + timerId = setTimeout(timerExpired, wait); |
| 171 | + // Invoke the leading edge. |
| 172 | + return leading ? invokeFunc(time) : result; |
| 173 | + } |
| 174 | + |
| 175 | + function remainingWait(time) { |
| 176 | + var timeSinceLastCall = time - lastCallTime, |
| 177 | + timeSinceLastInvoke = time - lastInvokeTime, |
| 178 | + result = wait - timeSinceLastCall; |
| 179 | + |
| 180 | + return maxing ? nativeMin(result, maxWait - timeSinceLastInvoke) : result; |
| 181 | + } |
| 182 | + |
| 183 | + function shouldInvoke(time) { |
| 184 | + var timeSinceLastCall = time - lastCallTime, |
| 185 | + timeSinceLastInvoke = time - lastInvokeTime; |
| 186 | + |
| 187 | + // Either this is the first call, activity has stopped and we're at the |
| 188 | + // trailing edge, the system time has gone backwards and we're treating |
| 189 | + // it as the trailing edge, or we've hit the `maxWait` limit. |
| 190 | + return (lastCallTime === undefined || (timeSinceLastCall >= wait) || |
| 191 | + (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); |
| 192 | + } |
| 193 | + |
| 194 | + function timerExpired() { |
| 195 | + var time = now(); |
| 196 | + if (shouldInvoke(time)) { |
| 197 | + return trailingEdge(time); |
| 198 | + } |
| 199 | + // Restart the timer. |
| 200 | + timerId = setTimeout(timerExpired, remainingWait(time)); |
| 201 | + } |
| 202 | + |
| 203 | + function trailingEdge(time) { |
| 204 | + timerId = undefined; |
| 205 | + |
| 206 | + // Only invoke if we have `lastArgs` which means `func` has been |
| 207 | + // debounced at least once. |
| 208 | + if (trailing && lastArgs) { |
| 209 | + return invokeFunc(time); |
| 210 | + } |
| 211 | + lastArgs = lastThis = undefined; |
| 212 | + return result; |
| 213 | + } |
| 214 | + |
| 215 | + function cancel() { |
| 216 | + if (timerId !== undefined) { |
| 217 | + clearTimeout(timerId); |
| 218 | + } |
| 219 | + lastInvokeTime = 0; |
| 220 | + lastArgs = lastCallTime = lastThis = timerId = undefined; |
| 221 | + } |
| 222 | + |
| 223 | + function flush() { |
| 224 | + return timerId === undefined ? result : trailingEdge(now()); |
| 225 | + } |
| 226 | + |
| 227 | + function debounced() { |
| 228 | + var time = now(), |
| 229 | + isInvoking = shouldInvoke(time); |
| 230 | + |
| 231 | + lastArgs = arguments; |
| 232 | + lastThis = this; |
| 233 | + lastCallTime = time; |
| 234 | + |
| 235 | + if (isInvoking) { |
| 236 | + if (timerId === undefined) { |
| 237 | + return leadingEdge(lastCallTime); |
| 238 | + } |
| 239 | + if (maxing) { |
| 240 | + // Handle invocations in a tight loop. |
| 241 | + timerId = setTimeout(timerExpired, wait); |
| 242 | + return invokeFunc(lastCallTime); |
| 243 | + } |
| 244 | + } |
| 245 | + if (timerId === undefined) { |
| 246 | + timerId = setTimeout(timerExpired, wait); |
| 247 | + } |
| 248 | + return result; |
| 249 | + } |
| 250 | + debounced.cancel = cancel; |
| 251 | + debounced.flush = flush; |
| 252 | + return debounced; |
| 253 | +} |
| 254 | + |
| 255 | +/** |
| 256 | + * Checks if `value` is the |
| 257 | + * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) |
| 258 | + * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) |
| 259 | + * |
| 260 | + * @static |
| 261 | + * @memberOf _ |
| 262 | + * @since 0.1.0 |
| 263 | + * @category Lang |
| 264 | + * @param {*} value The value to check. |
| 265 | + * @returns {boolean} Returns `true` if `value` is an object, else `false`. |
| 266 | + * @example |
| 267 | + * |
| 268 | + * _.isObject({}); |
| 269 | + * // => true |
| 270 | + * |
| 271 | + * _.isObject([1, 2, 3]); |
| 272 | + * // => true |
| 273 | + * |
| 274 | + * _.isObject(_.noop); |
| 275 | + * // => true |
| 276 | + * |
| 277 | + * _.isObject(null); |
| 278 | + * // => false |
| 279 | + */ |
| 280 | +function isObject(value) { |
| 281 | + var type = typeof value; |
| 282 | + return !!value && (type == 'object' || type == 'function'); |
| 283 | +} |
| 284 | + |
| 285 | +/** |
| 286 | + * Checks if `value` is object-like. A value is object-like if it's not `null` |
| 287 | + * and has a `typeof` result of "object". |
| 288 | + * |
| 289 | + * @static |
| 290 | + * @memberOf _ |
| 291 | + * @since 4.0.0 |
| 292 | + * @category Lang |
| 293 | + * @param {*} value The value to check. |
| 294 | + * @returns {boolean} Returns `true` if `value` is object-like, else `false`. |
| 295 | + * @example |
| 296 | + * |
| 297 | + * _.isObjectLike({}); |
| 298 | + * // => true |
| 299 | + * |
| 300 | + * _.isObjectLike([1, 2, 3]); |
| 301 | + * // => true |
| 302 | + * |
| 303 | + * _.isObjectLike(_.noop); |
| 304 | + * // => false |
| 305 | + * |
| 306 | + * _.isObjectLike(null); |
| 307 | + * // => false |
| 308 | + */ |
| 309 | +function isObjectLike(value) { |
| 310 | + return !!value && typeof value == 'object'; |
| 311 | +} |
| 312 | + |
| 313 | +/** |
| 314 | + * Checks if `value` is classified as a `Symbol` primitive or object. |
| 315 | + * |
| 316 | + * @static |
| 317 | + * @memberOf _ |
| 318 | + * @since 4.0.0 |
| 319 | + * @category Lang |
| 320 | + * @param {*} value The value to check. |
| 321 | + * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. |
| 322 | + * @example |
| 323 | + * |
| 324 | + * _.isSymbol(Symbol.iterator); |
| 325 | + * // => true |
| 326 | + * |
| 327 | + * _.isSymbol('abc'); |
| 328 | + * // => false |
| 329 | + */ |
| 330 | +function isSymbol(value) { |
| 331 | + return typeof value == 'symbol' || |
| 332 | + (isObjectLike(value) && objectToString.call(value) == symbolTag); |
| 333 | +} |
| 334 | + |
| 335 | +/** |
| 336 | + * Converts `value` to a number. |
| 337 | + * |
| 338 | + * @static |
| 339 | + * @memberOf _ |
| 340 | + * @since 4.0.0 |
| 341 | + * @category Lang |
| 342 | + * @param {*} value The value to process. |
| 343 | + * @returns {number} Returns the number. |
| 344 | + * @example |
| 345 | + * |
| 346 | + * _.toNumber(3.2); |
| 347 | + * // => 3.2 |
| 348 | + * |
| 349 | + * _.toNumber(Number.MIN_VALUE); |
| 350 | + * // => 5e-324 |
| 351 | + * |
| 352 | + * _.toNumber(Infinity); |
| 353 | + * // => Infinity |
| 354 | + * |
| 355 | + * _.toNumber('3.2'); |
| 356 | + * // => 3.2 |
| 357 | + */ |
| 358 | +function toNumber(value) { |
| 359 | + if (typeof value == 'number') { |
| 360 | + return value; |
| 361 | + } |
| 362 | + if (isSymbol(value)) { |
| 363 | + return NAN; |
| 364 | + } |
| 365 | + if (isObject(value)) { |
| 366 | + var other = typeof value.valueOf == 'function' ? value.valueOf() : value; |
| 367 | + value = isObject(other) ? (other + '') : other; |
| 368 | + } |
| 369 | + if (typeof value != 'string') { |
| 370 | + return value === 0 ? value : +value; |
| 371 | + } |
| 372 | + value = value.replace(reTrim, ''); |
| 373 | + var isBinary = reIsBinary.test(value); |
| 374 | + return (isBinary || reIsOctal.test(value)) |
| 375 | + ? freeParseInt(value.slice(2), isBinary ? 2 : 8) |
| 376 | + : (reIsBadHex.test(value) ? NAN : +value); |
| 377 | +} |
0 commit comments