forked from a-synchronous/rubico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
not.js
73 lines (68 loc) · 1.27 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
73
const isPromise = require('./_internal/isPromise')
// true -> false
const _not = value => !value
/**
* @name not
*
* @synopsis
* ```coffeescript [specscript]
* var args ...any,
* predicate ...args=>Promise|boolean
*
* not(predicate)(...args) -> boolean
* ```
*
* @description
* Negate a predicate (`!`)
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* console.log(
* not(isOdd)(3),
* ) // false
* ```
*
* @TODO
* const not = funcNot
* funcNotSync
*/
const not = func => function logicalInverter(...args) {
const boolean = func(...args)
return isPromise(boolean) ? boolean.then(_not) : !boolean
}
/**
* @name notSync
*
* @synopsis
* ```coffeescript [specscript]
* notSync(func ...any=>boolean) -> negated ...any=>boolean
* ```
*/
const notSync = func => function notSync(...args) {
return !func(...args)
}
/**
* @name not.sync
*
* @synopsis
* ```coffeescript [specscript]
* var args ...any,
* syncPredicate ...args=>boolean
*
* not.sync(syncPredicate)(...args) -> boolean
* ```
*
* @description
* `not` without promise handling.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* console.log(
* not.sync(isOdd)(2),
* ) // true
* ```
*/
not.sync = notSync
module.exports = not