forked from tinymce/tinymce-code-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2Ex1.ts
More file actions
52 lines (37 loc) · 1.42 KB
/
Copy pathPart2Ex1.ts
File metadata and controls
52 lines (37 loc) · 1.42 KB
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
/*
Code Style
==========
The first thing people might notice is that we don't use OO - we avoid it entirely.
Concepts like "classes" containing "fields" and "methods" - we avoid those.
Instead, we model data as *interfaces* - ideally with readonly fields -
and we create "static" methods to operate on this data.
1. Let's create a data structure to hold the bounds of an element.
Let's model the x,y of the top-left and bottom-right corners.
*Remember* we like immutable data, so mark the fields readonly.
*/
export interface Boundz {
// TODO: add fields: x1, y1, x2, y2
readonly x1: number;
readonly y1: number;
readonly x2: number;
readonly y2: number;
}
/*
2. Now let's calculate the width and height of the bounds here.
Notice the use of the arrow function - we like arrow functions because they're concise.
We tell tsc to transpile to ES5, so IE works.
Notice also that we have an explicit return type. This lets the compiler check that our
code matches the type signature.
*/
export const width = (b: Boundz): number => b.x2 - b.x1;
/*
3. Compiling.
TODO Run `yarn build` at your shell to make sure everything compiles.
`build` is a script defined in the package.json. Have a look at package.json to see what it does.
*/
/*
Ok, so we started off pretty easy.
Now, code is useless without tests, so let's head over to Exercise1CodeStyleTest.ts
and write some tests.
*/
export const height = (b: Boundz): number => b.y2 - b.y1;