Skip to content

Commit cc0dbc0

Browse files
committed
add iterator design pattern
Signed-off-by: Nimit <nimitagg95@gmail.com>
1 parent f44133c commit cc0dbc0

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

Behavioral/iterator.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const fs = require('fs');
2+
3+
class Iterator {
4+
getNext() {}
5+
hasNext() {}
6+
}
7+
8+
// Internally maintains list of files in the folder
9+
class FileBufferIterator extends Iterator {
10+
constructor(folderPath) {
11+
super();
12+
this.folderPath = folderPath;
13+
this.currentPosition = 0;
14+
this.init();
15+
}
16+
init() {
17+
const folderPath = this.folderPath;
18+
let fileList = fs.readdirSync(folderPath);
19+
console.log(fileList);
20+
fileList = fileList.map(fileName => folderPath + "/" + fileName);
21+
this.fileBuffer = fileList.map(filePath => fs.readFileSync(filePath));
22+
}
23+
getNext() {
24+
if(this.hasNext()) {
25+
return this.fileBuffer[this.currentPosition++];
26+
}
27+
}
28+
hasNext() {
29+
return this.fileBuffer.length > this.currentPosition;
30+
}
31+
}
32+
33+
function totalSize(iterator) {
34+
let size = 0;
35+
while(iterator.hasNext()) {
36+
size += iterator.getNext().length;
37+
}
38+
return size;
39+
}
40+
41+
console.log(totalSize(new FileBufferIterator(__dirname)));

Structural/composite.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
class ContentBuffer {
3-
getSize() {
4-
return this.buf.length || 0;
5-
}
3+
getSize() { }
64
}
75

86
// Composite Class has children as filebuffer
@@ -24,6 +22,9 @@ class FileBuffer extends ContentBuffer {
2422
this.buf = buf;
2523
return this;
2624
}
25+
getSize() {
26+
return this.buf.length || 0;
27+
}
2728
}
2829

2930
const file1 = new FileBuffer().setBuffer(Buffer.from("hello"));

buildDocs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const sections = [
3232
topics: [
3333
{ name: 'chainOfResponsibility', path: './Behavioral' },
3434
{ name: 'command', path: './Behavioral' },
35+
{ name: 'iterator', path: './Behavioral' },
3536
]
3637
}
3738
];

docs.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
**[ Behavioral](#behavioral)**
2020
* [Chain Of Responsibility](#chain-of-responsibility)
2121
* [Command](#command)
22+
* [Iterator](#iterator)
2223

2324

2425

@@ -408,9 +409,7 @@ new Post("jsonplaceholder.typicode.com/posts/1", ApiRequestFactory.createApiRequ
408409
```Javascript
409410

410411
class ContentBuffer {
411-
getSize() {
412-
return this.buf.length || 0;
413-
}
412+
getSize() { }
414413
}
415414

416415
// Composite Class has children as filebuffer
@@ -432,6 +431,9 @@ class FileBuffer extends ContentBuffer {
432431
this.buf = buf;
433432
return this;
434433
}
434+
getSize() {
435+
return this.buf.length || 0;
436+
}
435437
}
436438

437439
const file1 = new FileBuffer().setBuffer(Buffer.from("hello"));
@@ -732,5 +734,51 @@ exampleStream.on('disconnect', new DisconnectCallback());
732734
exampleStream.connect();
733735
```
734736

737+
### Iterator
738+
##### iterator.js
739+
```Javascript
740+
const fs = require('fs');
741+
742+
class Iterator {
743+
getNext() {}
744+
hasNext() {}
745+
}
746+
747+
// Internally maintains list of files in the folder
748+
class FileBufferIterator extends Iterator {
749+
constructor(folderPath) {
750+
super();
751+
this.folderPath = folderPath;
752+
this.currentPosition = 0;
753+
this.init();
754+
}
755+
init() {
756+
const folderPath = this.folderPath;
757+
let fileList = fs.readdirSync(folderPath);
758+
console.log(fileList);
759+
fileList = fileList.map(fileName => folderPath + "/" + fileName);
760+
this.fileBuffer = fileList.map(filePath => fs.readFileSync(filePath));
761+
}
762+
getNext() {
763+
if(this.hasNext()) {
764+
return this.fileBuffer[this.currentPosition++];
765+
}
766+
}
767+
hasNext() {
768+
return this.fileBuffer.length > this.currentPosition;
769+
}
770+
}
771+
772+
function totalSize(iterator) {
773+
let size = 0;
774+
while(iterator.hasNext()) {
775+
size += iterator.getNext().length;
776+
}
777+
return size;
778+
}
779+
780+
console.log(totalSize(new FileBufferIterator(__dirname)));
781+
```
782+
735783

736784

0 commit comments

Comments
 (0)