Initial commit
This commit is contained in:
5
node_modules/pipe-functions/.jshintrc
generated
vendored
Normal file
5
node_modules/pipe-functions/.jshintrc
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"esversion": 6,
|
||||
"node": true,
|
||||
"loopfunc": false
|
||||
}
|
||||
3
node_modules/pipe-functions/.npmignore
generated
vendored
Normal file
3
node_modules/pipe-functions/.npmignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
_private/
|
||||
node_modules/
|
||||
npm-debug.log*
|
||||
3
node_modules/pipe-functions/.travis.yml
generated
vendored
Normal file
3
node_modules/pipe-functions/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
21
node_modules/pipe-functions/LICENSE.md
generated
vendored
Normal file
21
node_modules/pipe-functions/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 INFOinvest http://infoinvest.com.br
|
||||
|
||||
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.
|
||||
124
node_modules/pipe-functions/README.md
generated
vendored
Normal file
124
node_modules/pipe-functions/README.md
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
# pipe-functions
|
||||
|
||||
[](https://travis-ci.org/DiegoZoracKy/pipe-functions)
|
||||
|
||||
Pipe functions in a Unix-like style. It supports `Promises` (async) anywhere in the pipeline and every step will be executed sequentially. The *return* (*resolve* in case of `Promises`) of each function will be passed in as an argument to the next one
|
||||
|
||||
Key features:
|
||||
- Supports `Promises`, or any lib following the Promises/A+ spec about being *thenable* (**.then()** method)
|
||||
- `Promises` will be executed sequentially
|
||||
- First argument can be of any type (`String`, `Number`, `Date`, etc.) or even a `Function` or a `Promise`
|
||||
- Node.js and Browser ready (to be used on a Browser, without a build step, check `lib/pipe-non-es6.js`)
|
||||
- Lightweight, **501 bytes**, before gzip!
|
||||
|
||||
## Install
|
||||
|
||||
### NPM / Node
|
||||
|
||||
```javascript
|
||||
npm install pipe-functions
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Sync
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
// First argument can be of any type
|
||||
const result = pipe('input', fn1, fn2, fnN);
|
||||
// And also a function
|
||||
const result2 = pipe(fn0, fn1, fn2, fnN);
|
||||
```
|
||||
|
||||
### Async (`Promises`)
|
||||
|
||||
If the pipeline contains a `Promise` anywhere in the pipeline, we must treat `pipe` like a `Promise` itself, so we must to use **.then()** to get the final result.
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
// First argument can be of any type, as shown in the previous example
|
||||
pipe('input', fn1, fnPromise1, fn2, fnPromise2).then(console.log);
|
||||
```
|
||||
|
||||
A suggestion regarding `Promises`. Probably you've seen, or had to write, a stack of promises like that:
|
||||
|
||||
```javascript
|
||||
someAsyncFunction('param')
|
||||
.then(doSomethingWithTheResult)
|
||||
.then(doSomethingElseWithTheResultOfTheLast)
|
||||
.then(oneMore)
|
||||
.then(almostThere)
|
||||
.then(done)
|
||||
.catch(console.log)
|
||||
```
|
||||
|
||||
It could be written as:
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
pipe(
|
||||
someAsyncFunction('param'),
|
||||
doSomethingWithTheResult,
|
||||
doSomethingElseWithTheResultOfTheLast,
|
||||
oneMore,
|
||||
almostThere,
|
||||
done
|
||||
).catch(console.log)
|
||||
```
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
OBS: Some examples needs a platform with support for *Destructuring* (Nodejs v6+, Chrome).
|
||||
|
||||
### Sync
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
/** Functions **/
|
||||
const capitalize = v => v[0].toUpperCase() + v.slice(1);
|
||||
const quote = v => `"${v}"`;
|
||||
|
||||
/** Pipe **/
|
||||
// result will be: "Time"
|
||||
const result = pipe('time', capitalize, quote);
|
||||
```
|
||||
|
||||
### Async (`Promises`)
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
/** Functions **/
|
||||
// Sync
|
||||
const capitalize = v => v[0].toUpperCase() + v.slice(1);
|
||||
// Async
|
||||
const fetchAndSetBandName = v => new Promise((resolve, reject) => setTimeout(() => resolve(`Pink Floyd - ${v}`), 1000));
|
||||
|
||||
/** Pipe **/
|
||||
// the result will be: Pink Floyd - Time
|
||||
pipe('time', capitalize, fetchAndSetBandName).then(console.log)
|
||||
```
|
||||
#### Example with destructuring,
|
||||
|
||||
To easily pass in more than one value (within an Object or Array) through the pipeline.
|
||||
|
||||
```javascript
|
||||
const pipe = require('pipe-functions');
|
||||
|
||||
/** Functions **/
|
||||
// Async
|
||||
const fetchBandName = ({ song }) => new Promise((resolve, reject) =>
|
||||
setTimeout(() => resolve({ song, band: 'Pink Floyd' }), 1000));
|
||||
// Sync
|
||||
const concatBandAndSong = ({ song, band }) => `${band} - ${song}`;
|
||||
|
||||
/** Pipe **/
|
||||
// the result will be: Pink Floyd - Time
|
||||
pipe('time', fetchBandName, concatBandAndSong).then(console.log)
|
||||
```
|
||||
17
node_modules/pipe-functions/lib/pipe-non-es6.js
generated
vendored
Normal file
17
node_modules/pipe-functions/lib/pipe-non-es6.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
// Is not being mantained anymore
|
||||
var pipe = function() {
|
||||
return Array.prototype.slice.call(arguments).reduce(function(v, f, i) {
|
||||
if (i === 1 && v && v.constructor === Function) {
|
||||
let exec = v();
|
||||
if (exec && exec.then) {
|
||||
return exec.then(v => f(v));
|
||||
} else {
|
||||
return f(exec);
|
||||
}
|
||||
} else if (v && v.then) {
|
||||
return v.then(v => f(v));
|
||||
} else {
|
||||
return f(v);
|
||||
}
|
||||
});
|
||||
};
|
||||
18
node_modules/pipe-functions/lib/pipe.js
generated
vendored
Normal file
18
node_modules/pipe-functions/lib/pipe.js
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const pipe = (...fns) => fns.length === 1 ? fns[0].constructor === Function ? fns[0]() : fns[0] : fns.reduce((v, f, i) => {
|
||||
if (i === 1 && v && v.constructor === Function) {
|
||||
let exec = v();
|
||||
if (exec && exec.then) {
|
||||
return exec.then(v => f(v));
|
||||
} else {
|
||||
return f(exec);
|
||||
}
|
||||
} else if (v && v.then) {
|
||||
return v.then(v => f(v));
|
||||
} else {
|
||||
return f(v);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = pipe;
|
||||
32
node_modules/pipe-functions/package.json
generated
vendored
Normal file
32
node_modules/pipe-functions/package.json
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "pipe-functions",
|
||||
"version": "1.3.0",
|
||||
"description": "Pipe functions in a Unix-like style. It supports Promises (async) anywhere in the pipeline and every step will be executed sequentially. The return (resolve in case of Promises) of each function will be passed in as an argument to the next one",
|
||||
"homepage": "https://github.com/DiegoZoracKy/pipe-functions/",
|
||||
"main": "./lib/pipe.js",
|
||||
"author": {
|
||||
"name": "Diego ZoracKy",
|
||||
"email": "diego.zoracky@gmail.com",
|
||||
"url": "https://github.com/DiegoZoracKy/"
|
||||
},
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/DiegoZoracKy/pipe-functions.git"
|
||||
},
|
||||
"keywords": [
|
||||
"pipe",
|
||||
"pipe functions",
|
||||
"promises",
|
||||
"sync",
|
||||
"async"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "mocha ./tests/main.test.js",
|
||||
"test:all": "mocha ./tests -b"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^3.5.0",
|
||||
"mocha": "^3.0.2"
|
||||
}
|
||||
}
|
||||
62
node_modules/pipe-functions/tests/main.test.js
generated
vendored
Normal file
62
node_modules/pipe-functions/tests/main.test.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
/*globals describe, it*/
|
||||
'use strict';
|
||||
|
||||
const pipe = require('../');
|
||||
const assert = require('assert');
|
||||
|
||||
const capitalize = v => v[0].toUpperCase() + v.slice(1);
|
||||
const quote = v => `"${v}"`;
|
||||
const fetchAndSetBandName = v => new Promise((resolve, reject) => setTimeout(() => resolve(`Pink Floyd - ${v}`), 1000));
|
||||
|
||||
describe('Pipe', function() {
|
||||
|
||||
describe('Functions (sync)', function() {
|
||||
|
||||
it(`'time' should be turned into '"Time"'`, function() {
|
||||
const result = pipe('time', capitalize, quote);
|
||||
assert.equal(result, `"Time"`);
|
||||
});
|
||||
|
||||
it(`should work with even with only one value being input`, function() {
|
||||
assert.equal(pipe('Z'), 'Z');
|
||||
});
|
||||
|
||||
it(`should work with even with only one function being input`, function() {
|
||||
assert.equal(pipe(x => 'K'), 'K');
|
||||
});
|
||||
|
||||
it(`should pass forward 'undefined' values`, function() {
|
||||
const a = () => undefined;
|
||||
const b = (x) => x + undefined;
|
||||
const c = (x) => x + 'C';
|
||||
|
||||
assert(pipe(a, b, c));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Functions && Promises (sync && async)', function() {
|
||||
|
||||
it(`'time' should be turned into 'Pink Floyd - Time'`, function(done) {
|
||||
pipe('time', capitalize, fetchAndSetBandName)
|
||||
.then(result => assert.equal(`Pink Floyd - Time`, result))
|
||||
.then(() => done());
|
||||
});
|
||||
|
||||
it(`should pass forward 'undefined' values`, function() {
|
||||
const a = () => new Promise((resolve, reject) => setTimeout(() => resolve(undefined), 1000));
|
||||
const b = (x) => x + undefined;
|
||||
const c = (x) => x + 'C';
|
||||
|
||||
assert(pipe(a, b, c));
|
||||
});
|
||||
|
||||
it(`should work with even with only one function being input`, function(done) {
|
||||
const promise = () => new Promise((resolve, reject) => setTimeout(() => resolve('Z'), 1000));
|
||||
pipe(promise)
|
||||
.then(result => assert.equal(`Z`, result))
|
||||
.then(() => done());
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
46
node_modules/pipe-functions/tests/pipe-es6.test.js
generated
vendored
Normal file
46
node_modules/pipe-functions/tests/pipe-es6.test.js
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
/*globals describe, it*/
|
||||
'use strict';
|
||||
|
||||
const pipe = require('../lib/pipe-es6');
|
||||
const assert = require('assert');
|
||||
|
||||
const capitalize = v => v[0].toUpperCase() + v.slice(1);
|
||||
const quote = v => `"${v}"`;
|
||||
const fetchAndSetBandName = v => new Promise((resolve, reject) => setTimeout(() => resolve(`Pink Floyd - ${v}`), 1000));
|
||||
const fetchBandName = ({ song }) => new Promise((resolve, reject) => setTimeout(() => resolve({ song, band: 'Pink Floyd' }), 1000));
|
||||
const concatBandAndSong = ({ song, band }) => `${band} - ${song}`;
|
||||
|
||||
describe('Pipe', function() {
|
||||
|
||||
describe('Functions (sync)', function() {
|
||||
|
||||
it(`'time' should be turned into '"Time"'`, function() {
|
||||
const result = pipe('time', capitalize, quote);
|
||||
assert.equal(result, `"Time"`);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Functions && Promises (sync && async)', function() {
|
||||
|
||||
it(`'time' should be turned into 'Pink Floyd - Time'`, function(done) {
|
||||
|
||||
pipe('time', capitalize, fetchAndSetBandName)
|
||||
.then(result => assert.equal(`Pink Floyd - Time`, result))
|
||||
.then(() => done());
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Functions && Promises (sync && async) - Destructuring', function() {
|
||||
|
||||
it(`'time' should be turned into 'Pink Floyd - Time'`, function(done) {
|
||||
|
||||
pipe({ song: 'Time' }, fetchBandName, concatBandAndSong)
|
||||
.then(result => assert.equal(`Pink Floyd - Time`, result))
|
||||
.then(() => done());
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user