Skip to content

Commit 3597c30

Browse files
committed
docs: use
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 32c0064 commit 3597c30

File tree

3 files changed

+335
-3
lines changed

3 files changed

+335
-3
lines changed

README.md

Lines changed: 239 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ tokens using [micromark][micromark], and turned into nodes using [mdast-util-fro
4040
This package is [ESM only][esm].
4141

4242
```sh
43-
yarn add @flex-development/docast-util-from-docs @flex-development/docast @types/mdast @types/unist
43+
yarn add @flex-development/docast-util-from-docs
44+
yarn add -D @flex-development/docast @types/mdast @types/unist micromark-util-types
4445
```
4546

4647
From Git:
@@ -58,7 +59,243 @@ yarn add @flex-development/docast-util-from-docs@flex-development/docast-util-fr
5859

5960
## Use
6061

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

63300
## API
64301

examples/fibonacci-sequence.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @file FibonacciSequence
3+
* @module FibonacciSequence
4+
* @see https://codewars.com/kata/55695bc4f75bbaea5100016b
5+
*/
6+
7+
/**
8+
* Fibonacci sequence iterator.
9+
*
10+
* :::info
11+
* A fibonacci sequence starts with two `1`s. Every element afterwards is the
12+
* sum of the two previous elements:
13+
* ```txt
14+
* 1, 1, 2, 3, 5, 8, 13, ..., 89, 144, 233, 377, ...
15+
* ```
16+
* :::
17+
*
18+
* @implements {Iterator<number, number>}
19+
*/
20+
class FibonacciSequence implements Iterator<number, number> {
21+
/**
22+
* First managed sequence value.
23+
*
24+
* @public
25+
* @instance
26+
* @member {number} fib1
27+
*/
28+
public fib1: number
29+
30+
/**
31+
* Second managed sequence value.
32+
*
33+
* @public
34+
* @instance
35+
* @member {number} fib2
36+
*/
37+
public fib2: number
38+
39+
/**
40+
* Max sequence value.
41+
*
42+
* @private
43+
* @instance
44+
* @member {number} max
45+
*/
46+
readonly #max: number
47+
48+
/**
49+
* Create a new fibonacci sequence iterator.
50+
*
51+
* @param {number} [max=Number.MAX_SAFE_INTEGER] - Max sequence value
52+
*/
53+
constructor(max: number = Number.MAX_SAFE_INTEGER) {
54+
this.#max = max < 0 ? 0 : max
55+
this.fib1 = this.fib2 = 1
56+
}
57+
58+
/**
59+
* Iterable protocol.
60+
*
61+
* @public
62+
* @instance
63+
*
64+
* @return {IterableIterator<number>} Current sequence iterator
65+
*/
66+
public [Symbol.iterator](): IterableIterator<number> {
67+
return this
68+
}
69+
70+
/**
71+
* Get the next value in the fibonacci sequence.
72+
*
73+
* @public
74+
* @instance
75+
*
76+
* @return {IteratorResult<number, number>} Next sequence value
77+
*/
78+
public next(): IteratorResult<number, number> {
79+
/**
80+
* Temporary sequence value.
81+
*
82+
* @const {number} value
83+
*/
84+
const value: number = this.fib1
85+
86+
// reset current sequence values
87+
this.fib1 = this.fib2
88+
this.fib2 = value + this.fib1
89+
90+
return { done: value >= this.#max, value }
91+
}
92+
}
93+
94+
export default FibonacciSequence

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"paths": {
3232
"#src": ["src/index"],
3333
"#src/*": ["src/*"],
34-
"#tests/*": ["__tests__/*"]
34+
"#tests/*": ["__tests__/*"],
35+
"@flex-development/docast-util-from-docs": ["src/index"]
3536
},
3637
"preserveConstEnums": true,
3738
"preserveSymlinks": false,

0 commit comments

Comments
 (0)