Skip to content

Commit e30e481

Browse files
committed
feat: menambahkan file materi decorator
1 parent 3321b00 commit e30e481

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { StringUtility } from "./Decorators";
2+
3+
test("[TypeScriptBasic/0x_Decorators] Pengecekan Class Decorator", () => {
4+
expect(new StringUtility(["Test"]).data[0]).toBe("Teks pengganti argumen pertama");
5+
})
6+
7+
test("[TypeScriptBasic/0x_Decorators] Pengecekan Property Decorator", () => {
8+
// @ts-expect-error
9+
expect(new StringUtility("abc")).toThrowError();
10+
})
11+
12+
test("[TypeScriptBasic/0x_Decorators] Pengecekan Method Decorator", () => {
13+
expect(StringUtility.prototype.sambungData = () => { return "" }).toThrowError();
14+
})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Penjelasan akan ditulis nanti.
2+
3+
function gantiArgumenConstructor(constructor: typeof StringUtility): any {
4+
return class extends constructor {
5+
constructor(args: string[]) {
6+
super(["Teks pengganti argumen pertama", ...args]);
7+
}
8+
}
9+
}
10+
11+
function buatMethodTidakDapatDiubah(target: any, key: string, descriptor: PropertyDescriptor) {
12+
descriptor.writable = false;
13+
}
14+
15+
function harusBerupaArray(target: any, key: string) {
16+
if (!Array.isArray(target[key])) {
17+
throw new Error(`${key} harus berupa array`);
18+
}
19+
}
20+
21+
@gantiArgumenConstructor
22+
class StringUtility {
23+
@harusBerupaArray
24+
public data: string[];
25+
26+
constructor(data: string[]) {
27+
this.data = data;
28+
}
29+
30+
@buatMethodTidakDapatDiubah
31+
sambungData(separator: string): string {
32+
return this.data.join(separator);
33+
}
34+
35+
// Bisa digunakan juga pada accessor property
36+
@buatMethodTidakDapatDiubah
37+
get dataKapital(): string[] {
38+
return this.data.map(data => data.toUpperCase());
39+
}
40+
}
41+
42+
export {
43+
StringUtility
44+
}

0 commit comments

Comments
 (0)