1 | ## FPJS
|
2 |
|
3 | A Functional tool set for my own learning FP style.
|
4 | Implemented by referring many other's source.
|
5 |
|
6 | Introduced the beginning of this repository in [Medium post](https://medium.com/@andrwj/either-implementation-that-is-throwable-and-catchable-5b0015f4b6e3)
|
7 |
|
8 |
|
9 | ## Installation
|
10 |
|
11 | `npm install @andrwj/fp`
|
12 |
|
13 |
|
14 | ## Usage (Not fully documented yet)
|
15 | * In command line:
|
16 | `node -r esm --harmony ./index.js`
|
17 |
|
18 | * In ReactJS/VueJS/RunJS: `import * as FP from '@andrwj/fp'`
|
19 |
|
20 | ## Either
|
21 | ### static methods
|
22 | * `Either.of(value, f)` - `f` is optional
|
23 | * `Either.fromNullable(value)`
|
24 | * `Either.filter(f, condition_f=truth)` - `condition_f` is optional
|
25 | * `Either.right(value)`
|
26 | * `Either.left(value)`
|
27 | * `Either.done(value)`
|
28 | * `Either.doneIf(f, value)`
|
29 | * `Either.throw(value)` - this throws `Right(value)` not Error instnace !!! and then only .`catch()` handle the passed `Right`
|
30 | * `Either.throwIf(f, value)`
|
31 | * `Either.try(f)`
|
32 |
|
33 | ### instance methods for Right
|
34 | * `.inspect(f)`
|
35 | * `.try(f)`
|
36 | * `.throw()`
|
37 | * `.tap(f=console.log)`
|
38 | * `.take(f)`
|
39 | * `.map(f)`
|
40 | * `.chain(f)`
|
41 | * `.filter(f)`
|
42 | * `.fold(_right, _left)`
|
43 | * `.done()`
|
44 | * `.doneIf(f)`
|
45 | * `.throwIf(f, value)`
|
46 | * `.apply( instance_of_Either )`
|
47 |
|
48 | ### instance methods for Left
|
49 | * `.inspect(f)`
|
50 | * `.tap(f=console.log)`
|
51 | * `.take(f)`
|
52 | * `.fold(_right, _left)`
|
53 |
|
54 | ### NOTE
|
55 | * All methods work for Right only.
|
56 | * Only Right can `.throw()` or `.done()`
|
57 | * Basically Left will skip most methods except `inspect, tap, take, fold`
|
58 | * `throw()` does not throw Error. It is deliver of `Right` to `.catch()`.
|
59 | * If you `throw()`, execution flow goes until find `.catch()`
|
60 | * If you `done()`, execution flow goes until find `.fold()` or `.take()`
|
61 | * If there is no exception in `try(f)`, return value will be of **Right**. If Exception occurs, then the return value will be of **Left**.
|
62 | * If you do not want to get **Unhandled Promise Rejection Error** in `.try(f)`, you HAVE TO deal with its logic of `f` yourself. The unhandled error is not because of `.try()` but because of your misunderstanding of Promise chain.
|
63 |
|
64 | ## Fromise (Experimental; Promimse + Either)
|
65 | * `Fromise.resolve (expr)`
|
66 | * `Fromise.reject (expr)`
|
67 | * `Fromise.isPromise (value)`
|
68 | * `.pipe (...funcs)`
|
69 | * `.if (f_condition, if_true, if_false)`
|
70 | * `.tap (f)`
|
71 | * `.either (f)`
|
72 | * `.map (f)`
|
73 | * `.fold (f, g)`
|
74 | * `.chain (f)`
|
75 |
|
76 | ## High Order Functions (Not fully documented yet)
|
77 | * `run(v, f1, f2, f3, ... ) : value`
|
78 | * `pipe(f1, f2, f3, ...) : function`
|
79 |
|
80 | ## Utilities (Not fully documented yet)
|
81 | * `isFunction (v)` - return true if `v` is function
|
82 | * `isFalsy (v)` - true if `v` is one of `undefined`, `NaN`, `null`
|
83 | * `isFalse (v)` - true if `v` is one of `undefined`, `NaN`, `null`, `0`, `""`
|
84 | * `identity (v)` - returns `v`, always.
|
85 |
|
86 | ## Changes
|
87 |
|
88 | ### `v0.2.4` Published compiled source by Babel7
|
89 |
|
90 | ### `v0.2.0` break changes to version of `0.1.*`
|
91 | * changed arguments order and way: `Either.of(f, value)` ➡ `Either.of(value, f)` - if `f` is not given, it just returns `Right(value)`. Otherwise, it returns `Right(value)` or `Left(value)` depends on the boolean result of `f(value)`
|
92 | * changed arguments: `.catch(f, condition_f)` ➡ `.catch(f)` - if `f` is not given, it returns the instance of thrown from `.throw()`. Otherwise it returns `.map(f)`
|
93 | * added: `.of(value, f)` - same as `Either.of(value, f)`
|
94 | * added: `isNotFalsy(v) : boolean `
|