index.d.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Cache used to store references to objects, used for circular
  3. * reference checks.
  4. */
  5. export interface Cache<Key extends object, Value> {
  6. delete(key: Key): boolean;
  7. get(key: Key): Value | undefined;
  8. set(key: Key, value: any): any;
  9. }
  10. export interface State<Meta> {
  11. /**
  12. * Cache used to identify circular references
  13. */
  14. readonly cache: Cache<any, any> | undefined;
  15. /**
  16. * Method used to determine equality of nested value.
  17. */
  18. readonly equals: InternalEqualityComparator<Meta>;
  19. /**
  20. * Additional value that can be used for comparisons.
  21. */
  22. meta: Meta;
  23. /**
  24. * Whether the equality comparison is strict, meaning it matches
  25. * all properties (including symbols and non-enumerable properties)
  26. * with equal shape of descriptors.
  27. */
  28. readonly strict: boolean;
  29. }
  30. export interface CircularState<Meta> extends State<Meta> {
  31. readonly cache: Cache<any, any>;
  32. }
  33. export interface DefaultState<Meta> extends State<Meta> {
  34. readonly cache: undefined;
  35. }
  36. export interface Dictionary<Value = any> {
  37. [key: string | symbol]: Value;
  38. $$typeof?: any;
  39. }
  40. export interface ComparatorConfig<Meta> {
  41. /**
  42. * Whether the arrays passed are equal in value. In strict mode, this includes
  43. * additional properties added to the array.
  44. */
  45. areArraysEqual: TypeEqualityComparator<any, Meta>;
  46. /**
  47. * Whether the dates passed are equal in value.
  48. */
  49. areDatesEqual: TypeEqualityComparator<any, Meta>;
  50. /**
  51. * Whether the errors passed are equal in value.
  52. */
  53. areErrorsEqual: TypeEqualityComparator<any, Meta>;
  54. /**
  55. * Whether the functions passed are equal in value.
  56. */
  57. areFunctionsEqual: TypeEqualityComparator<any, Meta>;
  58. /**
  59. * Whether the maps passed are equal in value. In strict mode, this includes
  60. * additional properties added to the map.
  61. */
  62. areMapsEqual: TypeEqualityComparator<any, Meta>;
  63. /**
  64. * Whether the numbers passed are equal in value.
  65. */
  66. areNumbersEqual: TypeEqualityComparator<any, Meta>;
  67. /**
  68. * Whether the objects passed are equal in value. In strict mode, this includes
  69. * non-enumerable properties added to the map, as well as symbol properties.
  70. */
  71. areObjectsEqual: TypeEqualityComparator<any, Meta>;
  72. /**
  73. * Whether the primitive wrappers passed are equal in value.
  74. */
  75. arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
  76. /**
  77. * Whether the regexps passed are equal in value.
  78. */
  79. areRegExpsEqual: TypeEqualityComparator<any, Meta>;
  80. /**
  81. * Whether the sets passed are equal in value. In strict mode, this includes
  82. * additional properties added to the set.
  83. */
  84. areSetsEqual: TypeEqualityComparator<any, Meta>;
  85. /**
  86. * Whether the typed arrays passed are equal in value. In strict mode, this includes
  87. * additional properties added to the typed array.
  88. */
  89. areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
  90. /**
  91. * Whether the URLs passed are equal in value.
  92. */
  93. areUrlsEqual: TypeEqualityComparator<any, Meta>;
  94. /**
  95. * Whether two values with unknown `@@toStringTag` are equal in value. This comparator is
  96. * called when no other comparator applies.
  97. *
  98. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
  99. */
  100. unknownTagComparators:
  101. | Record<string, TypeEqualityComparator<any, Meta>>
  102. | undefined;
  103. }
  104. export type CreateCustomComparatorConfig<Meta> = (
  105. config: ComparatorConfig<Meta>,
  106. ) => Partial<ComparatorConfig<Meta>>;
  107. export type CreateState<Meta> = () => {
  108. cache?: Cache<any, any> | undefined;
  109. meta?: Meta;
  110. };
  111. export type EqualityComparator<Meta> = <A, B>(
  112. a: A,
  113. b: B,
  114. state: State<Meta>,
  115. ) => boolean;
  116. export type AnyEqualityComparator<Meta> = (
  117. a: any,
  118. b: any,
  119. state: State<Meta>,
  120. ) => boolean;
  121. export type EqualityComparatorCreator<Meta> = (
  122. fn: EqualityComparator<Meta>,
  123. ) => InternalEqualityComparator<Meta>;
  124. export type InternalEqualityComparator<Meta> = (
  125. a: any,
  126. b: any,
  127. indexOrKeyA: any,
  128. indexOrKeyB: any,
  129. parentA: any,
  130. parentB: any,
  131. state: State<Meta>,
  132. ) => boolean;
  133. // We explicitly check for primitive wrapper types
  134. // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
  135. export type PrimitiveWrapper = Boolean | Number | String;
  136. /**
  137. * Type which encompasses possible instances of TypedArray
  138. * classes.
  139. *
  140. * **NOTE**: This does not include `BigInt64Array` and
  141. * `BitUint64Array` because those are part of ES2020 and
  142. * not supported by certain TS configurations. If using
  143. * either in `areTypedArraysEqual`, you can cast the
  144. * instance as `TypedArray` and it will work as expected,
  145. * because runtime checks will still work for those classes.
  146. */
  147. export type TypedArray =
  148. | Float32Array
  149. | Float64Array
  150. | Int8Array
  151. | Int16Array
  152. | Int32Array
  153. | Uint16Array
  154. | Uint32Array
  155. | Uint8Array
  156. | Uint8ClampedArray;
  157. export type TypeEqualityComparator<Type, Meta = undefined> = (
  158. a: Type,
  159. b: Type,
  160. state: State<Meta>,
  161. ) => boolean;
  162. export interface CustomEqualCreatorOptions<Meta> {
  163. /**
  164. * Whether circular references should be supported. It causes the
  165. * comparison to be slower, but for objects that have circular references
  166. * it is required to avoid stack overflows.
  167. */
  168. circular?: boolean;
  169. /**
  170. * Create a custom configuration of type-specific equality comparators.
  171. * This receives the default configuration, which allows either replacement
  172. * or supersetting of the default methods.
  173. */
  174. createCustomConfig?: CreateCustomComparatorConfig<Meta>;
  175. /**
  176. * Create a custom internal comparator, which is used as an override to the
  177. * default entry point for nested value equality comparisons. This is often
  178. * used for doing custom logic for specific types (such as handling a specific
  179. * class instance differently than other objects) or to incorporate `meta` in
  180. * the comparison. See the recipes for examples.
  181. */
  182. createInternalComparator?: (
  183. compare: EqualityComparator<Meta>,
  184. ) => InternalEqualityComparator<Meta>;
  185. /**
  186. * Create a custom `state` object passed between the methods. This allows for
  187. * custom `cache` and/or `meta` values to be used.
  188. */
  189. createState?: CreateState<Meta>;
  190. /**
  191. * Whether the equality comparison is strict, meaning it matches
  192. * all properties (including symbols and non-enumerable properties)
  193. * with equal shape of descriptors.
  194. */
  195. strict?: boolean;
  196. }
  197. /**
  198. * Whether the values passed are strictly equal or both NaN.
  199. */
  200. export declare const sameValueZeroEqual: <A, B>(a: A, b: B) => boolean;
  201. /**
  202. * Whether the items passed are deeply-equal in value.
  203. */
  204. export declare const deepEqual: <A, B>(a: A, b: B) => boolean;
  205. /**
  206. * Whether the items passed are deeply-equal in value based on strict comparison.
  207. */
  208. export declare const strictDeepEqual: <A, B>(a: A, b: B) => boolean;
  209. /**
  210. * Whether the items passed are deeply-equal in value, including circular references.
  211. */
  212. export declare const circularDeepEqual: <A, B>(a: A, b: B) => boolean;
  213. /**
  214. * Whether the items passed are deeply-equal in value, including circular references,
  215. * based on strict comparison.
  216. */
  217. export declare const strictCircularDeepEqual: <A, B>(a: A, b: B) => boolean;
  218. /**
  219. * Whether the items passed are shallowly-equal in value.
  220. */
  221. export declare const shallowEqual: <A, B>(a: A, b: B) => boolean;
  222. /**
  223. * Whether the items passed are shallowly-equal in value based on strict comparison
  224. */
  225. export declare const strictShallowEqual: <A, B>(a: A, b: B) => boolean;
  226. /**
  227. * Whether the items passed are shallowly-equal in value, including circular references.
  228. */
  229. export declare const circularShallowEqual: <A, B>(a: A, b: B) => boolean;
  230. /**
  231. * Whether the items passed are shallowly-equal in value, including circular references,
  232. * based on strict comparison.
  233. */
  234. export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
  235. /**
  236. * Create a custom equality comparison method.
  237. *
  238. * This can be done to create very targeted comparisons in extreme hot-path scenarios
  239. * where the standard methods are not performant enough, but can also be used to provide
  240. * support for legacy environments that cannot polyfill for modern features expected by
  241. * `fast-equals`, such as `WeakMap` or `RegExp.prototype.flags`.
  242. */
  243. export declare function createCustomEqual<Meta = undefined>(
  244. options?: CustomEqualCreatorOptions<Meta>,
  245. ): <A, B>(a: A, b: B) => boolean;