Comparing version 1.3.0 to 1.4.0
@@ -0,7 +1,9 @@ | ||
const {curry} = require('../fp'); | ||
Object.prototype.map = function(fn){ return Object.entries(this).reduce((acc,[k,v]) => ({...acc,[k]:fn(v,k)}),{}); }; | ||
Object.prototype.filter = function(fn){ return Object.entries(this).reduce((acc,[k,v]) => fn(v,k) && ({...acc,[k]:v}),{}) || acc; }; | ||
Object.prototype.filter = function(fn){ return Object.entries(this).reduce((acc,[k,v]) => fn(v,k) && ({...acc,[k]:v}),{}) || {}; }; | ||
const map = fn => col => col.map(fn); | ||
const filter = fn => col => col.filter(fn); | ||
const map = curry((fn, col) => col.map(fn)); | ||
const filter = curry((fn, col) => col.filter(fn)); | ||
module.exports = {map, filter}; |
@@ -1,1 +0,15 @@ | ||
Promise.any = (promises) => Promise.all(promises.map(promise => promise.then(value => ({value})).catch(error => ({error})))); | ||
Promise.any = promises => Promise.all(promises.map(promise => promise.then(value => ({ value })).catch(error => ({ error })))); | ||
Promise.best = promises => | ||
new Promise(function(resolve, reject, errors = []) { | ||
promises.map(promise => | ||
promise.then(resolve).catch(err => { | ||
errors.push(err); | ||
if (errors.length === promises.length) { | ||
reject(errors); | ||
} | ||
}) | ||
); | ||
}); | ||
module.exports = {}; |
@@ -6,2 +6,8 @@ const mixCompose = (chain, func) => (chain instanceof Promise || typeof chain.then === "function") ? chain.then(func) : func(chain); | ||
module.exports = {pipe, compose}; | ||
const curry = f => { | ||
return function currify() { | ||
const args = Array.prototype.slice.call(arguments); | ||
return args.length >= f.length ? f.apply(null, args) : currify.bind(null, ...args) | ||
} | ||
} | ||
module.exports = {pipe, compose, curry}; |
module.exports = { | ||
...require('./fp'), | ||
...require('./concurrency'), | ||
...require('./collections') | ||
...require('./fp'), | ||
...require('./concurrency'), | ||
...require('./collections'), | ||
...require('./logic'), | ||
...require('./conversions') | ||
}; |
{ | ||
"name": "keyu", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Key utilities for everyday use", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"vet": "./node_modules/.bin/eslint ./", | ||
"ci": "./node_modules/.bin/eslint --fix ./ && mocha", | ||
"test": "nyc mocha" | ||
}, | ||
@@ -16,3 +18,18 @@ "keywords": [ | ||
"repository": "github:nerac/keyu", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.1.0", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
"husky": "^1.3.1", | ||
"mocha": "^6.0.2", | ||
"nyc": "^13.3.0", | ||
"prettier": "1.16.4", | ||
"pretty-quick": "^1.10.0" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "npm run ci && pretty-quick --staged" | ||
} | ||
} | ||
} |
# Keyu | ||
**Key u**tilities you will need for any complex javascript project. | ||
[![Build Status](https://travis-ci.org/nerac/keyu.svg?branch=master)](https://travis-ci.org/nerac/keyu) | ||
[![install size](https://packagephobia.now.sh/badge?p=keyu)](https://packagephobia.now.sh/result?p=keyu) | ||
[![Known Vulnerabilities](https://snyk.io/test/npm/keyu/badge.svg)](https://snyk.io/test/npm/keyu) | ||
[![NPM](https://nodei.co/npm/keyu.png)](https://nodei.co/npm/keyu/) | ||
**Key u**tilities that you mis when you develop a javascript project. | ||
## Utilities | ||
@@ -11,3 +17,3 @@ | ||
Composes N functions into another one, also accept functions that a return Promise. | ||
Compose N functions, works with promises too. | ||
@@ -27,5 +33,6 @@ ``` | ||
``` | ||
#### Pipe | ||
Pipe N functions into another one, also accept functions that a return Promise. | ||
Compose N functions in reverse order, works promises too. | ||
@@ -50,3 +57,4 @@ ``` | ||
Maps over every key of an object | ||
Maps an object based on the mapping function passed. | ||
``` | ||
@@ -60,2 +68,3 @@ let obj = {a:1,b:2,c:3}; | ||
functional programmming implementation: | ||
``` | ||
@@ -72,3 +81,4 @@ const {map,compose} = require('keyu'); | ||
Filters an object for each single key | ||
Filters an object based on the filter function passed. | ||
``` | ||
@@ -95,6 +105,5 @@ let obj = {a:1,b:2,c:3}; | ||
#### Any | ||
#### Promise.any | ||
Promise.all follows a fail-fast pattern meaning that if one single promise fails it stops returning the failure. | ||
This implements Promise.any which will return the result of all promises independenly if they fail or not. | ||
Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing. | ||
@@ -104,1 +113,51 @@ ``` | ||
``` | ||
#### Promise.best | ||
Promise.best execute an array of promises in parallel and returns the first one to be sucessful. | ||
``` | ||
Promise.best([Promise.reject(1),Promise.resolve(2), Promise.resolve(3)]).then(console.log) // 2 | ||
Promise.best([Promise.reject(1),Promise.reject(2), Promise.reject(3)]).catch(console.log) // [1,2,3] | ||
``` | ||
### Conversions | ||
#### jsonOr | ||
Converts safetly any input into a json, if it fails returns the default function value. | ||
``` | ||
jsonOr(33)('{"a":1}') // -> {a:1} | ||
jsonOr(33)(null) // -> 33 | ||
jsonOr(value => 33)(null) // -> 33 | ||
``` | ||
#### floatOr | ||
Converts safetly any input into a float, if it fails returns the default function value. | ||
``` | ||
floatOr(33)('11.33') // -> 11.33 | ||
floatOr(33)(null) // -> 33 | ||
floatOr(value => 33)(null) // -> 33 | ||
``` | ||
#### intOr | ||
Converts safetly any input into an integer, if it fails returns the default function value. | ||
``` | ||
intOr(33)('11') // -> 11 | ||
intOr(33)(null) // -> 33 | ||
intOr(value => 33)(null) // -> 33 | ||
``` | ||
#### setPrecision | ||
Set the amount of decimals for any given float safelty or returns cero precision. | ||
``` | ||
setPrecision(3,'1.1234') // -> 1.234 | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17113
18
305
157
8
1