data-structure-typed 1.53.1 → 1.53.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +6 -0
  3. package/dist/cjs/data-structures/heap/heap.d.ts +165 -0
  4. package/dist/cjs/data-structures/heap/heap.js +165 -0
  5. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  6. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +426 -0
  7. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +430 -2
  8. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  9. package/dist/mjs/constants/index.js +1 -0
  10. package/dist/mjs/constants/index.js.map +1 -0
  11. package/dist/mjs/data-structures/base/index.js +1 -0
  12. package/dist/mjs/data-structures/base/index.js.map +1 -0
  13. package/dist/mjs/data-structures/base/iterable-element-base.js +1 -0
  14. package/dist/mjs/data-structures/base/iterable-element-base.js.map +1 -0
  15. package/dist/mjs/data-structures/base/iterable-entry-base.js +1 -0
  16. package/dist/mjs/data-structures/base/iterable-entry-base.js.map +1 -0
  17. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  18. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  19. package/dist/mjs/data-structures/binary-tree/avl-tree.js +1 -0
  20. package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -0
  21. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +1 -0
  22. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -0
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -0
  25. package/dist/mjs/data-structures/binary-tree/bst.js +1 -0
  26. package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -0
  27. package/dist/mjs/data-structures/binary-tree/index.js +1 -0
  28. package/dist/mjs/data-structures/binary-tree/index.js.map +1 -0
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -0
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -0
  31. package/dist/mjs/data-structures/binary-tree/segment-tree.js +1 -0
  32. package/dist/mjs/data-structures/binary-tree/segment-tree.js.map +1 -0
  33. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  34. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  35. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -0
  36. package/dist/mjs/data-structures/graph/abstract-graph.js.map +1 -0
  37. package/dist/mjs/data-structures/graph/directed-graph.js +1 -0
  38. package/dist/mjs/data-structures/graph/directed-graph.js.map +1 -0
  39. package/dist/mjs/data-structures/graph/index.js +1 -0
  40. package/dist/mjs/data-structures/graph/index.js.map +1 -0
  41. package/dist/mjs/data-structures/graph/map-graph.js +1 -0
  42. package/dist/mjs/data-structures/graph/map-graph.js.map +1 -0
  43. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -0
  44. package/dist/mjs/data-structures/graph/undirected-graph.js.map +1 -0
  45. package/dist/mjs/data-structures/hash/hash-map.js +1 -0
  46. package/dist/mjs/data-structures/hash/hash-map.js.map +1 -0
  47. package/dist/mjs/data-structures/hash/index.js +1 -0
  48. package/dist/mjs/data-structures/hash/index.js.map +1 -0
  49. package/dist/mjs/data-structures/heap/heap.d.ts +165 -0
  50. package/dist/mjs/data-structures/heap/heap.js +166 -0
  51. package/dist/mjs/data-structures/heap/heap.js.map +1 -0
  52. package/dist/mjs/data-structures/heap/index.js +1 -0
  53. package/dist/mjs/data-structures/heap/index.js.map +1 -0
  54. package/dist/mjs/data-structures/heap/max-heap.js +1 -0
  55. package/dist/mjs/data-structures/heap/max-heap.js.map +1 -0
  56. package/dist/mjs/data-structures/heap/min-heap.js +1 -0
  57. package/dist/mjs/data-structures/heap/min-heap.js.map +1 -0
  58. package/dist/mjs/data-structures/index.js +1 -0
  59. package/dist/mjs/data-structures/index.js.map +1 -0
  60. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +426 -0
  61. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +431 -2
  62. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  63. package/dist/mjs/data-structures/linked-list/index.js +1 -0
  64. package/dist/mjs/data-structures/linked-list/index.js.map +1 -0
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -0
  66. package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -0
  67. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +1 -0
  68. package/dist/mjs/data-structures/linked-list/skip-linked-list.js.map +1 -0
  69. package/dist/mjs/data-structures/matrix/index.js +1 -0
  70. package/dist/mjs/data-structures/matrix/index.js.map +1 -0
  71. package/dist/mjs/data-structures/matrix/matrix.js +1 -0
  72. package/dist/mjs/data-structures/matrix/matrix.js.map +1 -0
  73. package/dist/mjs/data-structures/matrix/navigator.js +1 -0
  74. package/dist/mjs/data-structures/matrix/navigator.js.map +1 -0
  75. package/dist/mjs/data-structures/priority-queue/index.js +1 -0
  76. package/dist/mjs/data-structures/priority-queue/index.js.map +1 -0
  77. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -0
  78. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  79. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -0
  80. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  81. package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -0
  82. package/dist/mjs/data-structures/priority-queue/priority-queue.js.map +1 -0
  83. package/dist/mjs/data-structures/queue/deque.js +1 -0
  84. package/dist/mjs/data-structures/queue/deque.js.map +1 -0
  85. package/dist/mjs/data-structures/queue/index.js +1 -0
  86. package/dist/mjs/data-structures/queue/index.js.map +1 -0
  87. package/dist/mjs/data-structures/queue/queue.js +1 -0
  88. package/dist/mjs/data-structures/queue/queue.js.map +1 -0
  89. package/dist/mjs/data-structures/stack/index.js +1 -0
  90. package/dist/mjs/data-structures/stack/index.js.map +1 -0
  91. package/dist/mjs/data-structures/stack/stack.js +1 -0
  92. package/dist/mjs/data-structures/stack/stack.js.map +1 -0
  93. package/dist/mjs/data-structures/tree/index.js +1 -0
  94. package/dist/mjs/data-structures/tree/index.js.map +1 -0
  95. package/dist/mjs/data-structures/tree/tree.js +1 -0
  96. package/dist/mjs/data-structures/tree/tree.js.map +1 -0
  97. package/dist/mjs/data-structures/trie/index.js +1 -0
  98. package/dist/mjs/data-structures/trie/index.js.map +1 -0
  99. package/dist/mjs/data-structures/trie/trie.js +1 -0
  100. package/dist/mjs/data-structures/trie/trie.js.map +1 -0
  101. package/dist/mjs/index.js +1 -0
  102. package/dist/mjs/index.js.map +1 -0
  103. package/dist/mjs/interfaces/binary-tree.js +1 -0
  104. package/dist/mjs/interfaces/binary-tree.js.map +1 -0
  105. package/dist/mjs/interfaces/doubly-linked-list.js +1 -0
  106. package/dist/mjs/interfaces/doubly-linked-list.js.map +1 -0
  107. package/dist/mjs/interfaces/graph.js +1 -0
  108. package/dist/mjs/interfaces/graph.js.map +1 -0
  109. package/dist/mjs/interfaces/heap.js +1 -0
  110. package/dist/mjs/interfaces/heap.js.map +1 -0
  111. package/dist/mjs/interfaces/index.js +1 -0
  112. package/dist/mjs/interfaces/index.js.map +1 -0
  113. package/dist/mjs/interfaces/navigator.js +1 -0
  114. package/dist/mjs/interfaces/navigator.js.map +1 -0
  115. package/dist/mjs/interfaces/priority-queue.js +1 -0
  116. package/dist/mjs/interfaces/priority-queue.js.map +1 -0
  117. package/dist/mjs/interfaces/segment-tree.js +1 -0
  118. package/dist/mjs/interfaces/segment-tree.js.map +1 -0
  119. package/dist/mjs/interfaces/singly-linked-list.js +1 -0
  120. package/dist/mjs/interfaces/singly-linked-list.js.map +1 -0
  121. package/dist/mjs/types/common.js +1 -0
  122. package/dist/mjs/types/common.js.map +1 -0
  123. package/dist/mjs/types/data-structures/base/base.js +1 -0
  124. package/dist/mjs/types/data-structures/base/base.js.map +1 -0
  125. package/dist/mjs/types/data-structures/base/index.js +1 -0
  126. package/dist/mjs/types/data-structures/base/index.js.map +1 -0
  127. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  128. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  129. package/dist/mjs/types/data-structures/binary-tree/avl-tree.js +1 -0
  130. package/dist/mjs/types/data-structures/binary-tree/avl-tree.js.map +1 -0
  131. package/dist/mjs/types/data-structures/binary-tree/binary-indexed-tree.js +1 -0
  132. package/dist/mjs/types/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  133. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -0
  134. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js.map +1 -0
  135. package/dist/mjs/types/data-structures/binary-tree/bst.js +1 -0
  136. package/dist/mjs/types/data-structures/binary-tree/bst.js.map +1 -0
  137. package/dist/mjs/types/data-structures/binary-tree/index.js +1 -0
  138. package/dist/mjs/types/data-structures/binary-tree/index.js.map +1 -0
  139. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +1 -0
  140. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js.map +1 -0
  141. package/dist/mjs/types/data-structures/binary-tree/segment-tree.js +1 -0
  142. package/dist/mjs/types/data-structures/binary-tree/segment-tree.js.map +1 -0
  143. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
  144. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  145. package/dist/mjs/types/data-structures/graph/abstract-graph.js +1 -0
  146. package/dist/mjs/types/data-structures/graph/abstract-graph.js.map +1 -0
  147. package/dist/mjs/types/data-structures/graph/directed-graph.js +1 -0
  148. package/dist/mjs/types/data-structures/graph/directed-graph.js.map +1 -0
  149. package/dist/mjs/types/data-structures/graph/index.js +1 -0
  150. package/dist/mjs/types/data-structures/graph/index.js.map +1 -0
  151. package/dist/mjs/types/data-structures/graph/map-graph.js +1 -0
  152. package/dist/mjs/types/data-structures/graph/map-graph.js.map +1 -0
  153. package/dist/mjs/types/data-structures/graph/undirected-graph.js +1 -0
  154. package/dist/mjs/types/data-structures/graph/undirected-graph.js.map +1 -0
  155. package/dist/mjs/types/data-structures/hash/hash-map.js +1 -0
  156. package/dist/mjs/types/data-structures/hash/hash-map.js.map +1 -0
  157. package/dist/mjs/types/data-structures/hash/index.js +1 -0
  158. package/dist/mjs/types/data-structures/hash/index.js.map +1 -0
  159. package/dist/mjs/types/data-structures/heap/heap.js +1 -0
  160. package/dist/mjs/types/data-structures/heap/heap.js.map +1 -0
  161. package/dist/mjs/types/data-structures/heap/index.js +1 -0
  162. package/dist/mjs/types/data-structures/heap/index.js.map +1 -0
  163. package/dist/mjs/types/data-structures/heap/max-heap.js +1 -0
  164. package/dist/mjs/types/data-structures/heap/max-heap.js.map +1 -0
  165. package/dist/mjs/types/data-structures/heap/min-heap.js +1 -0
  166. package/dist/mjs/types/data-structures/heap/min-heap.js.map +1 -0
  167. package/dist/mjs/types/data-structures/index.js +1 -0
  168. package/dist/mjs/types/data-structures/index.js.map +1 -0
  169. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.js +1 -0
  170. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  171. package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
  172. package/dist/mjs/types/data-structures/linked-list/index.js.map +1 -0
  173. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.js +1 -0
  174. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.js.map +1 -0
  175. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.js +1 -0
  176. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.js.map +1 -0
  177. package/dist/mjs/types/data-structures/matrix/index.js +1 -0
  178. package/dist/mjs/types/data-structures/matrix/index.js.map +1 -0
  179. package/dist/mjs/types/data-structures/matrix/matrix.js +1 -0
  180. package/dist/mjs/types/data-structures/matrix/matrix.js.map +1 -0
  181. package/dist/mjs/types/data-structures/matrix/navigator.js +1 -0
  182. package/dist/mjs/types/data-structures/matrix/navigator.js.map +1 -0
  183. package/dist/mjs/types/data-structures/priority-queue/index.js +1 -0
  184. package/dist/mjs/types/data-structures/priority-queue/index.js.map +1 -0
  185. package/dist/mjs/types/data-structures/priority-queue/max-priority-queue.js +1 -0
  186. package/dist/mjs/types/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  187. package/dist/mjs/types/data-structures/priority-queue/min-priority-queue.js +1 -0
  188. package/dist/mjs/types/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  189. package/dist/mjs/types/data-structures/priority-queue/priority-queue.js +1 -0
  190. package/dist/mjs/types/data-structures/priority-queue/priority-queue.js.map +1 -0
  191. package/dist/mjs/types/data-structures/queue/deque.js +1 -0
  192. package/dist/mjs/types/data-structures/queue/deque.js.map +1 -0
  193. package/dist/mjs/types/data-structures/queue/index.js +1 -0
  194. package/dist/mjs/types/data-structures/queue/index.js.map +1 -0
  195. package/dist/mjs/types/data-structures/queue/queue.js +1 -0
  196. package/dist/mjs/types/data-structures/queue/queue.js.map +1 -0
  197. package/dist/mjs/types/data-structures/stack/index.js +1 -0
  198. package/dist/mjs/types/data-structures/stack/index.js.map +1 -0
  199. package/dist/mjs/types/data-structures/stack/stack.js +1 -0
  200. package/dist/mjs/types/data-structures/stack/stack.js.map +1 -0
  201. package/dist/mjs/types/data-structures/tree/index.js +1 -0
  202. package/dist/mjs/types/data-structures/tree/index.js.map +1 -0
  203. package/dist/mjs/types/data-structures/tree/tree.js +1 -0
  204. package/dist/mjs/types/data-structures/tree/tree.js.map +1 -0
  205. package/dist/mjs/types/data-structures/trie/index.js +1 -0
  206. package/dist/mjs/types/data-structures/trie/index.js.map +1 -0
  207. package/dist/mjs/types/data-structures/trie/trie.js +1 -0
  208. package/dist/mjs/types/data-structures/trie/trie.js.map +1 -0
  209. package/dist/mjs/types/index.js +1 -0
  210. package/dist/mjs/types/index.js.map +1 -0
  211. package/dist/mjs/types/utils/index.js +1 -0
  212. package/dist/mjs/types/utils/index.js.map +1 -0
  213. package/dist/mjs/types/utils/utils.js +1 -0
  214. package/dist/mjs/types/utils/utils.js.map +1 -0
  215. package/dist/mjs/types/utils/validate-type.js +1 -0
  216. package/dist/mjs/types/utils/validate-type.js.map +1 -0
  217. package/dist/mjs/utils/index.js +1 -0
  218. package/dist/mjs/utils/index.js.map +1 -0
  219. package/dist/mjs/utils/number.js +1 -0
  220. package/dist/mjs/utils/number.js.map +1 -0
  221. package/dist/mjs/utils/utils.js +1 -0
  222. package/dist/mjs/utils/utils.js.map +1 -0
  223. package/dist/umd/data-structure-typed.js +2 -2
  224. package/dist/umd/data-structure-typed.min.js +1 -1
  225. package/dist/umd/data-structure-typed.min.js.map +1 -1
  226. package/package.json +13 -11
  227. package/src/data-structures/heap/heap.ts +165 -0
  228. package/src/data-structures/linked-list/doubly-linked-list.ts +428 -2
  229. package/test/unit/data-structures/graph/directed-graph.test.ts +14 -0
  230. package/test/unit/data-structures/heap/heap.test.ts +173 -0
  231. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +434 -0
  232. package/test/utils/array.ts +1 -1
  233. package/test/utils/string.ts +49 -1
  234. package/testToExample.ts +215 -0
  235. package/tsconfig-mjs.json +1 -1
