debut des details de la page. Vu que c'est le troisieme (euh quatrieme?) composant, c'etait un peu plus rapide, mais heureusement que claude est la pour repasser derriere mes erreurs prcq en solo je n'y arriverais pas du tout!
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Gianluca Guarini
|
||||
|
||||
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.
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
<img src="ruit-logo.svg" width="46%"/>
|
||||
|
||||
Functional tasks serialization mini script (0.3kb)
|
||||
|
||||
[![Build Status][travis-image]][travis-url]
|
||||
|
||||
[![NPM version][npm-version-image]][npm-url]
|
||||
[![NPM downloads][npm-downloads-image]][npm-url]
|
||||
[![MIT License][license-image]][license-url]
|
||||
|
||||
## Installation
|
||||
|
||||
```js
|
||||
import ruit from 'ruit'
|
||||
```
|
||||
|
||||
[travis-image]: https://img.shields.io/travis/GianlucaGuarini/ruit.svg?style=flat-square
|
||||
|
||||
[travis-url]: https://travis-ci.org/GianlucaGuarini/ruit
|
||||
|
||||
[license-image]: http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
|
||||
|
||||
[license-url]: LICENSE.txt
|
||||
|
||||
[npm-version-image]: http://img.shields.io/npm/v/ruit.svg?style=flat-square
|
||||
|
||||
[npm-downloads-image]: http://img.shields.io/npm/dm/ruit.svg?style=flat-square
|
||||
|
||||
[npm-url]: https://npmjs.org/package/ruit
|
||||
|
||||
## API
|
||||
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### ruit
|
||||
|
||||
Serialize a list of sync and async tasks from left to right
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `tasks` **any** list of tasks to process sequentially
|
||||
|
||||
**Examples**
|
||||
|
||||
```javascript
|
||||
const curry = f => a => b => f(a, b)
|
||||
const add = (a, b) => a + b
|
||||
|
||||
const addOne = curry(add)(1)
|
||||
|
||||
const squareAsync = (num) => {
|
||||
return new Promise(r => {
|
||||
setTimeout(r, 500, num * 2)
|
||||
})
|
||||
}
|
||||
|
||||
// a -> a + a -> a * 2
|
||||
// basically from left to right: 1 => 1 + 1 => 2 * 2
|
||||
ruit(1, addOne, squareAsync).then(result => console.log(result)) // 4
|
||||
```
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** a promise containing the result of the whole chain
|
||||
|
||||
#### cancel
|
||||
|
||||
Helper that can be returned by ruit function to cancel the tasks chain
|
||||
|
||||
**Examples**
|
||||
|
||||
```javascript
|
||||
ruit(
|
||||
100,
|
||||
num => Math.random() * num
|
||||
num => num > 50 ? ruit.cancel() : num
|
||||
num => num - 2
|
||||
).then(result => {
|
||||
console.log(result) // here we will get only number lower than 50
|
||||
})
|
||||
```
|
||||
|
||||
Returns **[Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)** internal private constant
|
||||
|
||||
#### compose
|
||||
|
||||
The same as ruit() but with the arguments inverted from right to left
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `tasks` **any** list of tasks to process sequentially
|
||||
|
||||
**Examples**
|
||||
|
||||
```javascript
|
||||
const curry = f => a => b => f(a, b)
|
||||
const add = (a, b) => a + b
|
||||
|
||||
const addOne = curry(add)(1)
|
||||
|
||||
const squareAsync = (num) => {
|
||||
return new Promise(r => {
|
||||
setTimeout(r, 500, num * 2)
|
||||
})
|
||||
}
|
||||
|
||||
// a -> a + a -> a * 2
|
||||
// basically from right to left: 1 => 1 + 1 => 2 * 2
|
||||
ruit.compose(squareAsync, addOne, 1).then(result => console.log(result)) // 4
|
||||
```
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** a promise containing the result of the whole chain
|
||||
|
||||
# Ruit meaning
|
||||
|
||||
`ruit` comes from the `ruere` latin verb that means `It falls`, It expresses properly the essence of this script and sounds also similar to `run it`
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global.ruit = factory());
|
||||
}(this, (function () { 'use strict';
|
||||
|
||||
/**
|
||||
* Cancel token
|
||||
* @private
|
||||
* @type { Symbol }
|
||||
*/
|
||||
var CANCEL = Symbol();
|
||||
|
||||
/**
|
||||
* Helper that can be returned by ruit function to cancel the tasks chain
|
||||
* @returns { Symbol } internal private constant
|
||||
* @example
|
||||
*
|
||||
* ruit(
|
||||
* 100,
|
||||
* num => Math.random() * num
|
||||
* num => num > 50 ? ruit.cancel() : num
|
||||
* num => num - 2
|
||||
* ).then(result => {
|
||||
* console.log(result) // here we will get only number lower than 50
|
||||
* })
|
||||
*
|
||||
*/
|
||||
ruit.cancel = function () { return CANCEL; };
|
||||
|
||||
/**
|
||||
* The same as ruit() but with the arguments inverted from right to left
|
||||
* @param { * } tasks - list of tasks to process sequentially
|
||||
* @returns { Promise } a promise containing the result of the whole chain
|
||||
* @example
|
||||
*
|
||||
* const curry = f => a => b => f(a, b)
|
||||
* const add = (a, b) => a + b
|
||||
*
|
||||
* const addOne = curry(add)(1)
|
||||
*
|
||||
* const squareAsync = (num) => {
|
||||
* return new Promise(r => {
|
||||
* setTimeout(r, 500, num * 2)
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* // a -> a + a -> a * 2
|
||||
* // basically from right to left: 1 => 1 + 1 => 2 * 2
|
||||
* ruit.compose(squareAsync, addOne, 1).then(result => console.log(result)) // 4
|
||||
*/
|
||||
ruit.compose = function () {
|
||||
var tasks = [], len = arguments.length;
|
||||
while ( len-- ) tasks[ len ] = arguments[ len ];
|
||||
|
||||
return ruit.apply(void 0, tasks.reverse());
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize a list of sync and async tasks from left to right
|
||||
* @param { * } tasks - list of tasks to process sequentially
|
||||
* @returns { Promise } a promise containing the result of the whole chain
|
||||
* @example
|
||||
*
|
||||
* const curry = f => a => b => f(a, b)
|
||||
* const add = (a, b) => a + b
|
||||
*
|
||||
* const addOne = curry(add)(1)
|
||||
*
|
||||
* const squareAsync = (num) => {
|
||||
* return new Promise(r => {
|
||||
* setTimeout(r, 500, num * 2)
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* // a -> a + a -> a * 2
|
||||
* // basically from left to right: 1 => 1 + 1 => 2 * 2
|
||||
* ruit(1, addOne, squareAsync).then(result => console.log(result)) // 4
|
||||
*/
|
||||
function ruit() {
|
||||
var tasks = [], len = arguments.length;
|
||||
while ( len-- ) tasks[ len ] = arguments[ len ];
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
return (function run(queue, result) {
|
||||
if (!queue.length) { return resolve(result) }
|
||||
|
||||
var task = queue[0];
|
||||
var rest = queue.slice(1);
|
||||
var value = typeof task === 'function' ? task(result) : task;
|
||||
var done = function (v) { return run(rest, v); };
|
||||
|
||||
// check against nil values
|
||||
if (value != null) {
|
||||
if (value === CANCEL) { return }
|
||||
if (value.then) { return value.then(done, reject) }
|
||||
}
|
||||
|
||||
return Promise.resolve(done(value))
|
||||
})(tasks)
|
||||
})
|
||||
}
|
||||
|
||||
return ruit;
|
||||
|
||||
})));
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Cancel token
|
||||
* @private
|
||||
* @type { Symbol }
|
||||
*/
|
||||
const CANCEL = Symbol()
|
||||
|
||||
/**
|
||||
* Helper that can be returned by ruit function to cancel the tasks chain
|
||||
* @returns { Symbol } internal private constant
|
||||
* @example
|
||||
*
|
||||
* ruit(
|
||||
* 100,
|
||||
* num => Math.random() * num
|
||||
* num => num > 50 ? ruit.cancel() : num
|
||||
* num => num - 2
|
||||
* ).then(result => {
|
||||
* console.log(result) // here we will get only number lower than 50
|
||||
* })
|
||||
*
|
||||
*/
|
||||
ruit.cancel = () => CANCEL
|
||||
|
||||
/**
|
||||
* The same as ruit() but with the arguments inverted from right to left
|
||||
* @param { * } tasks - list of tasks to process sequentially
|
||||
* @returns { Promise } a promise containing the result of the whole chain
|
||||
* @example
|
||||
*
|
||||
* const curry = f => a => b => f(a, b)
|
||||
* const add = (a, b) => a + b
|
||||
*
|
||||
* const addOne = curry(add)(1)
|
||||
*
|
||||
* const squareAsync = (num) => {
|
||||
* return new Promise(r => {
|
||||
* setTimeout(r, 500, num * 2)
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* // a -> a + a -> a * 2
|
||||
* // basically from right to left: 1 => 1 + 1 => 2 * 2
|
||||
* ruit.compose(squareAsync, addOne, 1).then(result => console.log(result)) // 4
|
||||
*/
|
||||
ruit.compose = (...tasks) => ruit(...tasks.reverse())
|
||||
|
||||
/**
|
||||
* Serialize a list of sync and async tasks from left to right
|
||||
* @param { * } tasks - list of tasks to process sequentially
|
||||
* @returns { Promise } a promise containing the result of the whole chain
|
||||
* @example
|
||||
*
|
||||
* const curry = f => a => b => f(a, b)
|
||||
* const add = (a, b) => a + b
|
||||
*
|
||||
* const addOne = curry(add)(1)
|
||||
*
|
||||
* const squareAsync = (num) => {
|
||||
* return new Promise(r => {
|
||||
* setTimeout(r, 500, num * 2)
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* // a -> a + a -> a * 2
|
||||
* // basically from left to right: 1 => 1 + 1 => 2 * 2
|
||||
* ruit(1, addOne, squareAsync).then(result => console.log(result)) // 4
|
||||
*/
|
||||
export default function ruit(...tasks) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return (function run(queue, result) {
|
||||
if (!queue.length) return resolve(result)
|
||||
|
||||
const [task, ...rest] = queue
|
||||
const value = typeof task === 'function' ? task(result) : task
|
||||
const done = v => run(rest, v)
|
||||
|
||||
// check against nil values
|
||||
if (value != null) {
|
||||
if (value === CANCEL) return
|
||||
if (value.then) return value.then(done, reject)
|
||||
}
|
||||
|
||||
return Promise.resolve(done(value))
|
||||
})(tasks)
|
||||
})
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "ruit",
|
||||
"version": "1.0.4",
|
||||
"description": "Tasks serialization minilibrary",
|
||||
"main": "index.js",
|
||||
"jsnext:main": "index.next.js",
|
||||
"module": "index.next.js",
|
||||
"scripts": {
|
||||
"prepublish": "npm run build && npm test",
|
||||
"lint": "eslint index.next.js test.js rollup.config.js",
|
||||
"build": "rollup -c",
|
||||
"doc": "documentation readme index.next.js -s API",
|
||||
"test": "npm run lint && mocha test.js"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.next.js"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/gianlucaguarini/ruit.git"
|
||||
},
|
||||
"keywords": [
|
||||
"series",
|
||||
"promises",
|
||||
"flow",
|
||||
"functional",
|
||||
"composition",
|
||||
"tasks"
|
||||
],
|
||||
"author": "Gianluca Guarini <gianluca.guarini@gmail.com> (http://gianlucaguarini.com)",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@gianlucaguarini/eslint-config": "^2.0.0",
|
||||
"documentation": "^7.1.0",
|
||||
"eslint": "^4.19.1",
|
||||
"mocha": "^5.2.0",
|
||||
"rollup": "^0.59.4",
|
||||
"rollup-plugin-buble": "^0.19.2"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/gianlucaguarini/ruit/issues"
|
||||
},
|
||||
"homepage": "https://github.com/gianlucaguarini/ruit#readme",
|
||||
"dependencies": {}
|
||||
}
|
||||
Reference in New Issue
Block a user