-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClass.ts
More file actions
126 lines (110 loc) · 3.08 KB
/
Class.ts
File metadata and controls
126 lines (110 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { hashCode } from "./index";
/**
* Abstract base class providing fundamental functionality for subclasses.
* Intended to be extended by other classes.
*
* @abstract
* @class Class
* @since 1.0.0
* @version 1.0.0
*/
export abstract class Class {
/**
* Getter to retrieve a string representing the class instance tag.
* Used in `Object.prototype.toString`.
*
* @returns {string} The tag string.
*/
get [Symbol.toStringTag](): string {
return this.constructor.name;
}
/**
* Creates a shallow copy of the current instance.
*
* __Note__ that the constructor is not called.
*
* @returns {this} A new instance with the same properties as the original.
*/
clone(): this {
const proto = Object.getPrototypeOf(this);
const result = Object.create(proto);
Object.assign(result, this);
return result;
}
/**
* Checks if this object is equivalent to another.
*
* @param {any} input The object to compare.
* @returns {boolean}
* - `true` if the objects are equivalent,
* - `false` otherwise.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
equals(input: any): boolean {
if (this === input) {
return true;
}
if (input === null || this.constructor !== input.constructor) {
return false;
}
if (Object.keys(this).length !== Object.keys(input).length) {
return false;
}
return this.hashCode() === hashCode(input);
}
/**
* Calculates a hash code for the current instance of the class.
*
* @returns {number} The calculated hash code for the instance.
*/
hashCode(): number {
return hashCode(this);
}
/**
* Returns a plain object with the properties of the current instance.
*
* @returns {object} A plain object of the current instance's properties.
*/
valueOf(): object {
const result: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
} = {};
for (const key of Object.keys(this)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result[key] = (this as any)[key];
}
return result;
}
/**
* Returns a string representing the current instance of the class.
* Intended to be overridden by subclasses for locale-specific purposes.
*
* @returns {string} A string representation of the current instance.
*/
toLocaleString(): string {
return this.constructor.name;
}
/**
* Returns a string representing the current instance of the class.
*
* @returns {string} A string representation of the current instance.
*/
toString(): string {
return this.constructor.name;
}
/**
* Called when an instance of a class is converted to a primitive value.
*
* @param {string} [hint] Desired primitive type:
* - `string`
* - `number`
* - `default`
* @returns {string | number} The primitive value.
*/
[Symbol.toPrimitive](
hint: "string" | "number" | "default" | undefined = "default"
): string | number {
return hint === "number" ? this.hashCode() : this.constructor.name;
}
}