function-tools
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,18 +0,18 @@ | ||
"use strict"; | ||
var f = require("../"); | ||
'use strict' | ||
var f = require('../') | ||
/* testFunc should be called maximum every 200ms */ | ||
var lastRun = Date.now(); | ||
function testFunc(n){ | ||
console.log("%d: %d", n, Date.now() - lastRun); | ||
lastRun = Date.now(); | ||
var lastRun = Date.now() | ||
function testFunc (n) { | ||
console.log('%d: %d', n, Date.now() - lastRun) | ||
lastRun = Date.now() | ||
} | ||
var throttled = f.throttle(testFunc, { restPeriod: 200 }); | ||
var throttled = f.throttle(testFunc, { restPeriod: 200 }) | ||
/* call the throttled testFunc 30 times */ | ||
var i = 0; | ||
var interval = setInterval(function(){ | ||
throttled(++i); | ||
if (i === 30) clearInterval(interval); | ||
}, 10); | ||
var i = 0 | ||
var interval = setInterval(function () { | ||
throttled(++i) | ||
if (i === 30) clearInterval(interval) | ||
}, 10) |
@@ -1,16 +0,16 @@ | ||
"use strict"; | ||
'use strict' | ||
/** | ||
Useful higher-order functions | ||
@module | ||
@alias f | ||
@module | ||
@typicalname f | ||
@example | ||
```js | ||
var f = require("function-tools"); | ||
var f = require("function-tools") | ||
``` | ||
*/ | ||
exports.throttle = throttle; | ||
exports.throttle = throttle | ||
/** | ||
Guarantees a function a specified `restPeriod` in between invocations. | ||
Guarantees a function a specified `restPeriod` in between invocations. | ||
@param {Function} - the function to throttle | ||
@@ -23,24 +23,24 @@ @param [options] {Object} - the options | ||
```js | ||
var throttled = f.throttle(myFunction, { restPeriod: 200 }); | ||
throtted(); // this will only execute if at least 200ms since the last invocation | ||
var throttled = f.throttle(myFunction, { restPeriod: 200 }) | ||
throtted() // this will only execute if at least 200ms since the last invocation | ||
``` | ||
*/ | ||
function throttle(f, options){ | ||
var timer = null; | ||
var lastRun = 0; | ||
return function throttled(){ | ||
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 throttle (f, options) { | ||
var timer = null | ||
var lastRun = 0 | ||
return function throttled () { | ||
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) | ||
} | ||
} | ||
} |
{ | ||
"name": "function-tools", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Useful higher-order functions", | ||
"repository": "https://github.com/75lb/function-tools.git", | ||
"main": "./lib/function-tools.js", | ||
"bin": { | ||
"function-tools": "bin/cli.js" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"throttle" | ||
"throttle", | ||
"function" | ||
], | ||
@@ -19,9 +18,8 @@ "engines": { | ||
"test": "tape test/*.js", | ||
"lint": "jshint lib/*.js bin/*.js test/*.js; echo", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^0.5", | ||
"tape": "^2.13.3" | ||
"jsdoc-to-markdown": "^1.2", | ||
"tape": "^4" | ||
} | ||
} |
@@ -5,5 +5,6 @@ [![view on npm](http://img.shields.io/npm/v/function-tools.svg)](https://www.npmjs.org/package/function-tools) | ||
[![Dependency Status](https://david-dm.org/75lb/function-tools.svg)](https://david-dm.org/75lb/function-tools) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
<a name="module_function-tools"></a> | ||
#function-tools | ||
## function-tools | ||
Useful higher-order functions | ||
@@ -13,21 +14,24 @@ | ||
```js | ||
var f = require("function-tools"); | ||
var f = require("function-tools") | ||
``` | ||
<a name="module_function-tools.throttle"></a> | ||
##f.throttle(f, [options]) | ||
### f.throttle(f, [options]) ⇒ <code>function</code> | ||
Guarantees a function a specified `restPeriod` in between invocations. | ||
**Params** | ||
**Kind**: static method of <code>[function-tools](#module_function-tools)</code> | ||
- f `function` - the function to throttle | ||
- \[options\] `Object` - the options | ||
- \[restPeriod\] `number` - a value in ms | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| f | <code>function</code> | the function to throttle | | ||
| [options] | <code>Object</code> | the options | | ||
| [options.restPeriod] | <code>number</code> | a value in ms | | ||
**Returns**: `function` | ||
**Example** | ||
```js | ||
var throttled = f.throttle(myFunction, { restPeriod: 200 }); | ||
throtted(); // this will only execute if at least 200ms since the last invocation | ||
var throttled = f.throttle(myFunction, { restPeriod: 200 }) | ||
throtted() // this will only execute if at least 200ms since the last invocation | ||
``` | ||
* * * | ||
© 2015 Lloyd Brookes <75pound@gmail.com> |
@@ -1,18 +0,17 @@ | ||
var test = require("tape"); | ||
var f = require("../"); | ||
var test = require('tape') | ||
var f = require('../') | ||
test("first", function(t){ | ||
t.plan(3); | ||
function testFunc(){ | ||
t.pass(); | ||
} | ||
test('first', function (t) { | ||
t.plan(3) | ||
function testFunc () { | ||
t.pass() | ||
} | ||
var throttled = f.throttle(testFunc, { restPeriod: 200 }); | ||
var throttled = f.throttle(testFunc, { restPeriod: 200 }) | ||
var i = 0; | ||
var interval = setInterval(function(){ | ||
throttled(++i); | ||
if (i === 30) clearInterval(interval); | ||
}, 10); | ||
}); | ||
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5663
36
9
71