-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan file materi decorator
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { StringUtility } from "./Decorators"; | ||
|
||
test("[TypeScriptBasic/0x_Decorators] Pengecekan Class Decorator", () => { | ||
expect(new StringUtility(["Test"]).data[0]).toBe("Teks pengganti argumen pertama"); | ||
}) | ||
|
||
test("[TypeScriptBasic/0x_Decorators] Pengecekan Property Decorator", () => { | ||
// @ts-expect-error | ||
expect(new StringUtility("abc")).toThrowError(); | ||
}) | ||
|
||
test("[TypeScriptBasic/0x_Decorators] Pengecekan Method Decorator", () => { | ||
expect(StringUtility.prototype.sambungData = () => { return "" }).toThrowError(); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Penjelasan akan ditulis nanti. | ||
|
||
function gantiArgumenConstructor(constructor: typeof StringUtility): any { | ||
return class extends constructor { | ||
constructor(args: string[]) { | ||
super(["Teks pengganti argumen pertama", ...args]); | ||
} | ||
} | ||
} | ||
|
||
function buatMethodTidakDapatDiubah(target: any, key: string, descriptor: PropertyDescriptor) { | ||
descriptor.writable = false; | ||
} | ||
|
||
function harusBerupaArray(target: any, key: string) { | ||
if (!Array.isArray(target[key])) { | ||
throw new Error(`${key} harus berupa array`); | ||
} | ||
} | ||
|
||
@gantiArgumenConstructor | ||
class StringUtility { | ||
@harusBerupaArray | ||
public data: string[]; | ||
|
||
constructor(data: string[]) { | ||
this.data = data; | ||
} | ||
|
||
@buatMethodTidakDapatDiubah | ||
sambungData(separator: string): string { | ||
return this.data.join(separator); | ||
} | ||
|
||
// Bisa digunakan juga pada accessor property | ||
@buatMethodTidakDapatDiubah | ||
get dataKapital(): string[] { | ||
return this.data.map(data => data.toUpperCase()); | ||
} | ||
} | ||
|
||
export { | ||
StringUtility | ||
} |