feat(@vates/predicates): utils to compose predicates
This commit is contained in:
parent
ecb66fb9f3
commit
c28fa78963
1
@vates/predicates/.npmignore
Symbolic link
1
@vates/predicates/.npmignore
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../scripts/npmignore
|
90
@vates/predicates/README.md
Normal file
90
@vates/predicates/README.md
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||||
|
|
||||||
|
# @vates/predicates
|
||||||
|
|
||||||
|
[](https://npmjs.org/package/@vates/predicates)  [](https://bundlephobia.com/result?p=@vates/predicates) [](https://npmjs.org/package/@vates/predicates)
|
||||||
|
|
||||||
|
> Utilities to compose predicates
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Installation of the [npm package](https://npmjs.org/package/@vates/predicates):
|
||||||
|
|
||||||
|
```
|
||||||
|
> npm install --save @vates/predicates
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
`undefined` predicates are ignored and `undefined` is returned if all predicates are `undefined`, this permits the most efficient composition:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const compositePredicate = every(undefined, some(predicate2, undefined))
|
||||||
|
|
||||||
|
// ends up as
|
||||||
|
|
||||||
|
const compositePredicate = predicate2
|
||||||
|
```
|
||||||
|
|
||||||
|
Predicates can also be passed wrapped in an array:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const compositePredicate = every([predicate1, some([predicate2, predicate3])])
|
||||||
|
```
|
||||||
|
|
||||||
|
`this` and all arguments are passed to the nested predicates.
|
||||||
|
|
||||||
|
### `every(predicates)`
|
||||||
|
|
||||||
|
> Returns a predicate that returns `true` iff every predicate returns `true`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const isBetween3And7 = every(
|
||||||
|
n => n >= 3,
|
||||||
|
n => n <= 7
|
||||||
|
)
|
||||||
|
|
||||||
|
isBetween3And10(0)
|
||||||
|
// → false
|
||||||
|
|
||||||
|
isBetween3And10(5)
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isBetween3And10(10)
|
||||||
|
// → false
|
||||||
|
```
|
||||||
|
|
||||||
|
### `some(predicates)`
|
||||||
|
|
||||||
|
> Returns a predicate that returns `true` iff some predicate returns `true`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const isAliceOrBob = some(
|
||||||
|
name => name === 'Alice',
|
||||||
|
name => name === 'Bob'
|
||||||
|
)
|
||||||
|
|
||||||
|
isAliceOrBob('Alice')
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isAliceOrBob('Bob')
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isAliceOrBob('Oscar')
|
||||||
|
// → false
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributions
|
||||||
|
|
||||||
|
Contributions are _very_ welcomed, either on the documentation or on
|
||||||
|
the code.
|
||||||
|
|
||||||
|
You may:
|
||||||
|
|
||||||
|
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
|
||||||
|
you've encountered;
|
||||||
|
- fork and create a pull request.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)
|
57
@vates/predicates/USAGE.md
Normal file
57
@vates/predicates/USAGE.md
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
`undefined` predicates are ignored and `undefined` is returned if all predicates are `undefined`, this permits the most efficient composition:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const compositePredicate = every(undefined, some(predicate2, undefined))
|
||||||
|
|
||||||
|
// ends up as
|
||||||
|
|
||||||
|
const compositePredicate = predicate2
|
||||||
|
```
|
||||||
|
|
||||||
|
Predicates can also be passed wrapped in an array:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const compositePredicate = every([predicate1, some([predicate2, predicate3])])
|
||||||
|
```
|
||||||
|
|
||||||
|
`this` and all arguments are passed to the nested predicates.
|
||||||
|
|
||||||
|
### `every(predicates)`
|
||||||
|
|
||||||
|
> Returns a predicate that returns `true` iff every predicate returns `true`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const isBetween3And7 = every(
|
||||||
|
n => n >= 3,
|
||||||
|
n => n <= 7
|
||||||
|
)
|
||||||
|
|
||||||
|
isBetween3And10(0)
|
||||||
|
// → false
|
||||||
|
|
||||||
|
isBetween3And10(5)
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isBetween3And10(10)
|
||||||
|
// → false
|
||||||
|
```
|
||||||
|
|
||||||
|
### `some(predicates)`
|
||||||
|
|
||||||
|
> Returns a predicate that returns `true` iff some predicate returns `true`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const isAliceOrBob = some(
|
||||||
|
name => name === 'Alice',
|
||||||
|
name => name === 'Bob'
|
||||||
|
)
|
||||||
|
|
||||||
|
isAliceOrBob('Alice')
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isAliceOrBob('Bob')
|
||||||
|
// → true
|
||||||
|
|
||||||
|
isAliceOrBob('Oscar')
|
||||||
|
// → false
|
||||||
|
```
|
69
@vates/predicates/index.js
Normal file
69
@vates/predicates/index.js
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
const {
|
||||||
|
isArray,
|
||||||
|
prototype: { filter },
|
||||||
|
} = Array
|
||||||
|
|
||||||
|
class InvalidPredicate extends TypeError {
|
||||||
|
constructor(value) {
|
||||||
|
super('not a valid predicate')
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDefinedPredicate(value) {
|
||||||
|
if (value === undefined) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof value !== 'function') {
|
||||||
|
throw new InvalidPredicate(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleArgs() {
|
||||||
|
let predicates
|
||||||
|
if (!(arguments.length === 1 && isArray((predicates = arguments[0])))) {
|
||||||
|
predicates = arguments
|
||||||
|
}
|
||||||
|
return filter.call(predicates, isDefinedPredicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.every = function every() {
|
||||||
|
const predicates = handleArgs.apply(this, arguments)
|
||||||
|
const n = predicates.length
|
||||||
|
if (n === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (n === 1) {
|
||||||
|
return predicates[0]
|
||||||
|
}
|
||||||
|
return function everyPredicate() {
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
if (!predicates[0].apply(this, arguments)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.some = function some() {
|
||||||
|
const predicates = handleArgs.apply(this, arguments)
|
||||||
|
const n = predicates.length
|
||||||
|
if (n === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (n === 1) {
|
||||||
|
return predicates[0]
|
||||||
|
}
|
||||||
|
return function somePredicate() {
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
if (predicates[0].apply(this, arguments)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
36
@vates/predicates/package.json
Normal file
36
@vates/predicates/package.json
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"private": false,
|
||||||
|
"name": "@vates/predicates",
|
||||||
|
"description": "Utilities to compose predicates",
|
||||||
|
"keywords": [
|
||||||
|
"and",
|
||||||
|
"combine",
|
||||||
|
"compose",
|
||||||
|
"every",
|
||||||
|
"function",
|
||||||
|
"functions",
|
||||||
|
"or",
|
||||||
|
"predicate",
|
||||||
|
"predicates",
|
||||||
|
"some"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/predicates",
|
||||||
|
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||||
|
"repository": {
|
||||||
|
"directory": "@vates/predicates",
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||||
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "Vates SAS",
|
||||||
|
"url": "https://vates.fr"
|
||||||
|
},
|
||||||
|
"license": "ISC",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"postversion": "npm publish --access public"
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,7 @@
|
|||||||
>
|
>
|
||||||
> In case of conflict, the highest (lowest in previous list) `$version` wins.
|
> In case of conflict, the highest (lowest in previous list) `$version` wins.
|
||||||
|
|
||||||
|
- @vates/predicates major
|
||||||
- @xen-orchestra/mixins minor
|
- @xen-orchestra/mixins minor
|
||||||
- @xen-orchestra/backups patch
|
- @xen-orchestra/backups patch
|
||||||
- @xen-orchestra/proxy patch
|
- @xen-orchestra/proxy patch
|
||||||
|
Loading…
Reference in New Issue
Block a user