react-children-hooks 0.2.0 → 0.3.0

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.
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ function isElementOfType(element, type) {
3
3
  return element.type === type;
4
4
  }
5
5
 
6
- // src/useChildWhere.ts
6
+ // src/useChildMatching.ts
7
7
  import { useMemo } from "react";
8
8
 
9
9
  // src/childrenToElements.ts
@@ -20,8 +20,8 @@ function childrenToElements(children) {
20
20
  return Children.toArray(children).filter(isReactElement);
21
21
  }
22
22
 
23
- // src/useChildWhere.ts
24
- function useChildWhere(children, predicate) {
23
+ // src/useChildMatching.ts
24
+ function useChildMatching(children, predicate) {
25
25
  return useMemo(
26
26
  () => childrenToElements(children).find(predicate) ?? null,
27
27
  [children, predicate]
@@ -30,50 +30,77 @@ function useChildWhere(children, predicate) {
30
30
 
31
31
  // src/useChildByType.ts
32
32
  function useChildByType(children, type) {
33
- return useChildWhere(
33
+ return useChildMatching(
34
34
  children,
35
35
  (element) => isElementOfType(element, type)
36
36
  );
37
37
  }
38
38
 
39
- // src/useChildrenWhere.ts
39
+ // src/reporter.ts
40
+ import { createReporter } from "runtime-reporter";
41
+ var messages = {
42
+ REQUIRED_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
43
+ MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.",
44
+ MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.",
45
+ EXACT_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.",
46
+ BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Bounded children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected between {{ minimumCount }} and {{ maximumCount }} inclusive."
47
+ };
48
+ var reporter = createReporter(
49
+ process.env.NODE_ENV === "production" ? {} : messages,
50
+ { formatMessage: (message) => message }
51
+ );
52
+ var reporter_default = reporter;
53
+
54
+ // src/useChildrenMatching.ts
40
55
  import { useMemo as useMemo2 } from "react";
41
- function useChildrenWhere(children, predicate) {
56
+ function useChildrenMatching(children, predicate) {
42
57
  return useMemo2(
43
58
  () => childrenToElements(children).filter(predicate),
44
59
  [children, predicate]
45
60
  );
46
61
  }
47
62
 
63
+ // src/useBoundedChildrenMatching.ts
64
+ function useBoundedChildrenMatching(children, predicate, bounds, options) {
65
+ const matchingChildren = useChildrenMatching(children, predicate);
66
+ if (matchingChildren.length >= bounds.minimum && matchingChildren.length <= bounds.maximum) {
67
+ return matchingChildren;
68
+ }
69
+ return reporter_default.fail("BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED", {
70
+ traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
71
+ childNameSegment: options?.childName ? ` for ${options.childName}` : "",
72
+ actualCount: matchingChildren.length,
73
+ actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
74
+ minimumCount: bounds.minimum,
75
+ maximumCount: bounds.maximum
76
+ });
77
+ }
78
+
79
+ // src/useBoundedChildrenByType.ts
80
+ function useBoundedChildrenByType(children, type, bounds, options) {
81
+ return useBoundedChildrenMatching(
82
+ children,
83
+ (element) => isElementOfType(element, type),
84
+ bounds,
85
+ options
86
+ );
87
+ }
88
+
48
89
  // src/useChildrenByType.ts
49
90
  function useChildrenByType(children, type) {
50
- return useChildrenWhere(
91
+ return useChildrenMatching(
51
92
  children,
52
93
  (element) => isElementOfType(element, type)
53
94
  );
54
95
  }
55
96
 
56
- // src/reporter.ts
57
- import { createReporter } from "runtime-reporter";
58
- var messages = {
59
- REQUIRED_CHILD_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
60
- MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.",
61
- MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.",
62
- EXACT_CHILDREN_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}."
63
- };
64
- var reporter = createReporter(
65
- process.env.NODE_ENV === "production" ? {} : messages,
66
- { formatMessage: (message) => message }
67
- );
68
- var reporter_default = reporter;
69
-
70
- // src/useExactChildrenWhere.ts
71
- function useExactChildrenWhere(children, predicate, exactCount, options) {
72
- const matchingChildren = useChildrenWhere(children, predicate);
97
+ // src/useExactChildrenMatching.ts
98
+ function useExactChildrenMatching(children, predicate, exactCount, options) {
99
+ const matchingChildren = useChildrenMatching(children, predicate);
73
100
  if (matchingChildren.length === exactCount) {
74
101
  return matchingChildren;
75
102
  }
76
- return reporter_default.fail("EXACT_CHILDREN_WHERE_PREDICATE_FAILED", {
103
+ return reporter_default.fail("EXACT_CHILDREN_MATCHING_PREDICATE_FAILED", {
77
104
  traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
78
105
  childNameSegment: options?.childName ? ` for ${options.childName}` : "",
79
106
  actualCount: matchingChildren.length,
@@ -82,22 +109,32 @@ function useExactChildrenWhere(children, predicate, exactCount, options) {
82
109
  });
83
110
  }
84
111
 
85
- // src/useHasChildWhere.ts
112
+ // src/useExactChildrenByType.ts
113
+ function useExactChildrenByType(children, type, exactCount, options) {
114
+ return useExactChildrenMatching(
115
+ children,
116
+ (element) => isElementOfType(element, type),
117
+ exactCount,
118
+ options
119
+ );
120
+ }
121
+
122
+ // src/useHasChildMatching.ts
86
123
  import { useMemo as useMemo3 } from "react";
87
- function useHasChildWhere(children, predicate) {
124
+ function useHasChildMatching(children, predicate) {
88
125
  return useMemo3(
89
126
  () => childrenToElements(children).some(predicate),
90
127
  [children, predicate]
91
128
  );
92
129
  }
93
130
 
94
- // src/useMaximumChildrenWhere.ts
95
- function useMaximumChildrenWhere(children, predicate, maximumCount, options) {
96
- const matchingChildren = useChildrenWhere(children, predicate);
131
+ // src/useMaximumChildrenMatching.ts
132
+ function useMaximumChildrenMatching(children, predicate, maximumCount, options) {
133
+ const matchingChildren = useChildrenMatching(children, predicate);
97
134
  if (matchingChildren.length <= maximumCount) {
98
135
  return matchingChildren;
99
136
  }
100
- return reporter_default.fail("MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED", {
137
+ return reporter_default.fail("MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
101
138
  traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
102
139
  childNameSegment: options?.childName ? ` for ${options.childName}` : "",
103
140
  actualCount: matchingChildren.length,
@@ -106,13 +143,23 @@ function useMaximumChildrenWhere(children, predicate, maximumCount, options) {
106
143
  });
107
144
  }
108
145
 
109
- // src/useMinimumChildrenWhere.ts
110
- function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
111
- const matchingChildren = useChildrenWhere(children, predicate);
146
+ // src/useMaximumChildrenByType.ts
147
+ function useMaximumChildrenByType(children, type, maximumCount, options) {
148
+ return useMaximumChildrenMatching(
149
+ children,
150
+ (element) => isElementOfType(element, type),
151
+ maximumCount,
152
+ options
153
+ );
154
+ }
155
+
156
+ // src/useMinimumChildrenMatching.ts
157
+ function useMinimumChildrenMatching(children, predicate, minimumCount, options) {
158
+ const matchingChildren = useChildrenMatching(children, predicate);
112
159
  if (matchingChildren.length >= minimumCount) {
113
160
  return matchingChildren;
114
161
  }
115
- return reporter_default.fail("MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED", {
162
+ return reporter_default.fail("MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
116
163
  traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
117
164
  childNameSegment: options?.childName ? ` for ${options.childName}` : "",
118
165
  actualCount: matchingChildren.length,
@@ -121,29 +168,54 @@ function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
121
168
  });
122
169
  }
123
170
 
124
- // src/useRequiredChildWhere.ts
125
- function useRequiredChildWhere(children, predicate, options) {
126
- const child = useChildWhere(children, predicate);
171
+ // src/useMinimumChildrenByType.ts
172
+ function useMinimumChildrenByType(children, type, minimumCount, options) {
173
+ return useMinimumChildrenMatching(
174
+ children,
175
+ (element) => isElementOfType(element, type),
176
+ minimumCount,
177
+ options
178
+ );
179
+ }
180
+
181
+ // src/useRequiredChildMatching.ts
182
+ function useRequiredChildMatching(children, predicate, options) {
183
+ const child = useChildMatching(children, predicate);
127
184
  if (child !== null) {
128
185
  return child;
129
186
  }
130
- return reporter_default.fail("REQUIRED_CHILD_WHERE_PREDICATE_FAILED", {
187
+ return reporter_default.fail("REQUIRED_CHILD_MATCHING_PREDICATE_FAILED", {
131
188
  traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
132
189
  childNameSegment: options?.childName ? ` for ${options.childName}` : ""
133
190
  });
134
191
  }
192
+
193
+ // src/useRequiredChildByType.ts
194
+ function useRequiredChildByType(children, type, options) {
195
+ return useRequiredChildMatching(
196
+ children,
197
+ (element) => isElementOfType(element, type),
198
+ options
199
+ );
200
+ }
135
201
  export {
136
202
  childrenToElements,
137
203
  isElementOfType,
138
204
  isReactElement,
205
+ useBoundedChildrenByType,
206
+ useBoundedChildrenMatching,
139
207
  useChildByType,
140
- useChildWhere,
208
+ useChildMatching,
141
209
  useChildrenByType,
142
- useChildrenWhere,
143
- useExactChildrenWhere,
144
- useHasChildWhere,
145
- useMaximumChildrenWhere,
146
- useMinimumChildrenWhere,
147
- useRequiredChildWhere
210
+ useChildrenMatching,
211
+ useExactChildrenByType,
212
+ useExactChildrenMatching,
213
+ useHasChildMatching,
214
+ useMaximumChildrenByType,
215
+ useMaximumChildrenMatching,
216
+ useMinimumChildrenByType,
217
+ useMinimumChildrenMatching,
218
+ useRequiredChildByType,
219
+ useRequiredChildMatching
148
220
  };
149
221
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/isElementOfType.ts","../src/useChildWhere.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/useChildByType.ts","../src/useChildrenWhere.ts","../src/useChildrenByType.ts","../src/reporter.ts","../src/useExactChildrenWhere.ts","../src/useHasChildWhere.ts","../src/useMaximumChildrenWhere.ts","../src/useMinimumChildrenWhere.ts","../src/useRequiredChildWhere.ts"],"sourcesContent":["import type { ElementType, ReactElement } from \"react\";\n\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Determines whether a React element exactly matches the provided element or component type.\n *\n * @param element The React element to compare.\n * @param type The element or component type to match.\n * @returns `true` when the element's type exactly matches the provided type; otherwise `false`.\n */\nexport function isElementOfType<T extends ElementType>(\n element: ReactElement,\n type: T\n): element is ElementOfType<T> {\n return element.type === type;\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.\n */\nexport function useChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T | null;\nexport function useChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null;\nexport function useChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null {\n return useMemo(\n () => childrenToElements(children).find(predicate) ?? null,\n [children, predicate]\n );\n}\n","import { Children, type ReactElement, type ReactNode } from \"react\";\n\nimport { isReactElement } from \"./isReactElement\";\n\n/**\n * Normalizes a React children value into an array containing only valid direct child elements.\n *\n * @param children The React children value to normalize.\n * @returns An array of valid React elements from the provided direct children.\n */\nexport function childrenToElements(children: ReactNode): ReactElement[] {\n return Children.toArray(children).filter(isReactElement);\n}\n","import { isValidElement, type ReactElement, type ReactNode } from \"react\";\n\n/**\n * Determines whether a React node is a valid React element.\n *\n * @param node The React node to check.\n * @returns `true` when the node is a valid React element; otherwise `false`.\n */\nexport function isReactElement(node: ReactNode): node is ReactElement {\n return isValidElement(node);\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport { useChildWhere } from \"./useChildWhere\";\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.\n */\nexport function useChildByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T> | null {\n return useChildWhere(children, (element): element is ElementOfType<T> =>\n isElementOfType(element, type)\n );\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.\n * @returns An array of direct child elements that satisfy the provided predicate.\n */\nexport function useChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T[];\nexport function useChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[];\nexport function useChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[] {\n return useMemo(\n () => childrenToElements(children).filter(predicate),\n [children, predicate]\n );\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns An array of direct child elements whose type matches the provided element type.\n */\nexport function useChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T>[] {\n return useChildrenWhere(children, (element): element is ElementOfType<T> =>\n isElementOfType(element, type)\n );\n}\n","import { createReporter, type RuntimeReporterMessages } from \"runtime-reporter\";\n\nconst messages: RuntimeReporterMessages<\n | {\n code: \"REQUIRED_CHILD_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\";\n tokens: \"traceCodePrefix\" | \"childNameSegment\";\n }\n | {\n code: \"MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"minimumCount\";\n }\n | {\n code: \"MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"maximumCount\";\n }\n | {\n code: \"EXACT_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"exactCount\";\n }\n> = {\n REQUIRED_CHILD_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\",\n MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\",\n MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\",\n EXACT_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\"\n};\n\n/** The runtime reporter for react-children-hooks */\nconst reporter = createReporter(\n process.env.NODE_ENV === \"production\" ? ({} as typeof messages) : messages,\n { formatMessage: (message) => message }\n);\n\nexport default reporter;\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseExactChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useExactChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): T[];\nexport function useExactChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): ReactElement[];\nexport function useExactChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length === exactCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"EXACT_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n exactCount\n });\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Determines whether any direct child element satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.\n */\nexport function useHasChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): boolean;\nexport function useHasChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean;\nexport function useHasChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean {\n return useMemo(\n () => childrenToElements(children).some(predicate),\n [children, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseMaximumChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMaximumChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): T[];\nexport function useMaximumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): ReactElement[];\nexport function useMaximumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length <= maximumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n maximumCount\n });\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseMinimumChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMinimumChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): T[];\nexport function useMinimumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): ReactElement[];\nexport function useMinimumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length >= minimumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount\n });\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildWhere } from \"./useChildWhere\";\n\nexport type UseRequiredChildWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element that satisfies the provided predicate.\n */\nexport function useRequiredChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: UseRequiredChildWhereOptions\n): T;\nexport function useRequiredChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: UseRequiredChildWhereOptions\n): ReactElement;\nexport function useRequiredChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: UseRequiredChildWhereOptions\n): ReactElement {\n const child = useChildWhere(children, predicate);\n\n if (child !== null) {\n return child;\n }\n\n return reporter.fail(\"REQUIRED_CHILD_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\"\n });\n}\n"],"mappings":";AAWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,SAAS,eAAkD;;;ACA3D,SAAS,gBAAmD;;;ACA5D,SAAS,sBAAyD;AAQ3D,SAAS,eAAe,MAAuC;AAClE,SAAO,eAAe,IAAI;AAC9B;;;ADAO,SAAS,mBAAmB,UAAqC;AACpE,SAAO,SAAS,QAAQ,QAAQ,EAAE,OAAO,cAAc;AAC3D;;;ADOO,SAAS,cACZ,UACA,WACmB;AACnB,SAAO;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,KAAK,SAAS,KAAK;AAAA,IACtD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;AGdO,SAAS,eACZ,UACA,MACuB;AACvB,SAAO;AAAA,IAAc;AAAA,IAAU,CAAC,YAC5B,gBAAgB,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,SAAS,WAAAA,gBAAkD;AAmBpD,SAAS,iBACZ,UACA,WACc;AACd,SAAOC;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,OAAO,SAAS;AAAA,IACnD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;ACdO,S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;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,IAC9D;AAAA,EACJ,CAAC;AACL;;;ACnBO,SAAS,wBACZ,UACA,WACA,cACA,SACc;AACd,QAAM,mBAAmB,iBAAiB,UAAU,SAAS;AAE7D,MAAI,iBAAiB,UAAU,cAAc;AACzC,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,2CAA2C;AAAA,IAC5D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,IAC9D;AAAA,EACJ,CAAC;AACL;;;ACtBO,SAAS,sBACZ,UACA,WACA,SACY;AACZ,QAAM,QAAQ,cAAc,UAAU,SAAS;AAE/C,MAAI,UAAU,MAAM;AAChB,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,yCAAyC;AAAA,IAC1D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,EACzE,CAAC;AACL;","names":["useMemo","useMemo","useMemo","useMemo"]}
1
+ {"version":3,"sources":["../src/isElementOfType.ts","../src/useChildMatching.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/useChildByType.ts","../src/reporter.ts","../src/useChildrenMatching.ts","../src/useBoundedChildrenMatching.ts","../src/useBoundedChildrenByType.ts","../src/useChildrenByType.ts","../src/useExactChildrenMatching.ts","../src/useExactChildrenByType.ts","../src/useHasChildMatching.ts","../src/useMaximumChildrenMatching.ts","../src/useMaximumChildrenByType.ts","../src/useMinimumChildrenMatching.ts","../src/useMinimumChildrenByType.ts","../src/useRequiredChildMatching.ts","../src/useRequiredChildByType.ts"],"sourcesContent":["import type { ElementType, ReactElement } from \"react\";\n\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Determines whether a React element exactly matches the provided element or component type.\n *\n * @param element The React element to compare.\n * @param type The element or component type to match.\n * @returns `true` when the element's type exactly matches the provided type; otherwise `false`.\n */\nexport function isElementOfType<T extends ElementType>(\n element: ReactElement,\n type: T\n): element is ElementOfType<T> {\n return element.type === type;\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.\n */\nexport function useChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T | null;\nexport function useChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null;\nexport function useChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null {\n return useMemo(\n () => childrenToElements(children).find(predicate) ?? null,\n [children, predicate]\n );\n}\n","import { Children, type ReactElement, type ReactNode } from \"react\";\n\nimport { isReactElement } from \"./isReactElement\";\n\n/**\n * Normalizes a React children value into an array containing only valid direct child elements.\n *\n * @param children The React children value to normalize.\n * @returns An array of valid React elements from the provided direct children.\n */\nexport function childrenToElements(children: ReactNode): ReactElement[] {\n return Children.toArray(children).filter(isReactElement);\n}\n","import { isValidElement, type ReactElement, type ReactNode } from \"react\";\n\n/**\n * Determines whether a React node is a valid React element.\n *\n * @param node The React node to check.\n * @returns `true` when the node is a valid React element; otherwise `false`.\n */\nexport function isReactElement(node: ReactNode): node is ReactElement {\n return isValidElement(node);\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType } from \"./types\";\nimport { useChildMatching } from \"./useChildMatching\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.\n */\nexport function useChildByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T> | null {\n return useChildMatching(children, (element): element is ElementOfType<T> =>\n isElementOfType(element, type)\n );\n}\n","import { createReporter, type RuntimeReporterMessages } from \"runtime-reporter\";\n\nconst messages: RuntimeReporterMessages<\n | {\n code: \"REQUIRED_CHILD_MATCHING_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\";\n tokens: \"traceCodePrefix\" | \"childNameSegment\";\n }\n | {\n code: \"MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"minimumCount\";\n }\n | {\n code: \"MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"maximumCount\";\n }\n | {\n code: \"EXACT_CHILDREN_MATCHING_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"exactCount\";\n }\n | {\n code: \"BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Bounded children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected between {{ minimumCount }} and {{ maximumCount }} inclusive.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"minimumCount\"\n | \"maximumCount\";\n }\n> = {\n REQUIRED_CHILD_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\",\n MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\",\n MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\",\n EXACT_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\",\n BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Bounded children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected between {{ minimumCount }} and {{ maximumCount }} inclusive.\"\n};\n\n/** The runtime reporter for react-children-hooks */\nconst reporter = createReporter(\n process.env.NODE_ENV === \"production\" ? ({} as typeof messages) : messages,\n { formatMessage: (message) => message }\n);\n\nexport default reporter;\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.\n * @returns An array of direct child elements that satisfy the provided predicate.\n */\nexport function useChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T[];\nexport function useChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[];\nexport function useChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[] {\n return useMemo(\n () => childrenToElements(children).filter(predicate),\n [children, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ChildrenCountBounds, ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the count falls outside the inclusive bounds.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useBoundedChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): T[];\nexport function useBoundedChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ReactElement[];\nexport function useBoundedChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate);\n\n if (\n matchingChildren.length >= bounds.minimum &&\n matchingChildren.length <= bounds.maximum\n ) {\n return matchingChildren;\n }\n\n return reporter.fail(\"BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount: bounds.minimum,\n maximumCount: bounds.maximum\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type {\n ChildrenCountBounds,\n ElementOfType,\n ValidationOptions\n} from \"./types\";\nimport { useBoundedChildrenMatching } from \"./useBoundedChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the count falls outside the inclusive bounds.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useBoundedChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useBoundedChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n bounds,\n options\n );\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns An array of direct child elements whose type matches the provided element type.\n */\nexport function useChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T>[] {\n return useChildrenMatching(\n children,\n (element): element is ElementOfType<T> => isElementOfType(element, type)\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useExactChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n exactCount: number,\n options?: ValidationOptions\n): T[];\nexport function useExactChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useExactChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate);\n\n if (matchingChildren.length === exactCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"EXACT_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n exactCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useExactChildrenMatching } from \"./useExactChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useExactChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n exactCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useExactChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n exactCount,\n options\n );\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Determines whether any direct child element satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.\n */\nexport function useHasChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): boolean;\nexport function useHasChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean;\nexport function useHasChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean {\n return useMemo(\n () => childrenToElements(children).some(predicate),\n [children, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMaximumChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n maximumCount: number,\n options?: ValidationOptions\n): T[];\nexport function useMaximumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useMaximumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate);\n\n if (matchingChildren.length <= maximumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n maximumCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useMaximumChildrenMatching } from \"./useMaximumChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useMaximumChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n maximumCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useMaximumChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n maximumCount,\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMinimumChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n minimumCount: number,\n options?: ValidationOptions\n): T[];\nexport function useMinimumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useMinimumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate);\n\n if (matchingChildren.length >= minimumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useMinimumChildrenMatching } from \"./useMinimumChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useMinimumChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n minimumCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useMinimumChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n minimumCount,\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildMatching } from \"./useChildMatching\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element that satisfies the provided predicate.\n */\nexport function useRequiredChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: ValidationOptions\n): T;\nexport function useRequiredChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement;\nexport function useRequiredChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement {\n const child = useChildMatching(children, predicate);\n\n if (child !== null) {\n return child;\n }\n\n return reporter.fail(\"REQUIRED_CHILD_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\"\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useRequiredChildMatching } from \"./useRequiredChildMatching\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element whose type matches the provided element type.\n */\nexport function useRequiredChildByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: ValidationOptions\n): ElementOfType<T> {\n return useRequiredChildMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n"],"mappings":";AAWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,SAAS,eAAkD;;;ACA3D,SAAS,gBAAmD;;;ACA5D,SAAS,sBAAyD;AAQ3D,SAAS,eAAe,MAAuC;AAClE,SAAO,eAAe,IAAI;AAC9B;;;ADAO,SAAS,mBAAmB,UAAqC;AACpE,SAAO,SAAS,QAAQ,QAAQ,EAAE,OAAO,cAAc;AAC3D;;;ADOO,SAAS,iBACZ,UACA,WACmB;AACnB,SAAO;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,KAAK,SAAS,KAAK;AAAA,IACtD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;AGdO,SAAS,eACZ,UACA,MACuB;AACvB,SAAO;AAAA,IAAiB;AAAA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AAS,IAAI;AAAA,IACjC;AAAA,IACA;AAAA,EACJ;AACJ;;;ACDO,SAAS,2BACZ,UACA,WACA,cACA,SACc;AACd,QAAM,mBAAmB,oBAAoB,UAAU,SAAS;AAEhE,MAAI,iBAAiB,UAAU,cAAc;AACzC,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,8CAA8C;AAAA,IAC/D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,IAC9D;AAAA,EACJ,CAAC;AACL;;;AC/BO,SAAS,yBACZ,UACA,MACA,cACA,SACkB;AAClB,SAAO;AAAA,IACH;AAAA,IACA,CAAC,YACG,gBAAgB,SAAS,IAAI;AAAA,IACjC;AAAA,IACA;AAAA,EACJ;AACJ;;;ACJO,SAAS,yBACZ,UACA,WACA,SACY;AACZ,QAAM,QAAQ,iBAAiB,UAAU,SAAS;AAElD,MAAI,UAAU,MAAM;AAChB,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,4CAA4C;AAAA,IAC7D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,EACzE,CAAC;AACL;;;ACzBO,SAAS,uBACZ,UACA,MACA,SACgB;AAChB,SAAO;AAAA,IACH;AAAA,IACA,CAAC,YACG,gBAAgB,SAAS,IAAI;AAAA,IACjC;AAAA,EACJ;AACJ;","names":["useMemo","useMemo","useMemo","useMemo"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-children-hooks",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "React hooks for inspecting, traversing, querying, and validating props.children.",
5
5
  "keywords": [
6
6
  "react",