Skip to content

Commit

Permalink
Merge 9579e59 into 0375973
Browse files Browse the repository at this point in the history
  • Loading branch information
nerac committed Jun 17, 2019
2 parents 0375973 + 9579e59 commit 90ff6d8
Show file tree
Hide file tree
Showing 51 changed files with 5,010 additions and 1,545 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/
docs/
83 changes: 81 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = {
rules: {
'no-async-promise-executor': 'error',
'no-await-in-loop': 'warn',
'no-extra-parens': 'warn',
'no-template-curly-in-string': 'error',
'require-atomic-updates': 'error',
'array-callback-return': 'error',
Expand All @@ -25,7 +24,87 @@ module.exports = {
curly: 'warn',
'dot-notation': 'warn',
eqeqeq: 'error',
'no-undef': 'off',
'no-alert': 'error',
'no-caller': 'error',
'no-else-return': 'error',
'no-empty-function': 'error',
'no-eval': 'error',
'no-extend-native': 'error',
'no-extra-bind': 'error',
'no-floating-decimal': 'error',
'no-implicit-coercion': 'error',
'no-implied-eval': 'error',
'no-invalid-this': 'error',
'no-labels': 'error',
'no-lone-blocks': 'error',
'no-loop-func': 'error',
'no-multi-spaces': 'warn',
'no-multi-str': 'error',
'no-new': 'error',
'no-new-func': 'error',
'no-new-wrappers': 'error',
'no-param-reassign': 'error',
'no-proto': 'error',
'no-return-assign': 'error',
'no-return-await': 'warn',
'no-self-compare': 'error',
'no-sequences': 'error',
'no-throw-literal': 'error',
'no-unmodified-loop-condition': 'error',
'no-unused-expressions': 'error',
'no-useless-call': 'error',
'no-useless-catch': 'error',
'no-useless-concat': 'error',
'no-useless-return': 'error',
'no-void': 'error',
'no-with': 'error',
'prefer-promise-reject-errors': 'error',
radix: 'error',
'vars-on-top': 'error',
'wrap-iife': 'error',
yoda: 'error',
'no-shadow': 'error',
'no-shadow-restricted-names': 'error',
'no-undef-init': 'error',
'no-undefined': 'error',
'no-undef': 'error',
'global-require': 'error',
'handle-callback-err': 'error',
'no-buffer-constructor': 'error',
'no-mixed-requires': 'error',
'no-new-require': 'error',
'no-path-concat': 'error',
'no-process-env': 'error',
'no-process-exit': 'warn',
'no-sync': 'error',
'arrow-body-style': ['error', 'as-needed'],
'arrow-spacing': 'error',
'no-confusing-arrow': ['error', { allowParens: true }],
'generator-star-spacing': ['error', { before: true, after: false }],
'no-duplicate-imports': 'error',
'no-useless-computed-key': 'error',
'no-useless-constructor': 'error',
'no-useless-rename': 'error',
'no-var': 'error',
'prefer-arrow-callback': ['error', { allowNamedFunctions: true, allowUnboundThis: true }],
'prefer-const': 'error',
'prefer-destructuring': ['error', { array: true, object: true }, { enforceForRenamedProperties: true }],
'prefer-numeric-literals': 'error',
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'rest-spread-spacing': 'error',
'sort-imports': 'error',
'template-curly-spacing': 'error',
'require-await': 'error',
'yield-star-spacing': ['error', 'after'],
//Styling
'array-bracket-newline': ['warn', 'consistent'],
'array-bracket-spacing': ['warn', 'never'],
'array-element-newline': ['warn', 'consistent'],
'block-spacing': 'warn',
'brace-style': 'warn',
'capitalized-comments': 'warn',
'no-console': 'off'
}
};
16 changes: 0 additions & 16 deletions .eslintrc.json

This file was deleted.

11 changes: 11 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Breaking changes
- Changes signature `setPrecision` to `setPrecisionOr`

## New features
- Adds `IsNumber`: returns true if it is a real number.

## Improvements
- Improves `parseOr`
- **Bug** Fixes `floatOr` ...

## Minor things
42 changes: 42 additions & 0 deletions .jsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

module.exports = {
opts: {
readme: './README.md',
package: './package.json',
template: './node_modules/jsdoc-baseline',
recurse: true,
verbose: true,
destination: './docs/'
},
plugins: ['plugins/markdown'],
source: {
excludePattern: '(^|\\/|\\\\)[._]',
include: [],
exclude: ['node_modules', 'docs', 'test'],
includePattern: '\\.js$'
},
templates: {
copyright: 'Copyright 2018 Google, LLC.',
includeDate: false,
sourceFiles: false,
systemName: '@google-cloud/error-reporting',
theme: 'lumen'
}
};
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/
194 changes: 15 additions & 179 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,191 +5,27 @@
[![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/)

**Keyu** (key utilities) provides **flexible** utilities to help you build **solid** projects <u>without repeating yourself</u>.
This library pretends to **fix** those **anoying things** that you find every day **when you write JavaScript**

With this utilities you will be able to:

- Build some functional programming code.
- Work with asyncronous code like if it was syncronous\*.
- Iterate objects in same way you iterate arrays.
- Apply some advanced concurrency patterns ([Rob Pike](https://www.youtube.com/watch?v=f6kdp27TYZs)).
- Convert integers, floats or json correctly and safetly.

<small>\*compose functions independently if they return promises or actual values</small>

## Utilities

### Functional programming

#### Compose

Compose N functions, works with promises too.

```
const {compose} = require('keyu');
const sum1 = x => x+1;
const mult2 = x => x*2;
const dbSum = x => Promise.resolve(x+1);
const sumAndMult = compose(mult2,sum1);
const sumAndMultDB = compose(dbSum,mult2,sum1);
sumAndMult(1) // 4
sumAndMultDB(1) // Promise(5)
```

#### Pipe

Compose N functions in reverse order, works promises too.

```
const {pipe} = require('keyu');
const sum1 = x => x+1;
const mult2 = x => x*2;
const dbSum = x => Promise.resolve(x+1);
const sumAndMult = pipe(sum1,mult2);
const sumAndMultDB = pipe(sum1, mult2, dbSum);
sumAndMult(1) // 4
sumAndMultDB(1) // Promise(5)
```

### Objects

#### Map

Maps an object based on the mapping function passed.

```
let obj = {a:1,b:2,c:3};
console.log(obj.map(value => value+1)) // {a:2}
console.log(obj.map((value, key) => `${key}:${value+1}`)) // {a:'a:2'}
```

functional programmming implementation:

```
const {map,compose} = require('keyu');
const sum1 = compose(map(x => x+1))
sum1([1]) // [2]
sum1({a:1}) // {a:2}
```

#### Filter

Filters an object based on the filter function passed.

```
let obj = {a:1,b:2,c:3};
console.log(obj.filter(value => value > 2)) // {c:3}
console.log(obj.filter((value, key) => value > 1 && key === 'c')) // {c:3}
```

functional programmming implementation:

```
const {filter,compose} = require('keyu');
const greaterThan5 = compose(filter(x => x > 5 ))
greaterThan5([1,2,3,4,5,6,7,8]) // [6,7,8]
greaterThan5({a:1,b:2,c:3,d:4,e:5,f:6,g:7}) // {f:6,g:7}
```

#### Reduce

### Concurrency

#### Promise.any

Promise.all executes an array of promises in parallel and returns all of them either they are sucessful or not without failing.

```
Promise.any([Promise.resolve(1),Promise.reject(2), Promise.resolve(3)]).then(console.log) // [ { value: 1 }, { error: 2 }, { value: 3 } ]
```

#### 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
```

### Logic

#### Either

Is a try/catch in a functional way, it will try to execute the first function and if this one throws an exception will return the value of the second one.

```
let parser = either(JSON.parse,33);
parser("") // => 33
parser('{}') // => {}
The library helps you:

```
- Write functional programming code in javascript
- Combine sync and async functions easly
- Do safe conversions
- Check javascript types correctly
- Work with any kind of collection
- Apply complex concurrency patterns ([Rob Pike](https://www.youtube.com/watch?v=f6kdp27TYZs)).

#### fnOrValue
Documentation: https://nerac.github.io/keyu/

Detects if the value passed is a function or a value and returns the function applied to the value or the value itself.
Repository: https://github.com/nerac/keyu

### Identity
License: [MIT](https://github.com/nerac/keyu/blob/master/LICENSE)

Function that given the same input returns the same output.
## Contribute

### Not
If you find yourself repeating over and over the same function maybe it should be here.

Negates any function that recieves
This repository is completely open to any kind of contribution that will simplify Javascript
developers time.

0 comments on commit 90ff6d8

Please sign in to comment.