Skip to content

Commit 5f47a47

Browse files
feat(ImmutableData): add the class to deep freeze the value on initialize and set.
1 parent 8e924c0 commit 5f47a47

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/lib/immutable-data.class.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Abstract.
2+
import { Immutability } from './immutability.abstract';
3+
// Class.
4+
import { ReadonlyData } from './readonly-data.class';
5+
/**
6+
* @description A class that stores and returns an immutable version of the provided value.
7+
* The value is frozen at runtime and marked as `readonly` at type level.
8+
* @export
9+
* @class ImmutableData
10+
* @template Type The original input type.
11+
* @extends {ReadonlyData<Type>}
12+
*/
13+
export class ImmutableData<Type> extends ReadonlyData<Type> {
14+
/**
15+
* @description Returns the `string` tag representation of the `ImmutableData` class when used in `Object.prototype.toString.call(instance)`.
16+
* @public
17+
* @readonly
18+
* @type {string}
19+
*/
20+
public override get [Symbol.toStringTag](): string {
21+
return ImmutableData.name;
22+
}
23+
24+
/**
25+
* Creates an instance of `ImmutableData`.
26+
* @constructor
27+
* @param {Type} value Initial value to store.
28+
*/
29+
constructor(value: Type) {
30+
super(Immutability.deepFreeze(value));
31+
}
32+
33+
/**
34+
* @description Sets the data value.
35+
* @public
36+
* @param {Type} value The data value of `Type` to set.
37+
* @returns {this} The `this` current instance.
38+
*/
39+
public override set(value: Type): this {
40+
return super.set(Immutability.deepFreeze(value));
41+
}
42+
}

0 commit comments

Comments
 (0)