-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnot.js
72 lines (67 loc) · 2.1 KB
/
not.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const areAnyValuesPromises = require('./_internal/areAnyValuesPromises')
const isPromise = require('./_internal/isPromise')
const promiseAll = require('./_internal/promiseAll')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const curryArgs2 = require('./_internal/curryArgs2')
// negate(value boolean) -> inverse boolean
const negate = value => !value
// _not(args Array, predicate function)
const _not = function (args, predicate) {
const boolean = predicate(...args)
return isPromise(boolean) ? boolean.then(negate) : !boolean
}
/**
* @name not
*
* @synopsis
* ```coffeescript [specscript]
* not(value boolean) -> negated boolean
*
* not(...args, predicate function) -> negated boolean
*
* not(predicate function)(...args) -> negated boolean
* ```
*
* @description
* Negate a value like the [logical NOT (`!`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT) operator.
*
* ```javascript [playground]
* const myObj = { a: 1 }
*
* console.log(not('a' in myObj)) // false
* console.log(not('b' in myObj)) // true
* ```
*
* If provided a predicate function, `not` returns a logically inverted predicate that returns true everywhere the original predicate would have returned false.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* console.log(
* not(isOdd)(3),
* ) // false
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* not(Promise.resolve(3), isOdd).then(console.log) // false
* ```
*/
const not = function (...args) {
const predicateOrValue = args.pop()
if (typeof predicateOrValue == 'function') {
if (args.length == 0) {
return curryArgs2(_not, __, predicateOrValue)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(_not, __, predicateOrValue))
}
return _not(args, predicateOrValue)
}
return !predicateOrValue
}
module.exports = not