UNPKG

4.54 kBTypeScriptView Raw
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 */
17import { Setoid } from "funland";
18/**
19 * Interface for testing the equality of value objects.
20 */
21export interface IEquals<A> {
22 /**
23 * Indicates whether some other object is "equal to" this one.
24 *
25 * Properties:
26 *
27 * - reflexive: for any value, `x.equals(x) == true`
28 * - symmetric: for any values x and y, `x.equals(y) == y.equals(x)`
29 * - transitive: `x.equals(y) && y.equals(z) => x.equals(z)`
30 * - consistent: `x.equals(y)` always yields the same result
31 *
32 * Rule: equal objects MUST have equal hash codes!
33 */
34 equals(other: A): boolean;
35 /**
36 * Returns a hash code value for this value.
37 *
38 * This method is supported for the benefit of hash tables.
39 *
40 * Properties:
41 *
42 * - consistent: multiple invocations always yield the same result
43 * - if `x.equals(y) == true` then `x.hashCode() == y.hashCode()`
44 * - if `x.equals(y) == false` it is NOT required for their hash codes
45 * to be equal, i.e. this function is not injective
46 */
47 hashCode(): number;
48}
49/**
50 * Test if the given reference is a value object.
51 *
52 * Value objects are objects that implement the [[IEquals]]
53 * interface.
54 *
55 * @param ref is the reference to test
56 */
57export declare function isValueObject(ref: any): boolean;
58/**
59 * Tests for universal equality.
60 *
61 * First attempting a reference check with `===`,
62 * after which it tries to fallback on [[IEquals]], if the
63 * left-hand side is implementing it.
64 *
65 * ```typescript
66 * equals(10, 10) // true, because 10 === 10
67 *
68 * class Box implements IEquals<Box> {
69 * constructor(value: number) { this.value = value }
70 *
71 * equals(other) { return this.value === other.value }
72 * hashCode() { return this.value << 2 }
73 * }
74 *
75 * // false, because they are not the same reference
76 * new Box(10) === new Box(10)
77 *
78 * // true, because `Box#equals` gets called
79 * equals(new Box(10), new Box(10))
80 * ```
81 */
82export declare function is<A>(lh: A, rh: A): boolean;
83/** Alias for [[is]]. */
84export declare function equals<A>(lh: A, rh: A): boolean;
85/**
86 * Returns a `Setoid` type-class instance that depends
87 * universal equality, as defined by {@link is}.
88 */
89export declare const universalSetoid: Setoid<any>;
90/**
91 * Universal hash-code function.
92 *
93 * Depending on the given value, it calculates the hash-code like so:
94 *
95 * 1. if it's a `number`, then it gets truncated
96 * to an integer and returned
97 * 2. if it's a "value object" (see [[isValueObject]]), then
98 * its `hashCode` is used
99 * 3. if a `valueOf()` function is provided, then the
100 * `hashCode` gets recursively invoked on its result
101 * 4. if all else fails, the value gets coerced to a `String`
102 * and a hash code is calculated using [[hashCodeOfString]]
103 *
104 * @param ref is the value to use for calculating a hash code
105 * @return an integer with the aforementioned properties
106 */
107export declare function hashCode(ref: any): number;
108/**
109 * Calculates a hash code out of any string.
110 */
111export declare function hashCodeOfString(str: string): number;
112/** The identity function. */
113export declare function id<A>(a: A): A;
114/**
115 * Utility function for implementing mixins, based on the
116 * [TypeScript Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html}
117 * documentation.
118 *
119 * Sample:
120 *
121 * ```typescript
122 * class Disposable { ... }
123 * class Activatable { ... }
124 * class SmartObject implements Disposable, Activatable { ... }
125 *
126 * applyMixins(SmartObject, [Disposable, Activatable]);
127 * ```
128 *
129 * Using `implements` instead of `extends` for base classes
130 * will make the type system treat them like interfaces instead of
131 * classes. And by `applyMixins` we can also supply global
132 * implementations for the non-abstract members.
133 */
134export declare function applyMixins(derivedCtor: {
135 prototype: any;
136}, baseCtors: {
137 prototype: any;
138}[]): void;