1313export function fake ( schema : Schema | JSONSchema ) : any ;
1414
1515/**
16- * Gets the tree node with the given name
17- * @param name name - tree node's name
16+ * Retrieves a node from the faker tree by its name.
17+ *
18+ * @param name name - The name of the node to find.
19+ * @returns The matching {@link Node} from the faker tree.
20+ *
1821 * @example
1922 * export default function() {
2023 * const root = findByName(RootName)
21- * root.insert(0, { name: 'foo', test: () => { return true }, fake: () => { return 'foobar' } })
24+ * root.children.unshift({
25+ * name: 'foo',
26+ * fake: () => {
27+ * return 'foobar'
28+ * }
29+ * })
2230 * console.log(fake({type: 'string'}))
31+ * // Expected output: foobar
2332 * }
2433 */
25- export function findByName ( name : string ) : Tree ;
34+ export function findByName ( name : string ) : Node ;
2635
2736/**
28- * The name of the root faker tree
37+ * The name of the root node in the faker tree.
2938 */
30- export const RootName = "Faker " ;
39+ export const ROOT_NAME = "root " ;
3140
3241/**
33- * The Tree object represents a node in the faker tree
42+ * Represents a node in the faker tree.
43+ * Nodes can have children and can generate fake values based on a request.
3444 */
35- export interface Tree {
45+ export interface Node {
3646 /**
37- * Gets the name of the tree node
47+ * The unique name of the node.
3848 */
3949 name : string ;
4050
4151 /**
42- * Inserts a Tree objects after the last child of this tree .
43- * @param node node - A Tree node to insert after the last child .
52+ * Attributes associated with this node .
53+ * These will be used for matching path or schema properties .
4454 */
45- append : ( node : Tree | CustomTree ) => void ;
55+ attributes : string [ ] ;
4656
4757 /**
48- * Inserts a Tree objects at a specified index position
49- * @param index index - The zero-based index position of the insertion.
50- * @param node node - The tree node to insert
58+ * A weight that determines how likely this node is to be selected,
59+ * if multiple nodes match.
5160 */
52- insert : ( index : number , node : Tree | CustomTree ) => void ;
61+ weight : number ;
5362
5463 /**
55- * Removes a Tree node at the specific index position
56- * @param index index - The zero-based index position to remove.
64+ * The child nodes of this node.
5765 */
58- removeAt : ( index : number ) => void ;
66+ children : Array < Node | AddNode > ;
5967
6068 /**
61- * Removes a Tree node with the given name
62- * @param name name - The name of a node to remove
63- */
64- remove : ( name : string ) => void ;
65- }
66-
67- /**
68- * The CustomTree object represents a custom node in the faker tree
69- */
70- export interface CustomTree {
71- /**
72- * Gets the name of the custom tree node
73- */
74- name : string ;
75-
76- /**
77- * Tests whether the tree node supports the request.
78- * @param request request - Request for a new fake value
69+ * Generates a fake value based on the given request.
70+ *
71+ * @param r r - The request describing the value to generate.
72+ * @returns A generated fake value.
73+ *
7974 * @example
8075 * export default function() {
8176 * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
82- * const node = findByName('Strings' )
83- * node.append ({
77+ * const node = findByName(ROOT_NAME )
78+ * node.children.unshift ({
8479 * name: 'Frequency',
85- * test: (r) => { return r.lastName() === 'frequency' },
80+ * attributes: [ 'frequency' ]
8681 * fake: (r) => {
8782 * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
8883 * }
8984 * })
90- * return fake({ type: 'string' })
85+ * console.log(fake({ properties: { frequency } }))
86+ * // Expected output: an object with a frequency attribute containing a random value of frequency
9187 * }
9288 */
93- test : ( r : Request ) => boolean ;
89+ fake : ( r : Request ) => any ;
90+ }
91+
92+ export interface AddNode {
93+ /**
94+ * The unique name of the node.
95+ */
96+ name : string ;
97+
98+ /**
99+ * Attributes associated with this node.
100+ * These will be used for matching path or schema properties.
101+ */
102+ attributes ?: string [ ] ;
103+
104+ /**
105+ * A weight that determines how likely this node is to be selected,
106+ * if multiple nodes match.
107+ */
108+ weight ?: number ;
109+
110+ /**
111+ * The child nodes of this node.
112+ */
113+ children ?: Array < Node | AddNode > ;
94114
95115 /**
96- * Gets a new fake value
97- * @param request request - Request for a new fake value
116+ * Generates a fake value based on the given request.
117+ *
118+ * @param r r - The request describing the value to generate.
119+ * @returns A generated fake value.
120+ *
98121 * @example
99122 * export default function() {
100123 * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
101- * const node = findByName('Strings' )
102- * node.append ({
124+ * const node = findByName(ROOT_NAME )
125+ * node.children.unshift ({
103126 * name: 'Frequency',
104- * test: (r) => { return r.lastName() === 'frequency' },
127+ * attributes: [ 'frequency' ]
105128 * fake: (r) => {
106129 * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
107130 * }
108131 * })
109- * return fake({ type: 'string' })
132+ * console.log(fake({ properties: { frequency } }))
133+ * // Expected output: an object with a frequency attribute containing a random value of frequency
110134 * }
111135 */
112136 fake : ( r : Request ) => any ;
113137}
114138
139+ /**
140+ * Represents the input provided to the `fake` function of a {@link Node}.
141+ */
115142export interface Request {
116- path : PathElement [ ] ;
143+ /**
144+ * The path to the value to generate.
145+ * This may include paths from an API endpoint or the structure of a nested object.
146+ */
147+ path : string [ ] ;
117148
118- last : ( ) => PathElement ;
119- lastName : ( ) => string ;
120- lastSchema : ( ) => JSONSchema ;
149+ /**
150+ * The {@link JSONSchema} definition that describes the value to generate.
151+ */
152+ schema : JSONSchema ;
153+
154+ /**
155+ * A shared context holding values generated by other nodes.
156+ * This allows for interdependent data generation — for example, creating an email
157+ * address using a previously generated first and last name.
158+ */
159+ context : Context
121160}
122161
123- export interface PathElement {
124- name : string ;
125- schema : JSONSchema ;
162+ /**
163+ * A context map holding values generated during the faker tree evaluation.
164+ */
165+ export interface Context {
166+ /**
167+ * A map of names to previously generated values.
168+ */
169+ values : { [ name : string ] : any }
126170}
127171
128172/**
@@ -171,7 +215,7 @@ export interface JSONSchema {
171215 maximum ?: number ;
172216
173217 /**
174- * Restricts the number to a exclusive maximum number
218+ * Restricts the number to an exclusive maximum number
175219 */
176220 exclusiveMaximum ?: number ;
177221
@@ -181,7 +225,7 @@ export interface JSONSchema {
181225 minimum ?: number ;
182226
183227 /**
184- * Restricts the number to a exclusive minimum number
228+ * Restricts the number to an exclusive minimum number
185229 */
186230 exclusiveMinimum ?: number ;
187231
@@ -348,4 +392,4 @@ export interface Schema {
348392
349393 /** Specifies whether all items in an array must be unique. */
350394 uniqueItems ?: boolean ;
351- }
395+ }
0 commit comments