1 | /*!
|
2 | * Copyright (c) 2017-2018 by The Funfix Project Developers.
|
3 | * Some rights reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | /**
|
18 | * Given a function, converts it into a method where `this` gets
|
19 | * passed around as the last argument.
|
20 | */
|
21 | export function convertToMethod(f) {
|
22 | return function () {
|
23 | const args = Array.prototype.slice.call(arguments);
|
24 | args.push(this);
|
25 | return f.apply(undefined, args);
|
26 | };
|
27 | }
|
28 | /**
|
29 | * Given a constructor, searches for all Fantasy-Land compatible
|
30 | * methods registered on its `prototype` and also registers them
|
31 | * using Fantasy-Land compatible symbols.
|
32 | *
|
33 | * For example:
|
34 | *
|
35 | * ```typescript
|
36 | * class Box<A> {
|
37 | * constructor(public readonly value: A) {}
|
38 | *
|
39 | * // Setoid
|
40 | * equals(that: Box<A>) {
|
41 | * return that && this.value === that.value
|
42 | * }
|
43 | * // Functor
|
44 | * map<B>(f: (a: A) => B): Box<B> {
|
45 | * return new Box(f(this.value))
|
46 | * }
|
47 | * }
|
48 | *
|
49 | * // Registering Fantasy-Land compatible symbols
|
50 | * fantasyLandRegister(Box)
|
51 | * ```
|
52 | *
|
53 | * The above registration call would make `fantasy-land/equals` and
|
54 | * `fantasy-land/functor` available on `Box.prototype`.
|
55 | *
|
56 | * @private
|
57 | * @Hidden
|
58 | */
|
59 | export function fantasyLandRegister(cls, monad, setoid) {
|
60 | const c = cls;
|
61 | const p = c.prototype;
|
62 | const fl = "fantasy-land/";
|
63 | const equals = "equals";
|
64 | const flEquals = fl + equals;
|
65 | const map = "map";
|
66 | const flMap = fl + map;
|
67 | const ap = "ap";
|
68 | const flAp = fl + ap;
|
69 | const flOf = fl + "of";
|
70 | const chain = "chain";
|
71 | const flChain = fl + chain;
|
72 | const chainRec = "chainRec";
|
73 | const flChainRec = fl + chainRec;
|
74 | // Setoid
|
75 | if (p[equals]) {
|
76 | p[flEquals] = p[equals];
|
77 | }
|
78 | else {
|
79 | /* istanbul ignore else */
|
80 | if (setoid)
|
81 | p[flEquals] = convertToMethod(setoid.equals);
|
82 | }
|
83 | // Functor
|
84 | if (p[map]) {
|
85 | p[flMap] = p[map];
|
86 | }
|
87 | else {
|
88 | /* istanbul ignore else */
|
89 | if (monad)
|
90 | p[flMap] = convertToMethod(monad.map);
|
91 | }
|
92 | // Apply
|
93 | if (p[ap]) {
|
94 | p[flAp] = p[ap];
|
95 | }
|
96 | else {
|
97 | /* istanbul ignore else */
|
98 | if (monad)
|
99 | p[flAp] = convertToMethod(monad.ap);
|
100 | }
|
101 | // Applicative
|
102 | if (c["pure"]) {
|
103 | c[flOf] = c["pure"];
|
104 | }
|
105 | else {
|
106 | /* istanbul ignore else */
|
107 | if (monad)
|
108 | c[flOf] = monad.of;
|
109 | }
|
110 | // Chain
|
111 | if (p[chain]) {
|
112 | p[flChain] = p[chain];
|
113 | }
|
114 | else {
|
115 | /* istanbul ignore else */
|
116 | if (monad)
|
117 | p[flChain] = convertToMethod(monad.chain);
|
118 | }
|
119 | // ChainRec
|
120 | if (c[chainRec]) {
|
121 | c[flChainRec] = c[chainRec];
|
122 | }
|
123 | else {
|
124 | /* istanbul ignore else */
|
125 | if (monad)
|
126 | c[flChainRec] = monad.chainRec;
|
127 | }
|
128 | }
|
129 | //# sourceMappingURL=internals.js.map |
\ | No newline at end of file |