Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

function-tools

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

function-tools - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

.travis.yml

56

lib/function-tools.js
"use strict";
/**
Useful higher-order functions
@module
@alias f
*/
exports.throttle = throttle;
/**
Guarantees a function a specified `restPeriod` in between invocations.
@param {Function} - the function to throttle
@param {Object} - options
@returns {Function}
@alias module:function-tools.throttle
*/
function throttle(f, options){
var resting = false;
var args = null;
var y = 0
var timer = null;
var lastRun = 0;
return function throttled(){
args = arguments;
// console.log("run %d, n=%d", ++y, args[0]);
if (!resting){
f.apply(this, args);
resting = true;
setTimeout(function(){
resting = false;
if (args) throttled.apply(null, args);
args = null
}, options.interval || 1000);
clearTimeout(timer);
var fArgs = arguments;
var timeSinceLastRun = Date.now() - lastRun;
if (timeSinceLastRun > options.restPeriod){
f.apply(f, fArgs);
lastRun = Date.now();
} else {
timer = setTimeout(function(){
f.apply(f, fArgs);
lastRun = Date.now();
}, options.restPeriod - timeSinceLastRun);
}
}
}
function printNumber(n){
console.log("%s: %s", new Date().toLocaleTimeString(), n);
}
var throttledPrint = throttle(printNumber, { interval: 100 });
var i = 0;
var interval = setInterval(function(){
throttledPrint(++i);
if (i === 27) clearInterval(interval);
}, 100);
{
"name": "function-tools",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "0.0.1",
"version": "0.1.0",
"description": "Useful higher-order functions",

@@ -19,3 +19,7 @@ "repository": "https://github.com/75lb/function-tools.git",

"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo"
},
"devDependencies": {
"jsdoc-to-markdown": "^0.1.9",
"tape": "^2.13.3"
}
}

@@ -6,2 +6,42 @@ [![view on npm](http://img.shields.io/npm/v/function-tools.svg)](https://www.npmjs.org/package/function-tools)

#function-tools
Useful higher-order functions
**Contents**
* [throttle(f, options)](#module_function-tools.throttle)
<a name="module_function-tools.throttle"></a>
###f.throttle(f, options)
Guarantees a function a specified `restPeriod` in between invocations.
- f `function` the function to throttle
- options `Object` options
**Returns**: `function`
var test = require("tape");
var lib = require("../");
var f = require("../");
test("first", function(t){
t.plan(3);
function testFunc(){
t.pass();
}
var throttled = f.throttle(testFunc, { restPeriod: 200 });
var i = 0;
var interval = setInterval(function(){
throttled(++i);
if (i === 30) clearInterval(interval);
}, 10);
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc