Skip to content

Commit

Permalink
package.json update with new version, required node version and descr…
Browse files Browse the repository at this point in the history
…iption, also readme update
  • Loading branch information
pawelzny committed Jan 19, 2018
1 parent db4f1d9 commit de64f14
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 61 deletions.
146 changes: 89 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# last-callback v 1.0.3
# last-callback v 2.0.0

Extract function's last argument and wrap with ready to invoke function.
If last argument is callable, hence callback - wrapper will invoke it.
Otherwise invoked wrapper do nothing.

## Description

Get and call last given argument if is a function. You do not need to check last argument manually any more.
All you need to do is to pass arguments array to this little module, then call it, or bind with `this` context.
Last-callback always return function with built in validation. Last non callable argument will be ignored without throwing any exception.
Last callback provides callable wrapper for last argument. There is no need for
further checking if last argument is callable.

last-callback is compatible with ES5 and latest.
Wrapping works for infinite number of arguments even undeclared one.

[![npm](https://img.shields.io/npm/l/last-callback.svg?maxAge=2592000)]()
[![npm](https://img.shields.io/npm/dt/last-callback.svg?maxAge=2592000)]()
[![node](https://img.shields.io/node/v/last-callback.svg?maxAge=2592000)]()
[![CircleCI](https://img.shields.io/circleci/project/github/pawelzny/last-callback.svg)]()
[![Maintenance](https://img.shields.io/maintenance/yes/2018.svg?maxAge=2592000)]()
[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)]()

## Requirements

NodeJS >= 4.4.0
NodeJS >= 8.9.0

<div style="color: red; font-weight: 700;">
For NodeJS 4.4, 6.x and 7.x use last-callback version 1.0.3
</div>

## installation

Expand All @@ -26,106 +34,130 @@ with NPM:
npm install -S last-callback
```

## Get last callback
For older NodeJS

last-callback respects: bind, call, and apply methods;
```javascript
npm install -S last-callback@1.0.3
```

### ES6 Style
## Optional callback as last argument

If you are using `NodeJS >= 6.2` You should definitely use __ES6 style__ with spread operators.
**Scenario:** There is "execute" function which expect last argument to be optional callback.

```javascript
const lastCallback = require('last-callback');

function myFunc (param) {
let callback = lastCallback(...arguments);
function execute(x, y, cb=null) {
// 'arguments' is magic variable always visible inside function.
let callback = lastCallback(...arguments);
let sum = x + y;

callback(param);
callback(sum);
return sum;
}

myFunc('test value', function (param) {
console.log(param); // 'test value'
});
execute(10, 34, (result) => console.log(result));
```

### ES5 Style
## Undeclared optional callback as last argument

If you are still using `NodeJS < 6.2.0`, i.e. `NodeJS 4.4.7 LTS`, you can use __ES5 style__.
**Scenario:** There is "make" function which allow callback as last argument.

**TIP** Undeclared arguments will NOT be visible for debugger and IDE inspection.

```javascript
var lastCallback = require('last-callback');
const lastCallback = require('last-callback');

function myFunc (param) {
var callback = lastCallback.apply(null, arguments);
function make(x, y, z) {
// 'arguments' is magic variable always visible inside function.
let callback = lastCallback(...arguments);
let result = (x + y) * z;

callback(param);
callback(result);
return result;
}

myFunc('test value', function (param) {
console.log(param); // 'test value'
});
// make declared 3 arguments: 'x', 'y' and 'z'
// but passing fourth argument is completely legal
// and JS will not complain.
execute(10, 34, 5, (result) => console.log(result));
```

### ES5 Style with context
### Calling callback with different context

**Scenario:** There is 'execute' function which expect callback. There is also 'spy' object, which alter callback context.

```javascript
var lastCallback = require('last-callback');
const lastCallback = require('last-callback');

function myFunc (param) {
var callback = lastCallback.apply(null, arguments);
function returnSecret(secret) {
if (this.secret === undefined) {
this.secret = secret;
}

this.contextVariable = 'this is my context';
return this.secret;
}

callback.call(this, param); // bind context if you need it
// returns 'secret' without change
function execute(secret, cb) {
return lastCallback(...arguments)(secret);
}

myFunc('test value', function (param) {
console.log(param); // 'test value'
console.log(this.contextVariable); // 'this is my context'
});
// returns 'secret' from different context
function executeContext(secret, context, cb) {
return lastCallback(...arguments).call(context);
}

secretPassword = 'I am real secret';

// will return 'I am real secret'
execute(secretPassword, returnSecret);

// will return 'Luke I am your father'
executeContext(secretPassword, {secret: 'Luke I am your father}, returnSecret);
```
### Recursive callback
You can use recursive callback if needed.
**Scenario:** There is a 'execute' function which expected callback. The callback will be recursive.
```javascript
const lastCallback = require('last-callback');
let
iteration = 1,
limit = 5;

function myFunc (iteration) {
let callback = lastCallback(...arguments);

callback(iteration);
function recursiveCb (n, t) {
if (n < t) {
console.log(n)
recursiveCb(n + 1, t);
}
}
function recursiveCallabck (iteration) {
if (iteration >= limit) {
return;
}

console.log(iteration);

iteration += 1;
recursiveCallabck(iteration);
function execute(start, stop, cb) {
lastCallback(...arguments)(start, stop);
}
myFunc(iteration, recursiveCallabck);
// console log:
execute(0, 5, recursiveCb);
// console log result:
// 0
// 1
// 2
// 3
// 4
// 5
```
## Contribution
Feel free to Pull Request
Did you find any bugs?
Maybe this documentation has language mistakes?
You have idea for great new feature?
Create new issue and describe your point of view.
I will do my best to meet all requests.
This repository is open for changes and suggestions.
I encourage you to write your own solution and make pull request.
## LICENSE
The MIT License (MIT)
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "last-callback",
"version": "1.0.3",
"version": "2.0.0",
"author": "Paweł Zadrożny <[email protected]> (http://pawelzny.com/)",
"description": "Get and call last given argument if is a function.",
"description": "Extract function's last argument and wrap with ready to invoke function.",
"keywords": [
"last",
"callback",
"function",
"arguments"
"argument",
"parameter",
"callback wrapper"
],
"license": "MIT",
"homepage": "https://pawelzny.com/npm/last-callback",
Expand All @@ -24,7 +26,7 @@
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha"
},
"engines": {
"node": ">= 4.4.0"
"node": ">= 8.9.0"
},
"devDependencies": {
"chai": "4.1.2",
Expand Down

0 comments on commit de64f14

Please sign in to comment.