@@ -75,7 +75,243 @@ In browsers with [`esm.sh`][esmsh]:
7575
7676## Use
7777
78- ** TODO** : use
78+ Say we have the following TypeScript file ` fibonacci-sequence.ts ` :
79+
80+ ```` ts
81+ /**
82+ * @file FibonacciSequence
83+ * @module FibonacciSequence
84+ * @see https://codewars.com/kata/55695bc4f75bbaea5100016b
85+ */
86+
87+ /**
88+ * Fibonacci sequence iterator.
89+ *
90+ * :::info
91+ * A fibonacci sequence starts with two `1`s. Every element afterwards is the
92+ * sum of the two previous elements:
93+ * ```txt
94+ * 1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ...
95+ * ```
96+ * :::
97+ *
98+ * @implements {Iterator<number, number>}
99+ */
100+ class FibonacciSequence implements Iterator <number , number > {
101+ /**
102+ * First managed sequence value.
103+ *
104+ * @public
105+ * @instance
106+ * @member {number} fib1
107+ */
108+ public fib1: number
109+
110+ /**
111+ * Second managed sequence value.
112+ *
113+ * @public
114+ * @instance
115+ * @member {number} fib2
116+ */
117+ public fib2: number
118+
119+ /**
120+ * Max sequence value.
121+ *
122+ * @private
123+ * @instance
124+ * @member {number} max
125+ */
126+ readonly #max: number
127+
128+ /**
129+ * Create a new fibonacci sequence iterator.
130+ *
131+ * @param {number} [max= Number.MAX_SAFE_INTEGER ] - Max sequence value
132+ */
133+ constructor (max : number = Number .MAX_SAFE_INTEGER) {
134+ this .#max = max < 0 ? 0 : max
135+ this .fib1 = this .fib2 = 1
136+ }
137+
138+ /**
139+ * Iterable protocol.
140+ *
141+ * @public
142+ * @instance
143+ *
144+ * @return {IterableIterator<number>} Current sequence iterator
145+ */
146+ public [Symbol .iterator ](): IterableIterator <number > {
147+ return this
148+ }
149+
150+ /**
151+ * Get the next value in the fibonacci sequence.
152+ *
153+ * @public
154+ * @instance
155+ *
156+ * @return {IteratorResult<number, number>} Next sequence value
157+ */
158+ public next(): IteratorResult <number , number > {
159+ /**
160+ * Temporary sequence value.
161+ *
162+ * @const {number} value
163+ */
164+ const value: number = this .fib1
165+
166+ // reset current sequence values
167+ this .fib1 = this .fib2
168+ this .fib2 = value + this .fib1
169+
170+ return { done: value >= this .#max , value }
171+ }
172+ }
173+
174+ export default FibonacciSequence
175+ ````
176+
177+ …and our module ` example.mjs ` looks as follows:
178+
179+ ``` js
180+ import docastParse from ' @flex-development/docast-parse'
181+ import remarkDirective from ' remark-directive'
182+ import { read } from ' to-vfile'
183+ import { unified } from ' unified'
184+ import { inspect } from ' unist-util-inspect'
185+
186+ const file = await read (' fibonacci-sequence.ts' )
187+
188+ const tree = unified ()
189+ .use (docastParse)
190+ .use (remarkDirective)
191+ .parse (file)
192+
193+ console .log (inspect (tree))
194+ ```
195+
196+ …now running ` node example.mjs ` yields:
197+
198+ ``` sh
199+ root[9]
200+ ├─0 comment[3] (1:1-5:4, 0-122)
201+ │ │ code: null
202+ │ ├─0 blockTag< file> [1] (2:4-2:27, 7-30)
203+ │ │ │ tag: " @file"
204+ │ │ └─0 text " FibonacciSequence" (2:10-2:27, 13-30)
205+ │ ├─1 blockTag< module> [1] (3:4-3:29, 34-59)
206+ │ │ │ tag: " @module"
207+ │ │ └─0 text " FibonacciSequence" (3:12-3:29, 42-59)
208+ │ └─2 blockTag< see> [1] (4:4-4:59, 63-118)
209+ │ │ tag: " @see"
210+ │ └─0 text " https://codewars.com/kata/55695bc4f75bbaea5100016b" (4:9-4:59, 68-118)
211+ ├─1 comment[2] (7:1-19:4, 124-414)
212+ │ │ code: null
213+ │ ├─0 description[4] (8:4-16:7, 131-365)
214+ │ │ ├─0 paragraph[1] (8:4-8:32, 131-159)
215+ │ │ │ └─0 text " Fibonacci sequence iterator." (8:4-8:32, 131-159)
216+ │ │ ├─1 break (8:32-9:1, 159-160)
217+ │ │ ├─2 break (9:3-10:1, 162-163)
218+ │ │ │ data: {" blank" :true}
219+ │ │ └─3 containerDirective< info> [2] (10:4-16:7, 166-365)
220+ │ │ │ attributes: {}
221+ │ │ ├─0 paragraph[3] (11:4-12:37, 177-288)
222+ │ │ │ ├─0 text " A fibonacci sequence starts with two " (11:4-11:41, 177-214)
223+ │ │ │ ├─1 inlineCode " 1" (11:41-11:44, 214-217)
224+ │ │ │ └─2 text " s. Every element afterwards is the sum of the two previous elements:" (11:44-12:37, 217-288)
225+ │ │ └─1 code " 1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ..." (13:4-15:7, 292-358)
226+ │ │ lang: " txt"
227+ │ │ meta: null
228+ │ └─1 blockTag< implements> [1] (18:4-18:42, 372-410)
229+ │ │ tag: " @implements"
230+ │ └─0 typeExpression " Iterator<number, number>" (18:16-18:42, 384-410)
231+ ├─2 comment[4] (21:3-27:6, 479-583)
232+ │ │ code: null
233+ │ ├─0 description[1] (22:6-22:35, 488-517)
234+ │ │ └─0 paragraph[1] (22:6-22:35, 488-517)
235+ │ │ └─0 text " First managed sequence value." (22:6-22:35, 488-517)
236+ │ ├─1 blockTag< public> [0] (24:6-24:13, 528-535)
237+ │ │ tag: " @public"
238+ │ ├─2 blockTag< instance> [0] (25:6-25:15, 541-550)
239+ │ │ tag: " @instance"
240+ │ └─3 blockTag< member> [2] (26:6-26:27, 556-577)
241+ │ │ tag: " @member"
242+ │ ├─0 typeExpression " number" (26:14-26:22, 564-572)
243+ │ └─1 text " fib1" (26:23-26:27, 573-577)
244+ ├─3 comment[4] (30:3-36:6, 609-714)
245+ │ │ code: null
246+ │ ├─0 description[1] (31:6-31:36, 618-648)
247+ │ │ └─0 paragraph[1] (31:6-31:36, 618-648)
248+ │ │ └─0 text " Second managed sequence value." (31:6-31:36, 618-648)
249+ │ ├─1 blockTag< public> [0] (33:6-33:13, 659-666)
250+ │ │ tag: " @public"
251+ │ ├─2 blockTag< instance> [0] (34:6-34:15, 672-681)
252+ │ │ tag: " @instance"
253+ │ └─3 blockTag< member> [2] (35:6-35:27, 687-708)
254+ │ │ tag: " @member"
255+ │ ├─0 typeExpression " number" (35:14-35:22, 695-703)
256+ │ └─1 text " fib2" (35:23-35:27, 704-708)
257+ ├─4 comment[4] (39:3-45:6, 740-834)
258+ │ │ code: null
259+ │ ├─0 description[1] (40:6-40:25, 749-768)
260+ │ │ └─0 paragraph[1] (40:6-40:25, 749-768)
261+ │ │ └─0 text " Max sequence value." (40:6-40:25, 749-768)
262+ │ ├─1 blockTag< private> [0] (42:6-42:14, 779-787)
263+ │ │ tag: " @private"
264+ │ ├─2 blockTag< instance> [0] (43:6-43:15, 793-802)
265+ │ │ tag: " @instance"
266+ │ └─3 blockTag< member> [2] (44:6-44:26, 808-828)
267+ │ │ tag: " @member"
268+ │ ├─0 typeExpression " number" (44:14-44:22, 816-824)
269+ │ └─1 text " max" (44:23-44:26, 825-828)
270+ ├─5 comment[2] (48:3-52:6, 862-995)
271+ │ │ code: null
272+ │ ├─0 description[1] (49:6-49:47, 871-912)
273+ │ │ └─0 paragraph[1] (49:6-49:47, 871-912)
274+ │ │ └─0 text " Create a new fibonacci sequence iterator." (49:6-49:47, 871-912)
275+ │ └─1 blockTag< param> [2] (51:6-51:72, 923-989)
276+ │ │ tag: " @param"
277+ │ ├─0 typeExpression " number" (51:13-51:21, 930-938)
278+ │ └─1 text " [max=Number.MAX_SAFE_INTEGER] - Max sequence value" (51:22-51:72, 939-989)
279+ ├─6 comment[4] (58:3-65:6, 1122-1259)
280+ │ │ code: null
281+ │ ├─0 description[1] (59:6-59:24, 1131-1149)
282+ │ │ └─0 paragraph[1] (59:6-59:24, 1131-1149)
283+ │ │ └─0 text " Iterable protocol." (59:6-59:24, 1131-1149)
284+ │ ├─1 blockTag< public> [0] (61:6-61:13, 1160-1167)
285+ │ │ tag: " @public"
286+ │ ├─2 blockTag< instance> [0] (62:6-62:15, 1173-1182)
287+ │ │ tag: " @instance"
288+ │ └─3 blockTag< return> [2] (64:6-64:66, 1193-1253)
289+ │ │ tag: " @return"
290+ │ ├─0 typeExpression " IterableIterator<number>" (64:14-64:40, 1201-1227)
291+ │ └─1 text " Current sequence iterator" (64:41-64:66, 1228-1253)
292+ ├─7 comment[4] (70:3-77:6, 1340-1504)
293+ │ │ code: null
294+ │ ├─0 description[1] (71:6-71:51, 1349-1394)
295+ │ │ └─0 paragraph[1] (71:6-71:51, 1349-1394)
296+ │ │ └─0 text " Get the next value in the fibonacci sequence." (71:6-71:51, 1349-1394)
297+ │ ├─1 blockTag< public> [0] (73:6-73:13, 1405-1412)
298+ │ │ tag: " @public"
299+ │ ├─2 blockTag< instance> [0] (74:6-74:15, 1418-1427)
300+ │ │ tag: " @instance"
301+ │ └─3 blockTag< return> [2] (76:6-76:66, 1438-1498)
302+ │ │ tag: " @return"
303+ │ ├─0 typeExpression " IteratorResult<number, number>" (76:14-76:46, 1446-1478)
304+ │ └─1 text " Next sequence value" (76:47-76:66, 1479-1498)
305+ └─8 comment[2] (79:5-83:8, 1559-1639)
306+ │ code: null
307+ ├─0 description[1] (80:8-80:33, 1570-1595)
308+ │ └─0 paragraph[1] (80:8-80:33, 1570-1595)
309+ │ └─0 text " Temporary sequence value." (80:8-80:33, 1570-1595)
310+ └─1 blockTag< const> [2] (82:8-82:29, 1610-1631)
311+ │ tag: " @const"
312+ ├─0 typeExpression " number" (82:15-82:23, 1617-1625)
313+ └─1 text " value" (82:24-82:29, 1626-1631)
314+ ```
79315
80316## API
81317
0 commit comments