Skip to content

Commit cccea7d

Browse files
authored
Merge pull request #44 from WalnutProgramming/new-node-api
New node api
2 parents 27f4c77 + 24863d7 commit cccea7d

29 files changed

+2068
-835
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ dist/
1919
yarn-error.log
2020

2121
docs
22+
23+
# jest test coverage
24+
coverage/

README.md

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,39 @@ replacing "room-finder" with "[email protected]" in either URL above. -->
111111

112112
The version number in the URL isn't required, but it's recommended so that breaking changes in the package don't break your app.
113113

114-
## Usage
114+
## Modeling a simple Hallway
115115

116-
### Modeling a simple Hallway
116+
![A single, straight hallway with 8 rooms that alternate between the left and right sides. If the image is not appearing, view the docs at https://room-finder.walnut.direct.](images/basic.png)
117117

118-
![A single, straight hallway with 8 rooms that alternate between the left and right sides](images/1.png)
118+
This is a simple example of a small "Building." Let's see the code we would need to model this Building so that we can generate directions between any pair of rooms.
119+
120+
The example building above includes only a single hallway with 8 rooms. In this example, we will arbitrarily decide to list the rooms starting from the left side of the picture. (That's what the arrow in the picture represents.)
121+
122+
To create the model, imagine walking down the hallway in the picture from left to right.
123+
124+
- You would pass 102 on your RIGHT,
125+
- then pass 103 on your LEFT,
126+
- then pass 104 on your RIGHT,
127+
- then pass 105 on your LEFT.
128+
- then pass 106 on your RIGHT,
129+
- then pass 107 on your LEFT,
130+
- then pass 108 on your RIGHT,
131+
- then pass 109 on your LEFT.
132+
133+
If we reversed it and started from the right, we would need to flip the side that each room is on, but the generated directions would stay the same.
119134

120135
```js
121-
// examples/ex1.ts#L3-L1000
136+
// examples/basic.ts#L1-L1000
122137

138+
import {
139+
Building,
140+
Hallway,
141+
Room,
142+
Direction,
143+
assertValidBuilding,
144+
} from "room-finder";
145+
146+
/* 1. MODEL THE BUILDING */
123147
// A Hallway has an array of Rooms. Each Room has a name and a side.
124148
const hallway = new Hallway([
125149
new Room("102", Direction.RIGHT),
@@ -133,39 +157,72 @@ const hallway = new Hallway([
133157
new Room("109", Direction.LEFT),
134158
]);
135159

136-
// A Building has an array of Hallways.
137-
// In this case, there's only one Hallway in the Building.
160+
// A Building has an array of Hallways. In this case, there's only one Hallway in the Building.
138161
const building = new Building([hallway]);
139162

163+
/* 2. CHECK BUILDING VALIDITY */
164+
// The assertValidBuilding function throws an error
165+
// if there is a problem with the generated Building.
166+
assertValidBuilding(building);
167+
168+
/* 3. GENERATE DIRECTIONS BETWEEN ROOMS */
140169
console.log(building.getDirections("102", "109"));
141-
// Turn right out of room 102
142-
// Continue, then turn left into room 109
170+
// output:
171+
// Turn right out of room 102
172+
// Continue, then turn left into room 109
143173

144174
console.log(building.getDirections("107", "103"));
145-
// Turn right out of room 107
146-
// Continue, then turn right into room 103
175+
// output:
176+
// Turn right out of room 107
177+
// Continue, then turn right into room 103
147178
```
148179

149-
### Adding a Turn
180+
## Adding a Turn
150181

151-
![The same hallway as before, but with a Turn inserted between 105 and 106](images/2.png)
182+
![The same hallway as before, but with a Turn inserted between 105 and 106. If the image is not appearing, view the docs at https://room-finder.walnut.direct.](images/fork.png)
152183

153-
```js
154-
// examples/ex2.ts#L3-L1000
184+
This example is the same as above, but a Turn is inserted between 105 and 106. Again, we decided to start the hallway from the left in the picture. Imagine walking through this hallway:
155185

156-
const hallway = new Hallway([
157-
new Room("102", Direction.RIGHT),
158-
new Room("103", Direction.LEFT),
159-
new Room("104", Direction.RIGHT),
160-
new Room("105", Direction.LEFT),
161-
new Turn(Direction.RIGHT),
162-
new Room("106", Direction.RIGHT),
163-
new Room("107"),
164-
new Room("108", Direction.RIGHT),
165-
new Room("109", Direction.LEFT),
186+
- You would pass 102 on your RIGHT,
187+
- then pass 103 on your LEFT,
188+
- then pass 104 on your RIGHT,
189+
- then pass 105 on your LEFT,
190+
- **then turn RIGHT,**
191+
- then pass 106 on your RIGHT,
192+
- then pass 107 on your LEFT,
193+
- then pass 108 on your RIGHT,
194+
- then pass 109 on your LEFT.
195+
196+
Let's convert that into a room-finder model:
197+
198+
```js
199+
// examples/turn.ts#L1-L1000
200+
201+
import {
202+
Building,
203+
Hallway,
204+
Room,
205+
Direction,
206+
Turn,
207+
assertValidBuilding,
208+
} from "room-finder";
209+
210+
const building = new Building([
211+
new Hallway([
212+
new Room("102", Direction.RIGHT),
213+
new Room("103", Direction.LEFT),
214+
new Room("104", Direction.RIGHT),
215+
new Room("105", Direction.LEFT),
216+
// All we have to do is add a Turn in the middle of the Hallway.
217+
new Turn(Direction.RIGHT),
218+
new Room("106", Direction.RIGHT),
219+
new Room("107"),
220+
new Room("108", Direction.RIGHT),
221+
new Room("109", Direction.LEFT),
222+
]),
166223
]);
167224

168-
const building = new Building([hallway]);
225+
assertValidBuilding(building);
169226

170227
console.log(building.getDirections("102", "109"));
171228
// Turn right out of room 102
@@ -177,3 +234,11 @@ console.log(building.getDirections("107", "103"));
177234
// Continue, then turn left (after passing room 106 on your left)
178235
// Continue, then turn right into room 103
179236
```
237+
238+
## FRONT
239+
240+
## Adding a Fork
241+
242+
![Two hallways connected by a Fork. If the image is not appearing, view the docs at https://room-finder.walnut.direct.](images/fork.png)
243+
244+
Since a person walking through the top hallway in this example has a choice about whether to turn right into the second hallway or to continue in the same hallway, we need to model this example as **two separate Hallways connected by a Fork**.
File renamed without changes.

docs-copied-files/images/fork.png

9.26 KB
Loading
File renamed without changes.

examples/basic.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
Building,
3+
Hallway,
4+
Room,
5+
Direction,
6+
assertValidBuilding,
7+
} from "room-finder";
8+
9+
/* 1. MODEL THE BUILDING */
10+
// A Hallway has an array of Rooms. Each Room has a name and a side.
11+
const hallway = new Hallway([
12+
new Room("102", Direction.RIGHT),
13+
new Room("103", Direction.LEFT),
14+
new Room("104", Direction.RIGHT),
15+
new Room("105", Direction.LEFT),
16+
new Room("106", Direction.RIGHT),
17+
// If you don't specify a side, the default is Direction.LEFT
18+
new Room("107"),
19+
new Room("108", Direction.RIGHT),
20+
new Room("109", Direction.LEFT),
21+
]);
22+
23+
// A Building has an array of Hallways. In this case, there's only one Hallway in the Building.
24+
const building = new Building([hallway]);
25+
26+
/* 2. CHECK BUILDING VALIDITY */
27+
// The assertValidBuilding function throws an error
28+
// if there is a problem with the generated Building.
29+
assertValidBuilding(building);
30+
31+
/* 3. GENERATE DIRECTIONS BETWEEN ROOMS */
32+
console.log(building.getDirections("102", "109"));
33+
// output:
34+
// Turn right out of room 102
35+
// Continue, then turn left into room 109
36+
37+
console.log(building.getDirections("107", "103"));
38+
// output:
39+
// Turn right out of room 107
40+
// Continue, then turn right into room 103

examples/ex1.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/ex2.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/turn.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
Building,
3+
Hallway,
4+
Room,
5+
Direction,
6+
Turn,
7+
assertValidBuilding,
8+
} from "room-finder";
9+
10+
const building = new Building([
11+
new Hallway([
12+
new Room("102", Direction.RIGHT),
13+
new Room("103", Direction.LEFT),
14+
new Room("104", Direction.RIGHT),
15+
new Room("105", Direction.LEFT),
16+
// All we have to do is add a Turn in the middle of the Hallway.
17+
new Turn(Direction.RIGHT),
18+
new Room("106", Direction.RIGHT),
19+
new Room("107"),
20+
new Room("108", Direction.RIGHT),
21+
new Room("109", Direction.LEFT),
22+
]),
23+
]);
24+
25+
assertValidBuilding(building);
26+
27+
console.log(building.getDirections("102", "109"));
28+
// Turn right out of room 102
29+
// Continue, then turn right (after passing room 105 on your left)
30+
// Continue, then turn left into room 109
31+
32+
console.log(building.getDirections("107", "103"));
33+
// Turn right out of room 107
34+
// Continue, then turn left (after passing room 106 on your left)
35+
// Continue, then turn right into room 103

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "room-finder",
3-
"version": "0.0.11",
3+
"version": "0.0.12-alpha.8",
44
"repository": "github:WalnutProgramming/room-finder",
55
"homepage": "https://room-finder.walnut.direct/",
66
"license": "AGPL-3.0-only",
@@ -30,7 +30,7 @@
3030
"rollup": "^1.32.0",
3131
"rollup-plugin-babel": "^4.3.3",
3232
"rollup-plugin-terser": "^5.1.3",
33-
"typedoc": "^0.16.11",
33+
"typedoc": "^0.17.4",
3434
"typescript": "~3.8.2"
3535
},
3636
"dependencies": {
@@ -48,7 +48,7 @@
4848
"prep": "yarn build && yarn docs && yarn test",
4949
"prepublishOnly": "yarn prep",
5050
"readme": "yarn embedme README.md",
51-
"docs": "yarn readme && yarn typedoc --out docs --mode file --exclude **/*.test.ts src && copyfiles -u 1 \"docs-copied-files/**/*\" docs",
51+
"docs": "yarn readme && yarn typedoc src && copyfiles -u 1 \"docs-copied-files/**/*\" docs",
5252
"test": "jest"
5353
},
5454
"prettier": {

0 commit comments

Comments
 (0)