Skip to content

Commit bc7b612

Browse files
committed
Use Symbol.iterator and spread syntax
Refs: https://github.com/HowProgrammingWorks/Dictionary/issues/10
1 parent 9fca418 commit bc7b612

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

JavaScript/c-iterator.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const text = (s = '', o = {
4+
line: a => (s += '\n' + a, o),
5+
[Symbol.iterator]: () => ({
6+
next() {
7+
const res = { value: s, done: this.finished };
8+
this.finished = true;
9+
return res;
10+
}
11+
})
12+
}) => o;
13+
14+
// Usage
15+
16+
const txt = text('line1')
17+
.line('line2')
18+
.line('line3')
19+
.line('line4');
20+
21+
console.log(...txt);

0 commit comments

Comments
 (0)