版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.eqDate = exports.eqNumber = exports.eqString = exports.eqBoolean = exports.eq = exports.strictEqual = exports.getStructEq = exports.getTupleEq = exports.Contravariant = exports.getMonoid = exports.getSemigroup = exports.eqStrict = exports.URI = exports.contramap = exports.tuple = exports.struct = exports.fromEquals = void 0;
  4. var function_1 = require("./function");
  5. // -------------------------------------------------------------------------------------
  6. // constructors
  7. // -------------------------------------------------------------------------------------
  8. /**
  9. * @category constructors
  10. * @since 2.0.0
  11. */
  12. var fromEquals = function (equals) { return ({
  13. equals: function (x, y) { return x === y || equals(x, y); }
  14. }); };
  15. exports.fromEquals = fromEquals;
  16. // -------------------------------------------------------------------------------------
  17. // combinators
  18. // -------------------------------------------------------------------------------------
  19. /**
  20. * @since 2.10.0
  21. */
  22. var struct = function (eqs) {
  23. return (0, exports.fromEquals)(function (first, second) {
  24. for (var key in eqs) {
  25. if (!eqs[key].equals(first[key], second[key])) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. });
  31. };
  32. exports.struct = struct;
  33. /**
  34. * Given a tuple of `Eq`s returns a `Eq` for the tuple
  35. *
  36. * @example
  37. * import { tuple } from 'fp-ts/Eq'
  38. * import * as S from 'fp-ts/string'
  39. * import * as N from 'fp-ts/number'
  40. * import * as B from 'fp-ts/boolean'
  41. *
  42. * const E = tuple(S.Eq, N.Eq, B.Eq)
  43. * assert.strictEqual(E.equals(['a', 1, true], ['a', 1, true]), true)
  44. * assert.strictEqual(E.equals(['a', 1, true], ['b', 1, true]), false)
  45. * assert.strictEqual(E.equals(['a', 1, true], ['a', 2, true]), false)
  46. * assert.strictEqual(E.equals(['a', 1, true], ['a', 1, false]), false)
  47. *
  48. * @since 2.10.0
  49. */
  50. var tuple = function () {
  51. var eqs = [];
  52. for (var _i = 0; _i < arguments.length; _i++) {
  53. eqs[_i] = arguments[_i];
  54. }
  55. return (0, exports.fromEquals)(function (first, second) { return eqs.every(function (E, i) { return E.equals(first[i], second[i]); }); });
  56. };
  57. exports.tuple = tuple;
  58. /* istanbul ignore next */
  59. var contramap_ = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.contramap)(f)); };
  60. /**
  61. * A typical use case for `contramap` would be like, given some `User` type, to construct an `Eq<User>`.
  62. *
  63. * We can do so with a function from `User -> X` where `X` is some value that we know how to compare
  64. * for equality (meaning we have an `Eq<X>`)
  65. *
  66. * For example, given the following `User` type, we want to construct an `Eq<User>` that just looks at the `key` field
  67. * for each user (since it's known to be unique).
  68. *
  69. * If we have a way of comparing `UUID`s for equality (`eqUUID: Eq<UUID>`) and we know how to go from `User -> UUID`,
  70. * using `contramap` we can do this
  71. *
  72. * @example
  73. * import { contramap, Eq } from 'fp-ts/Eq'
  74. * import { pipe } from 'fp-ts/function'
  75. * import * as S from 'fp-ts/string'
  76. *
  77. * type UUID = string
  78. *
  79. * interface User {
  80. * readonly key: UUID
  81. * readonly firstName: string
  82. * readonly lastName: string
  83. * }
  84. *
  85. * const eqUUID: Eq<UUID> = S.Eq
  86. *
  87. * const eqUserByKey: Eq<User> = pipe(
  88. * eqUUID,
  89. * contramap((user) => user.key)
  90. * )
  91. *
  92. * assert.deepStrictEqual(
  93. * eqUserByKey.equals(
  94. * { key: 'k1', firstName: 'a1', lastName: 'b1' },
  95. * { key: 'k2', firstName: 'a1', lastName: 'b1' }
  96. * ),
  97. * false
  98. * )
  99. * assert.deepStrictEqual(
  100. * eqUserByKey.equals(
  101. * { key: 'k1', firstName: 'a1', lastName: 'b1' },
  102. * { key: 'k1', firstName: 'a2', lastName: 'b1' }
  103. * ),
  104. * true
  105. * )
  106. *
  107. * @since 2.0.0
  108. */
  109. var contramap = function (f) { return function (fa) {
  110. return (0, exports.fromEquals)(function (x, y) { return fa.equals(f(x), f(y)); });
  111. }; };
  112. exports.contramap = contramap;
  113. /**
  114. * @category type lambdas
  115. * @since 2.0.0
  116. */
  117. exports.URI = 'Eq';
  118. /**
  119. * @category instances
  120. * @since 2.5.0
  121. */
  122. exports.eqStrict = {
  123. equals: function (a, b) { return a === b; }
  124. };
  125. var empty = {
  126. equals: function () { return true; }
  127. };
  128. /**
  129. * @category instances
  130. * @since 2.10.0
  131. */
  132. var getSemigroup = function () { return ({
  133. concat: function (x, y) { return (0, exports.fromEquals)(function (a, b) { return x.equals(a, b) && y.equals(a, b); }); }
  134. }); };
  135. exports.getSemigroup = getSemigroup;
  136. /**
  137. * @category instances
  138. * @since 2.6.0
  139. */
  140. var getMonoid = function () { return ({
  141. concat: (0, exports.getSemigroup)().concat,
  142. empty: empty
  143. }); };
  144. exports.getMonoid = getMonoid;
  145. /**
  146. * @category instances
  147. * @since 2.7.0
  148. */
  149. exports.Contravariant = {
  150. URI: exports.URI,
  151. contramap: contramap_
  152. };
  153. // -------------------------------------------------------------------------------------
  154. // deprecated
  155. // -------------------------------------------------------------------------------------
  156. /**
  157. * Use [`tuple`](#tuple) instead.
  158. *
  159. * @category zone of death
  160. * @since 2.0.0
  161. * @deprecated
  162. */
  163. exports.getTupleEq = exports.tuple;
  164. /**
  165. * Use [`struct`](#struct) instead.
  166. *
  167. * @category zone of death
  168. * @since 2.0.0
  169. * @deprecated
  170. */
  171. exports.getStructEq = exports.struct;
  172. /**
  173. * Use [`eqStrict`](#eqstrict) instead
  174. *
  175. * @category zone of death
  176. * @since 2.0.0
  177. * @deprecated
  178. */
  179. exports.strictEqual = exports.eqStrict.equals;
  180. /**
  181. * This instance is deprecated, use small, specific instances instead.
  182. * For example if a function needs a `Contravariant` instance, pass `E.Contravariant` instead of `E.eq`
  183. * (where `E` is from `import E from 'fp-ts/Eq'`)
  184. *
  185. * @category zone of death
  186. * @since 2.0.0
  187. * @deprecated
  188. */
  189. exports.eq = exports.Contravariant;
  190. /**
  191. * Use [`Eq`](./boolean.ts.html#eq) instead.
  192. *
  193. * @category zone of death
  194. * @since 2.0.0
  195. * @deprecated
  196. */
  197. exports.eqBoolean = exports.eqStrict;
  198. /**
  199. * Use [`Eq`](./string.ts.html#eq) instead.
  200. *
  201. * @category zone of death
  202. * @since 2.0.0
  203. * @deprecated
  204. */
  205. exports.eqString = exports.eqStrict;
  206. /**
  207. * Use [`Eq`](./number.ts.html#eq) instead.
  208. *
  209. * @category zone of death
  210. * @since 2.0.0
  211. * @deprecated
  212. */
  213. exports.eqNumber = exports.eqStrict;
  214. /**
  215. * Use [`Eq`](./Date.ts.html#eq) instead.
  216. *
  217. * @category zone of death
  218. * @since 2.0.0
  219. * @deprecated
  220. */
  221. exports.eqDate = {
  222. equals: function (first, second) { return first.valueOf() === second.valueOf(); }
  223. };