https://github.com/alvarobernalg/logical-operators
Abstracts logical operators into functions with the intention of improving code readability.
https://github.com/alvarobernalg/logical-operators
every javascript logical logical-operators nodejs operators or readability
Last synced: about 1 month ago
JSON representation
Abstracts logical operators into functions with the intention of improving code readability.
- Host: GitHub
- URL: https://github.com/alvarobernalg/logical-operators
- Owner: AlvaroBernalG
- License: mit
- Created: 2017-06-22T22:02:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-30T11:22:41.000Z (almost 9 years ago)
- Last Synced: 2025-03-09T07:02:31.858Z (over 1 year ago)
- Topics: every, javascript, logical, logical-operators, nodejs, operators, or, readability
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# logical-operators
> A tiny library that abstracts away logical operators with the intention of improving code readability. Ideal for composing complex validation queries.
[](https://travis-ci.org/AlvaroBernalG/logical-operators) [](https://badge.fury.io/js/logical-operators) [](https://standardjs.com)
## [](#i-disagree-with-rule-x-can-you-change-it)
## Install
```
$ npm install logical-operators --save
```
## Usage
### every ( && )...
```js
const { every } = require('logical-operators')
const is = require('library-that-validate-stuff')
const values = [1, 9, 7,'level', 'Venezuela', 'Caracas', 'Aba', 'hola', 10, undefined, 'USA']
const palindromeCities = every(is.string, is.palindrome, is.city)
values.filter(palindromeCities) // => ['Aba']
values.every(palindromeCities) // => false
values.some(palindromeCities) // => true
```
### some ( || ) ...
```js
const { some } = require('logical-operators')
const between = require('in-between')
const is = require('library-that-validate-stuff')
const values = ['a','b','two', 'd', 'x', 3, undefined, null,'three']
values.every(some(between('w','z'), is.number, between('a','c'))) // => false
values.filter(some(between('w','z'), is.number, between('a','c'))) // => ['b', 3, 'x']
```
### or ( || )
Same as 'some' but only accepts 2 params
```js
const { or } = require('logical-operators')
const is = require('library-that-validate-stuff')
const values = [1,2,'three']
const string_or_number = or(is.string, is.number)
values.every(string_or_number) // => true
values.filter(string_or_number) // => [1, 2, 'three']
```
### both ( && )
Same as 'every' but only accepts 2 params
```js
const { both } = require('logical-operators')
const is = require('library-that-validate-stuff')
const values = ['a','b','two']
const string_and_singleChar = both(is.string, is.singleChar)
values.every(string_and_singleChar) // => false
values.filter(string_and_singleChar) // => ['a','b']
```
### equal ( == )
```js
const { equal, greater } = require('logical-operators')
const is = require('library-that-validate-stuff')
const compose = require('lodash/compose')
var getAsciiSum = (str) => str.split('').map(char => char.charCodeAt(0)).reduce((prev, next)=> prev+ next,0)
var values = [1,2,3, 'ab', 'e', 'T', 'W', '/']
values.every(
is.string,
equal(getAsciiSum, compose(greater(102).than, toAscii) ),
) // => false
values.filter(
is.string,
equal(getAsciiSum, compose(greater(100).than, toAscii) ),
) // => ['ab', 'e']
```
## License
MIT © [Alvaro Bernal](https://github.com/AlvaroBernalG/)