@@ -0,0 +1,215 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import * as ts from 'typescript';
4
+ import { toPascalCase } from './test/utils';
5
+
6
+ const isReplaceMD = false;
7
+ const START_MARKER = '[//]: # (No deletion!!! Start of Example Replace Section)';
8
+ const END_MARKER = '[//]: # (No deletion!!! End of Example Replace Section)';
9
+
10
+ /**
11
+ * Recursively retrieve all `.ts` files in a directory.
12
+ */
13
+ function getAllTestFiles(dir: string): string[] {
14
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
15
+
16
+ const files = entries
17
+ .filter(file => !file.isDirectory() && file.name.endsWith('.ts'))
18
+ .map(file => path.join(dir, file.name));
19
+
20
+ const directories = entries.filter(entry => entry.isDirectory());
21
+
22
+ for (const directory of directories) {
23
+ files.push(...getAllTestFiles(path.join(dir, directory.name)));
24
+ }
25
+
26
+ return files;
27
+ }
28
+
29
+ /**
30
+ * Extract test cases with `@example` from TypeScript files using AST.
31
+ */
32
+ function extractExamplesFromFile(filePath: string): { name: string; body: string }[] {
33
+ const fileContent = fs.readFileSync(filePath, 'utf-8');
34
+ const sourceFile = ts.createSourceFile(filePath, fileContent, ts.ScriptTarget.Latest, true);
35
+
36
+ const examples: { name: string; body: string }[] = [];
37
+
38
+ function visit(node: ts.Node) {
39
+ if (
40
+ ts.isCallExpression(node) && // Ensure it's a function call
41
+ node.arguments.length >= 2 && // At least two arguments
42
+ ts.isStringLiteral(node.arguments[0]) && // First argument is a string
43
+ node.arguments[0].text.startsWith('@example') && // Matches @example
44
+ ts.isArrowFunction(node.arguments[1]) // Second argument is an arrow function
45
+ ) {
46
+ const exampleName = node.arguments[0].text.replace('@example ', '').trim();
47
+ const bodyNode = node.arguments[1].body;
48
+
49
+ let exampleBody: string;
50
+ if (ts.isBlock(bodyNode)) {
51
+ // If it's a block, remove outer {}
52
+ exampleBody = bodyNode.statements
53
+ .map(stmt => stmt.getFullText(sourceFile))
54
+ .join('')
55
+ .trim();
56
+ } else {
57
+ // If it's a single expression, use it directly
58
+ exampleBody = bodyNode.getFullText(sourceFile).trim();
59
+ }
60
+
61
+ const transformedBody = exampleBody
62
+ .replace(
63
+ /expect\((.*?)\)\.(toEqual|toBe|toStrictEqual|toHaveLength|toMatchObject)\((.*?)\);/gs, // Use `s` flag for multiline
64
+ (match, actual, method, expected) => {
65
+ expected = expected.replace(/\n/g, '\n //')
66
+ return `console.log(${actual}); // ${expected}`;
67
+ }
68
+ )
69
+ .replace(
70
+ /expect\((.*?)\)\.(toBeUndefined|toBeNull)\(\);/g,
71
+ (match, actual, method) => {
72
+ const expectedValue = method === 'toBeUndefined' ? 'undefined' : 'null';
73
+ return `console.log(${actual}); // ${expectedValue}`;
74
+ }
75
+ )
76
+ .trim();
77
+
78
+ examples.push({ name: exampleName, body: transformedBody });
79
+ }
80
+
81
+ ts.forEachChild(node, visit);
82
+ }
83
+
84
+ visit(sourceFile);
85
+
86
+ return examples;
87
+ }
88
+
89
+ /**
90
+ * Add examples to the corresponding class in the source file.
91
+ */
92
+ function addExamplesToSourceFile(
93
+ sourceFilePath: string,
94
+ className: string,
95
+ examples: { name: string; body: string }[]
96
+ ): void {
97
+ if (!fs.existsSync(sourceFilePath)) {
98
+ console.warn(`Source file not found: ${sourceFilePath}`);
99
+ return;
100
+ }
101
+
102
+ const sourceContent = fs.readFileSync(sourceFilePath, 'utf-8');
103
+ const sourceFile = ts.createSourceFile(sourceFilePath, sourceContent, ts.ScriptTarget.Latest, true);
104
+
105
+ let updatedContent = sourceContent;
106
+
107
+ const classNode = sourceFile.statements.find(
108
+ stmt => ts.isClassDeclaration(stmt) && stmt.name?.text === className
109
+ ) as ts.ClassDeclaration | undefined;
110
+
111
+ if (classNode) {
112
+ const classStart = classNode.getStart(sourceFile);
113
+ const classEnd = classNode.getEnd();
114
+ const classText = classNode.getFullText(sourceFile);
115
+
116
+ // Extract annotation content
117
+ const existingCommentMatch = classText.match(/\/\*\*([\s\S]*?)\*\//);
118
+ if (!existingCommentMatch) {
119
+ console.warn(`No existing comment found for class: ${className}`);
120
+ return;
121
+ }
122
+
123
+ const existingCommentInner = existingCommentMatch[1].replace(/^\n \* /, ''); // Extract comment content (excluding `/**` and `*/`)
124
+
125
+ // Replace @example part
126
+ const exampleSection = examples
127
+ .map(
128
+ example =>
129
+ ` * @example \n * \/\/ ${example.name} \n${example.body
130
+ .split('\n')
131
+ .map(line => ` * ${line}`)
132
+ .join('\n')}`
133
+ )
134
+ .join('\n') + '\n ';
135
+
136
+ let newComment = '';
137
+ if (existingCommentInner.includes('@example')) {
138
+ newComment = existingCommentInner.replace(/ \* @example[\s\S]*?(?=\*\/|$)/g, exampleSection);
139
+ } else {
140
+ newComment = existingCommentInner + `${exampleSection}`;
141
+ }
142
+
143
+
144
+ // Replace original content
145
+ updatedContent =
146
+ sourceContent.slice(0, classStart - existingCommentInner.length - 3) +
147
+ newComment +
148
+ classText.slice(existingCommentMatch[0].length).trim() +
149
+ sourceContent.slice(classEnd);
150
+ }
151
+
152
+ fs.writeFileSync(sourceFilePath, updatedContent, 'utf-8');
153
+ console.log(`Updated examples in ${sourceFilePath}`);
154
+ }
155
+
156
+
157
+ /**
158
+ * Process all test files and update README.md and source files.
159
+ */
160
+ function updateExamples(testDir: string, readmePath: string, sourceBaseDir: string): void {
161
+ const testFiles = getAllTestFiles(testDir);
162
+
163
+ let allExamples: string[] = [];
164
+ for (const file of testFiles) {
165
+ const examples = extractExamplesFromFile(file);
166
+
167
+ if (examples.length === 0) {
168
+ console.log(`No @example found in test file: ${file}`);
169
+ continue;
170
+ }
171
+
172
+ const relativePath = path.relative(testDir, file);
173
+ const sourceFilePath = path.resolve(sourceBaseDir, relativePath.replace('.test.ts', '.ts'));
174
+ const className = path.basename(sourceFilePath, '.ts');
175
+
176
+ addExamplesToSourceFile(sourceFilePath, toPascalCase(className), examples);
177
+
178
+ allExamples = allExamples.concat(
179
+ examples.map(example => `### ${example.name}\n\`\`\`typescript\n${example.body}\n\`\`\``)
180
+ );
181
+ }
182
+
183
+ if (isReplaceMD && allExamples.length > 0) {
184
+ replaceExamplesInReadme(readmePath, allExamples);
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Replace content between markers in README.md.
190
+ */
191
+ function replaceExamplesInReadme(readmePath: string, newExamples: string[]): void {
192
+ const readmeContent = fs.readFileSync(readmePath, 'utf-8');
193
+
194
+ const startIdx = readmeContent.indexOf(START_MARKER);
195
+ const endIdx = readmeContent.indexOf(END_MARKER);
196
+
197
+ if (startIdx === -1 || endIdx === -1) {
198
+ throw new Error(`Markers not found in ${readmePath}`);
199
+ }
200
+
201
+ const before = readmeContent.slice(0, startIdx + START_MARKER.length);
202
+ const after = readmeContent.slice(endIdx);
203
+
204
+ const updatedContent = `${before}\n\n${newExamples.join('\n\n')}\n\n${after}`;
205
+ fs.writeFileSync(readmePath, updatedContent, 'utf-8');
206
+
207
+ console.log(`README.md updated with new examples.`);
208
+ }
209
+
210
+ // Run the script
211
+ const testDir = path.resolve(__dirname, 'test/unit');
212
+ const readmePath = path.resolve(__dirname, 'README.md');
213
+ const sourceBaseDir = path.resolve(__dirname, 'src');
214
+
215
+ updateExamples(testDir, readmePath, sourceBaseDir);
package/tsconfig-mjs.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "module": "ESNext",
7
7
  "target": "ESNext",
8
8
  "moduleResolution": "Node",
9
- "sourceMap": false
9
+ "sourceMap": true
10
10
  },
11
11
  "include": [
12
12
  "./src/**/*.ts"