package/package.json000644 000766 000024 0000001276 12533677254013042 0ustar00000000 000000 { "name": "just-debounce", "version": "1.0.0", "description": "a simple debounce with no dependencies or crazy defaults", "main": "index.js", "scripts": { "test": "node test.js && npm run lint", "lint": "eslint ." }, "repository": { "type": "git", "url": "git://github.com/hayes/just-debounce.git" }, "keywords": [ "debounce" ], "author": "Michael Hayes", "license": "MIT", "bugs": { "url": "https://github.com/hayes/just-debounce/issues" }, "homepage": "https://github.com/hayes/just-debounce", "devDependencies": { "eslint": "^0.22.1", "eslint-config-standard": "^2.0.0", "eslint-plugin-react": "^2.4.0", "tape": "^4.0.0" } } package/.npmignore000644 000766 000024 0000001027 12436376242012540 0ustar00000000 000000 # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Deployed apps should consider commenting this line out: # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git node_modules package/README.md000644 000766 000024 0000002200 12436377116012013 0ustar00000000 000000 just-debounce ============= just a basic debounce function # Why? I searched npm and the first 3 pages of results for "debounce" did not have a small correctly implemented version of debounce # Usage ### arguments * `fn`: the function to debounce * `delay`: debounce delay in ms * `at_start:` if true, the function will be called at the beginning of the delay rather than the end * `guarantee`: ensures the time before the next call the `fn` is not greater \ than the delay period. ```javascript var db = require('just-debounce') var debounced = db(function(v) {console.log(v)}, 100) debounced('hi') debounced('hi') // logs 'hi' once after 100ms ``` ```javascript var db = require('just-debounce') var debounced = db(function(v) {console.log(v)}, 100, true) debounced('hi') debounced('hi') // logs 'hi' once right away, but not a second time. calling after 100ms will log again ``` ```javascript var db = require('just-debounce') var debounced = db(function(v) {console.log(v)}, 100, false, true) debounced('hi') setTimeout(function() {debounced('hi2')}, 80) // logs 'hi2' once 100ms after the first call to debounced ``` # license MIT package/LICENSE000644 000766 000024 0000002067 12436376242011553 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2014 Michael Hayes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/index.js000644 000766 000024 0000001135 12533675150012204 0ustar00000000 000000 module.exports = debounce function debounce (fn, delay, at_start, guarantee) { var timeout var args var self return function debounced () { self = this args = Array.prototype.slice.call(arguments) if (timeout && (at_start || guarantee)) { return } else if (!at_start) { clear() timeout = setTimeout(run, delay) return timeout } timeout = setTimeout(clear, delay) fn.apply(self, args) function run () { clear() fn.apply(self, args) } function clear () { clearTimeout(timeout) timeout = null } } } package/test.js000644 000766 000024 0000005731 12533677147012072 0ustar00000000 000000 var debounce = require('./index.js') var test = require('tape') test('debauce', function (t) { t.plan(3) var fn = debounce(function (a, b) { t.deepEqual(this, {call: 3}, 'context should be preserved') t.equal(a, 30, 'should preserve args') t.equal(b, 300, 'should preserve args') }, 10) fn.call({call: 1}, 10, 100) fn.call({call: 2}, 20, 200) setTimeout(function () { fn.call({call: 3}, 30, 300) }, 3) }) test('multiple calls should extend delay', function (t) { t.plan(4) var wasDelayed = false var fn = debounce(function (a, b) { t.deepEqual(this, {call: 3}, 'context should be preserved') t.equal(a, 30, 'should preserve args') t.equal(b, 300, 'should preserve args') t.ok(wasDelayed, 'should have waited longer than debounce period') }, 6) setTimeout(function longer () { wasDelayed = true }, 9) fn.call({call: 1}, 10, 100) setTimeout(function () { fn.call({call: 2}, 20, 200) setTimeout(function () { fn.call({call: 3}, 30, 300) }, 5) }, 3) }) test('multiple calls should not extend delay when guarantee is true', function (t) { t.plan(8) var first = true var wasDelayed = false var fn = debounce(function (a, b) { if (first) { t.deepEqual(this, {call: 2}, '1st context should be preserved') t.equal(a, 20, '1st should preserve 1st args') t.equal(b, 200, '1st should preserve 2nd args') t.notOk(wasDelayed, 'should not have waited longer than debounce period') first = false } else { t.deepEqual(this, {call: 3}, 'context should be preserved') t.equal(a, 30, 'should preserve args') t.equal(b, 300, 'should preserve args') t.ok(wasDelayed, 'should have waited longer than debounce period') } }, 6, false, true) setTimeout(function longer () { wasDelayed = true }, 7) fn.call({call: 1}, 10, 100) setTimeout(function () { fn.call({call: 2}, 20, 200) setTimeout(function () { fn.call({call: 3}, 30, 300) }, 5) }, 3) }) test('at start', function (t) { t.plan(9) var callCount = 0 var fn = debounce(function (a, b) { if (callCount === 0) { t.deepEqual(this, {call: 1}, '1st context should be preserved') t.equal(a, 10, '1st should preserve 1st args') t.equal(b, 100, '1st should preserve 2nd args') } else if (callCount === 1) { t.deepEqual(this, {call: 3}, 'context should be preserved') t.equal(a, 30, 'should preserve args') t.equal(b, 300, 'should preserve args') } else { t.deepEqual(this, {call: 4}, 'context should be preserved') t.equal(a, 40, 'should preserve 1st args') t.equal(b, 400, 'should preserve 2nd args') } callCount += 1 }, 6, true) fn.call({call: 1}, 10, 100) fn.call({call: 2}, 20, 200) setTimeout(function () { fn.call({call: 3}, 30, 300) setTimeout(function () { fn.call({call: 4}, 40, 400) }, 10) setTimeout(function () { fn.call({call: 5}, 50, 500) }, 3) }, 10) }) package/.eslintrc000644 000766 000024 0000000034 12533672405012360 0ustar00000000 000000 { "extends": "standard" }