This file contains hidden or 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
| interface Command { | |
| name: string; | |
| execute(args: any): any; | |
| } | |
| class OpenUrlCommand implements Command { | |
| name = "openUrl"; | |
| execute(args: any) { | |
| console.log(`Open url: ${args[0]}`); | |
| } |
This file contains hidden or 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
| interface Strategy { | |
| authenticate(args: any[]): boolean; | |
| } | |
| class Authenticator { | |
| strategies: Record<string, Strategy> = {}; | |
| use(name: string, strategy: Strategy) { | |
| this.strategies[name] = strategy; | |
| } |
This file contains hidden or 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
| type CacheItem = { | |
| now: number; | |
| value: string; | |
| maxAge: number; | |
| }; | |
| class LocalStorageProxy { | |
| setItem(key: string, value: unknown, maxAge: number = 0) { | |
| localStorage.setItem( | |
| key, |
This file contains hidden or 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
| class HttpServiceProxy { | |
| private httpService: HttpService; | |
| private dataCache: Map<string, any> = new Map(); | |
| constructor() { | |
| this.httpService = new HttpService(); | |
| } | |
| async sendRequest(method: string, url: string, body?: BodyInit) { | |
| const cacheKey = method + ":" + url; |
This file contains hidden or 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
| class Client { | |
| private shapeFacade: ShapeFacade; | |
| constructor() { | |
| this.shapeFacade = new ShapeFacade(); | |
| } | |
| drawShapes() { | |
| this.shapeFacade.drawCircle(); | |
| this.shapeFacade.drawRectangle(); | |
| this.shapeFacade.drawTriangle(); |
This file contains hidden or 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
| class ShapeFacade { | |
| private circle: Shape; | |
| private rectangle: Shape; | |
| private triangle: Shape; | |
| constructor() { | |
| this.circle = new Circle(); | |
| this.rectangle = new Rectangle(); | |
| this.triangle = new Triangle(); | |
| } |
This file contains hidden or 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
| interface Shape { | |
| draw(): void; | |
| } | |
| class Circle implements Shape { | |
| draw(): void { | |
| console.log("Draw circle"); | |
| } | |
| } |
This file contains hidden or 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
| class UPhoneFlyweight { | |
| constructor(model: string, screen: number, memory: number) {} | |
| } | |
| class FlyweightFactory { | |
| public phonesMap: Map<string, UPhoneFlyweight> = new Map(); | |
| public get(model: string, screen: number, memory: number): UPhoneFlyweight { | |
| const key = model + screen + memory; | |
| if (!this.phonesMap.has(key)) { |
This file contains hidden or 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
| function Builder(typeOrTemplate, template) { | |
| let type; | |
| if (typeOrTemplate instanceof Function) { | |
| type = typeOrTemplate; | |
| } else { | |
| template = typeOrTemplate; | |
| } | |
| const built = template ? Object.assign({}, template) : {}; | |
| const builder = new Proxy( | |
| {}, |
This file contains hidden or 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
| function sandbox(code) { | |
| code = "with (sandbox) {" + code + "}"; | |
| const fn = new Function("sandbox", code); | |
| return function (sandbox) { | |
| const sandboxProxy = new Proxy(sandbox, { | |
| has(target, key) { | |
| return true; | |
| }, | |
| get(target, key) { |
NewerOlder