-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.4 Prototype.ts
183 lines (151 loc) · 5.49 KB
/
1.4 Prototype.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
abstract class Prototype {
abstract clone(): Prototype;
}
interface LetterBlock {
name?: string;
text?: string;
size?: number;
formatting?: Formatting;
}
class LetterBlockPrototype extends Prototype implements LetterBlock {
name: string;
text: string;
size: number;
formatting: Formatting;
readonly DEFAULT_NAME = 'sample_block';
readonly DEFAULT_TEXT = 'Lorem Ipsum';
readonly DEFAULT_SIZE = 14;
readonly DEFAULT_FORMATTING = 'plain';
readonly PLACEHOLDER_PREFIX = '\{\{ ';
readonly PLACEHOLDER_SUFFIX = ' \}\}';
constructor(name?: string, text?: string, size?: number, formatting?: Formatting) {
super();
this.name = name ?? this.DEFAULT_NAME;
this.text = text ?? this.DEFAULT_TEXT;
this.size = size ?? this.DEFAULT_SIZE;
this.formatting = formatting ?? this.DEFAULT_FORMATTING;
}
static fromLetterBlock(block: LetterBlockPrototype): LetterBlockPrototype {
return new LetterBlockPrototype(block.name, block.text, block.size, block.formatting);
}
clone(): LetterBlockPrototype {
return LetterBlockPrototype.fromLetterBlock(this);
}
fillPlaceholders(placeholders: Placeholder[]): LetterBlockPrototype {
const placeholderKeysInBlock = this.getBlockPlaceholderKeys();
for (const key of placeholderKeysInBlock) {
const placeholder = placeholders.filter(p => p.key === key).pop();
if (!placeholder) continue;
const placeholderText = this.PLACEHOLDER_PREFIX + placeholder.key + this.PLACEHOLDER_SUFFIX;
this.text = this.text.replace(placeholderText, placeholder.value);
}
return this;
}
getBlockPlaceholderKeys(): string[] {
const placeholderExtractor = new RegExp(`${this.PLACEHOLDER_PREFIX}(.+?)${this.PLACEHOLDER_SUFFIX}`, 'g');
const decorationExtractor = new RegExp(`${this.PLACEHOLDER_PREFIX}|${this.PLACEHOLDER_SUFFIX}`, 'g');
return (this.text.match(placeholderExtractor) || [])
.map(key => key.replace(decorationExtractor, ''));
}
toString(): string {
const output = { text: this.text, size: this.size, formatting: this.formatting };
return JSON.stringify(output, null, 2);
}
}
type Formatting = 'plain' | 'italic' | 'bold';
type Placeholder = { key: string, value: string };
class PrototypeLibrary {
data: { [key: string]: Prototype };
constructor() {
this.data = {};
}
add(key: string, value: Prototype): void {
this.data[key] = value;
}
pick(key: string): Prototype {
return this.data[key];
}
remove(key: string): void {
delete this.data[key];
}
}
abstract class LetterComposer {
library: PrototypeLibrary;
placeholders: Placeholder[];
composition: LetterBlockPrototype[];
constructor(library: PrototypeLibrary) {
this.library = library;
this.placeholders = [];
this.composition = [];
}
addPlaceholders(placeholders: Placeholder[]): void {
this.placeholders = placeholders;
}
printLetter(): string {
this.pickBlocks();
this.fillPlaceholders();
return this.toString();
}
pickBlocks(): void { };
fillPlaceholders(): void {
for (const block of this.composition) {
block.fillPlaceholders(this.placeholders);
}
}
toString(): string {
const blocks = this.composition.map(block => block.toString());
return blocks.join('\n\n');
}
}
class JobApplicationLetterComposer extends LetterComposer {
constructor(library: PrototypeLibrary) {
super(library);
}
pickBlocks(): void {
const greeting = this.loadLetterBlock('greeting_formal');
const introduction = this.loadLetterBlock('introduction_formal');
const offer = this.loadLetterBlock('offer_formal');
const ending = this.loadLetterBlock('ending_formal');
this.composition.push(greeting, introduction, offer, ending);
}
loadLetterBlock(key: string) {
const block = this.library.pick(key);
if (block instanceof LetterBlockPrototype) return block;
return new LetterBlockPrototype();
}
}
const letterBlocks: LetterBlock[] = [
{
name: 'greeting_formal',
text: 'Dear {{ receiverName }},',
size: 18,
formatting: 'bold',
},
{
name: 'introduction_formal',
text: 'My name is {{ senderName }}. I am a middle backend developer (JS/TS) and data analyst (R/Python).',
},
{
name: 'offer_formal',
text: 'I want to apply for a {{ position }} position. I really like the mission of your {{ companyName }} and I am fascinated with the results your team achieved to the moment. I look forward to hearing from you soon!',
formatting: 'italic',
},
{
name: 'ending_formal',
text: 'Sincerely,\n{{ senderName }}',
},
];
const placeholders: Placeholder[] = [
{ key: 'receiverName', value: 'John Greenfield' },
{ key: 'senderName', value: 'Georgy Mishurovsky' },
{ key: 'position', value: 'Middle TS Developer' },
{ key: 'companyName', value: 'IQVIA' },
];
const library = new PrototypeLibrary();
for (const block of letterBlocks) {
const prototype = new LetterBlockPrototype(block.name, block.text, block.size, block.formatting);
library.add(prototype.name, prototype);
}
const composer = new JobApplicationLetterComposer(library);
composer.addPlaceholders(placeholders);
console.log(composer.printLetter());