react-children-hooks 0.1.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/README.md +24 -11
- package/dist/index.cjs +152 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +127 -31
- package/dist/index.d.ts +127 -31
- package/dist/index.js +139 -33
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,23 +6,36 @@
|
|
|
6
6
|
|
|
7
7
|
### Query Hooks
|
|
8
8
|
|
|
9
|
-
- [`useChildByType`](./docs/
|
|
10
|
-
- [`
|
|
11
|
-
- [`useChildrenByType`](./docs/
|
|
12
|
-
- [`
|
|
9
|
+
- [`useChildByType`](./docs/useChildByType.md)
|
|
10
|
+
- [`useChildMatching`](./docs/useChildMatching.md)
|
|
11
|
+
- [`useChildrenByType`](./docs/useChildrenByType.md)
|
|
12
|
+
- [`useChildrenMatching`](./docs/useChildrenMatching.md)
|
|
13
|
+
|
|
14
|
+
### Validation + Query Hooks
|
|
15
|
+
|
|
16
|
+
- [`useBoundedChildrenByType`](./docs/useBoundedChildrenByType.md)
|
|
17
|
+
- [`useBoundedChildrenMatching`](./docs/useBoundedChildrenMatching.md)
|
|
18
|
+
- [`useExactChildrenByType`](./docs/useExactChildrenByType.md)
|
|
19
|
+
- [`useExactChildrenMatching`](./docs/useExactChildrenMatching.md)
|
|
20
|
+
- [`useMaximumChildrenByType`](./docs/useMaximumChildrenByType.md)
|
|
21
|
+
- [`useMaximumChildrenMatching`](./docs/useMaximumChildrenMatching.md)
|
|
22
|
+
- [`useMinimumChildrenByType`](./docs/useMinimumChildrenByType.md)
|
|
23
|
+
- [`useMinimumChildrenMatching`](./docs/useMinimumChildrenMatching.md)
|
|
24
|
+
- [`useRequiredChildByType`](./docs/useRequiredChildByType.md)
|
|
25
|
+
- [`useRequiredChildMatching`](./docs/useRequiredChildMatching.md)
|
|
13
26
|
|
|
14
27
|
### Validation Hooks
|
|
15
28
|
|
|
16
|
-
- [`
|
|
17
|
-
- [`useMinimumChildrenWhere`](./docs/api/useMinimumChildrenWhere.md)
|
|
18
|
-
- [`useRequiredChildWhere`](./docs/api/useRequiredChildWhere.md)
|
|
29
|
+
- [`useHasChildMatching`](./docs/useHasChildMatching.md)
|
|
19
30
|
|
|
20
31
|
### Utilities
|
|
21
32
|
|
|
22
|
-
- [`childrenToElements`](./docs/
|
|
23
|
-
- [`isElementOfType`](./docs/
|
|
24
|
-
- [`isReactElement`](./docs/
|
|
33
|
+
- [`childrenToElements`](./docs/childrenToElements.md)
|
|
34
|
+
- [`isElementOfType`](./docs/isElementOfType.md)
|
|
35
|
+
- [`isReactElement`](./docs/isReactElement.md)
|
|
25
36
|
|
|
26
37
|
### Types
|
|
27
38
|
|
|
28
|
-
-
|
|
39
|
+
- `ChildrenCountBounds`
|
|
40
|
+
- `ElementOfType`
|
|
41
|
+
- `ValidationOptions`
|
package/dist/index.cjs
CHANGED
|
@@ -23,13 +23,21 @@ __export(index_exports, {
|
|
|
23
23
|
childrenToElements: () => childrenToElements,
|
|
24
24
|
isElementOfType: () => isElementOfType,
|
|
25
25
|
isReactElement: () => isReactElement,
|
|
26
|
+
useBoundedChildrenByType: () => useBoundedChildrenByType,
|
|
27
|
+
useBoundedChildrenMatching: () => useBoundedChildrenMatching,
|
|
26
28
|
useChildByType: () => useChildByType,
|
|
27
|
-
|
|
29
|
+
useChildMatching: () => useChildMatching,
|
|
28
30
|
useChildrenByType: () => useChildrenByType,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
useChildrenMatching: () => useChildrenMatching,
|
|
32
|
+
useExactChildrenByType: () => useExactChildrenByType,
|
|
33
|
+
useExactChildrenMatching: () => useExactChildrenMatching,
|
|
34
|
+
useHasChildMatching: () => useHasChildMatching,
|
|
35
|
+
useMaximumChildrenByType: () => useMaximumChildrenByType,
|
|
36
|
+
useMaximumChildrenMatching: () => useMaximumChildrenMatching,
|
|
37
|
+
useMinimumChildrenByType: () => useMinimumChildrenByType,
|
|
38
|
+
useMinimumChildrenMatching: () => useMinimumChildrenMatching,
|
|
39
|
+
useRequiredChildByType: () => useRequiredChildByType,
|
|
40
|
+
useRequiredChildMatching: () => useRequiredChildMatching
|
|
33
41
|
});
|
|
34
42
|
module.exports = __toCommonJS(index_exports);
|
|
35
43
|
|
|
@@ -38,7 +46,7 @@ function isElementOfType(element, type) {
|
|
|
38
46
|
return element.type === type;
|
|
39
47
|
}
|
|
40
48
|
|
|
41
|
-
// src/
|
|
49
|
+
// src/useChildMatching.ts
|
|
42
50
|
var import_react3 = require("react");
|
|
43
51
|
|
|
44
52
|
// src/childrenToElements.ts
|
|
@@ -55,8 +63,8 @@ function childrenToElements(children) {
|
|
|
55
63
|
return import_react2.Children.toArray(children).filter(isReactElement);
|
|
56
64
|
}
|
|
57
65
|
|
|
58
|
-
// src/
|
|
59
|
-
function
|
|
66
|
+
// src/useChildMatching.ts
|
|
67
|
+
function useChildMatching(children, predicate) {
|
|
60
68
|
return (0, import_react3.useMemo)(
|
|
61
69
|
() => childrenToElements(children).find(predicate) ?? null,
|
|
62
70
|
[children, predicate]
|
|
@@ -65,57 +73,136 @@ function useChildWhere(children, predicate) {
|
|
|
65
73
|
|
|
66
74
|
// src/useChildByType.ts
|
|
67
75
|
function useChildByType(children, type) {
|
|
68
|
-
return
|
|
76
|
+
return useChildMatching(
|
|
69
77
|
children,
|
|
70
78
|
(element) => isElementOfType(element, type)
|
|
71
79
|
);
|
|
72
80
|
}
|
|
73
81
|
|
|
74
|
-
// src/
|
|
82
|
+
// src/reporter.ts
|
|
83
|
+
var import_runtime_reporter = require("runtime-reporter");
|
|
84
|
+
var messages = {
|
|
85
|
+
REQUIRED_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
|
|
86
|
+
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 }}.",
|
|
87
|
+
MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.",
|
|
88
|
+
EXACT_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.",
|
|
89
|
+
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."
|
|
90
|
+
};
|
|
91
|
+
var reporter = (0, import_runtime_reporter.createReporter)(
|
|
92
|
+
process.env.NODE_ENV === "production" ? {} : messages,
|
|
93
|
+
{ formatMessage: (message) => message }
|
|
94
|
+
);
|
|
95
|
+
var reporter_default = reporter;
|
|
96
|
+
|
|
97
|
+
// src/useChildrenMatching.ts
|
|
75
98
|
var import_react4 = require("react");
|
|
76
|
-
function
|
|
99
|
+
function useChildrenMatching(children, predicate) {
|
|
77
100
|
return (0, import_react4.useMemo)(
|
|
78
101
|
() => childrenToElements(children).filter(predicate),
|
|
79
102
|
[children, predicate]
|
|
80
103
|
);
|
|
81
104
|
}
|
|
82
105
|
|
|
106
|
+
// src/useBoundedChildrenMatching.ts
|
|
107
|
+
function useBoundedChildrenMatching(children, predicate, bounds, options) {
|
|
108
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
109
|
+
if (matchingChildren.length >= bounds.minimum && matchingChildren.length <= bounds.maximum) {
|
|
110
|
+
return matchingChildren;
|
|
111
|
+
}
|
|
112
|
+
return reporter_default.fail("BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
113
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
114
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
115
|
+
actualCount: matchingChildren.length,
|
|
116
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
117
|
+
minimumCount: bounds.minimum,
|
|
118
|
+
maximumCount: bounds.maximum
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/useBoundedChildrenByType.ts
|
|
123
|
+
function useBoundedChildrenByType(children, type, bounds, options) {
|
|
124
|
+
return useBoundedChildrenMatching(
|
|
125
|
+
children,
|
|
126
|
+
(element) => isElementOfType(element, type),
|
|
127
|
+
bounds,
|
|
128
|
+
options
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
83
132
|
// src/useChildrenByType.ts
|
|
84
133
|
function useChildrenByType(children, type) {
|
|
85
|
-
return
|
|
134
|
+
return useChildrenMatching(
|
|
86
135
|
children,
|
|
87
136
|
(element) => isElementOfType(element, type)
|
|
88
137
|
);
|
|
89
138
|
}
|
|
90
139
|
|
|
91
|
-
// src/
|
|
140
|
+
// src/useExactChildrenMatching.ts
|
|
141
|
+
function useExactChildrenMatching(children, predicate, exactCount, options) {
|
|
142
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
143
|
+
if (matchingChildren.length === exactCount) {
|
|
144
|
+
return matchingChildren;
|
|
145
|
+
}
|
|
146
|
+
return reporter_default.fail("EXACT_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
147
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
148
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
149
|
+
actualCount: matchingChildren.length,
|
|
150
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
151
|
+
exactCount
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/useExactChildrenByType.ts
|
|
156
|
+
function useExactChildrenByType(children, type, exactCount, options) {
|
|
157
|
+
return useExactChildrenMatching(
|
|
158
|
+
children,
|
|
159
|
+
(element) => isElementOfType(element, type),
|
|
160
|
+
exactCount,
|
|
161
|
+
options
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/useHasChildMatching.ts
|
|
92
166
|
var import_react5 = require("react");
|
|
93
|
-
function
|
|
167
|
+
function useHasChildMatching(children, predicate) {
|
|
94
168
|
return (0, import_react5.useMemo)(
|
|
95
169
|
() => childrenToElements(children).some(predicate),
|
|
96
170
|
[children, predicate]
|
|
97
171
|
);
|
|
98
172
|
}
|
|
99
173
|
|
|
100
|
-
// src/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
174
|
+
// src/useMaximumChildrenMatching.ts
|
|
175
|
+
function useMaximumChildrenMatching(children, predicate, maximumCount, options) {
|
|
176
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
177
|
+
if (matchingChildren.length <= maximumCount) {
|
|
178
|
+
return matchingChildren;
|
|
179
|
+
}
|
|
180
|
+
return reporter_default.fail("MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
181
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
182
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
183
|
+
actualCount: matchingChildren.length,
|
|
184
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
185
|
+
maximumCount
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/useMaximumChildrenByType.ts
|
|
190
|
+
function useMaximumChildrenByType(children, type, maximumCount, options) {
|
|
191
|
+
return useMaximumChildrenMatching(
|
|
192
|
+
children,
|
|
193
|
+
(element) => isElementOfType(element, type),
|
|
194
|
+
maximumCount,
|
|
195
|
+
options
|
|
196
|
+
);
|
|
197
|
+
}
|
|
111
198
|
|
|
112
|
-
// src/
|
|
113
|
-
function
|
|
114
|
-
const matchingChildren =
|
|
199
|
+
// src/useMinimumChildrenMatching.ts
|
|
200
|
+
function useMinimumChildrenMatching(children, predicate, minimumCount, options) {
|
|
201
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
115
202
|
if (matchingChildren.length >= minimumCount) {
|
|
116
203
|
return matchingChildren;
|
|
117
204
|
}
|
|
118
|
-
return reporter_default.fail("
|
|
205
|
+
return reporter_default.fail("MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
119
206
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
120
207
|
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
121
208
|
actualCount: matchingChildren.length,
|
|
@@ -124,28 +211,55 @@ function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
|
|
|
124
211
|
});
|
|
125
212
|
}
|
|
126
213
|
|
|
127
|
-
// src/
|
|
128
|
-
function
|
|
129
|
-
|
|
214
|
+
// src/useMinimumChildrenByType.ts
|
|
215
|
+
function useMinimumChildrenByType(children, type, minimumCount, options) {
|
|
216
|
+
return useMinimumChildrenMatching(
|
|
217
|
+
children,
|
|
218
|
+
(element) => isElementOfType(element, type),
|
|
219
|
+
minimumCount,
|
|
220
|
+
options
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/useRequiredChildMatching.ts
|
|
225
|
+
function useRequiredChildMatching(children, predicate, options) {
|
|
226
|
+
const child = useChildMatching(children, predicate);
|
|
130
227
|
if (child !== null) {
|
|
131
228
|
return child;
|
|
132
229
|
}
|
|
133
|
-
return reporter_default.fail("
|
|
230
|
+
return reporter_default.fail("REQUIRED_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
134
231
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
135
232
|
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
136
233
|
});
|
|
137
234
|
}
|
|
235
|
+
|
|
236
|
+
// src/useRequiredChildByType.ts
|
|
237
|
+
function useRequiredChildByType(children, type, options) {
|
|
238
|
+
return useRequiredChildMatching(
|
|
239
|
+
children,
|
|
240
|
+
(element) => isElementOfType(element, type),
|
|
241
|
+
options
|
|
242
|
+
);
|
|
243
|
+
}
|
|
138
244
|
// Annotate the CommonJS export names for ESM import in node:
|
|
139
245
|
0 && (module.exports = {
|
|
140
246
|
childrenToElements,
|
|
141
247
|
isElementOfType,
|
|
142
248
|
isReactElement,
|
|
249
|
+
useBoundedChildrenByType,
|
|
250
|
+
useBoundedChildrenMatching,
|
|
143
251
|
useChildByType,
|
|
144
|
-
|
|
252
|
+
useChildMatching,
|
|
145
253
|
useChildrenByType,
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
254
|
+
useChildrenMatching,
|
|
255
|
+
useExactChildrenByType,
|
|
256
|
+
useExactChildrenMatching,
|
|
257
|
+
useHasChildMatching,
|
|
258
|
+
useMaximumChildrenByType,
|
|
259
|
+
useMaximumChildrenMatching,
|
|
260
|
+
useMinimumChildrenByType,
|
|
261
|
+
useMinimumChildrenMatching,
|
|
262
|
+
useRequiredChildByType,
|
|
263
|
+
useRequiredChildMatching
|
|
150
264
|
});
|
|
151
265
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/isElementOfType.ts","../src/useChildWhere.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/useChildByType.ts","../src/useChildrenWhere.ts","../src/useChildrenByType.ts","../src/useHasChildWhere.ts","../src/reporter.ts","../src/useMinimumChildrenWhere.ts","../src/useRequiredChildWhere.ts"],"sourcesContent":["/**\n * Public package entrypoint.\n *\n * Hooks and child utilities will be exported here as the API is defined.\n */\nexport { useChildByType } from \"./useChildByType\";\nexport { useChildWhere } from \"./useChildWhere\";\nexport { useChildrenByType } from \"./useChildrenByType\";\nexport { useChildrenWhere } from \"./useChildrenWhere\";\nexport { useHasChildWhere } from \"./useHasChildWhere\";\nexport { useMinimumChildrenWhere } from \"./useMinimumChildrenWhere\";\nexport { useRequiredChildWhere } from \"./useRequiredChildWhere\";\nexport { childrenToElements } from \"./childrenToElements\";\nexport { isElementOfType } from \"./isElementOfType\";\nexport { isReactElement } from \"./isReactElement\";\nexport type { ElementOfType } from \"./types\";\nexport type { UseMinimumChildrenWhereOptions } from \"./useMinimumChildrenWhere\";\nexport type { UseRequiredChildWhereOptions } from \"./useRequiredChildWhere\";\n","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 { 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 { 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 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};\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 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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,IA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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":["import_react","import_react","import_react","import_react"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../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":["/**\n * Public package entrypoint.\n *\n * Hooks and child utilities will be exported here as the API is defined.\n */\nexport { useChildByType } from \"./useChildByType\";\nexport { useChildMatching } from \"./useChildMatching\";\nexport { useBoundedChildrenByType } from \"./useBoundedChildrenByType\";\nexport { useBoundedChildrenMatching } from \"./useBoundedChildrenMatching\";\nexport { useChildrenByType } from \"./useChildrenByType\";\nexport { useChildrenMatching } from \"./useChildrenMatching\";\nexport { useExactChildrenByType } from \"./useExactChildrenByType\";\nexport { useExactChildrenMatching } from \"./useExactChildrenMatching\";\nexport { useHasChildMatching } from \"./useHasChildMatching\";\nexport { useMaximumChildrenByType } from \"./useMaximumChildrenByType\";\nexport { useMaximumChildrenMatching } from \"./useMaximumChildrenMatching\";\nexport { useMinimumChildrenByType } from \"./useMinimumChildrenByType\";\nexport { useMinimumChildrenMatching } from \"./useMinimumChildrenMatching\";\nexport { useRequiredChildByType } from \"./useRequiredChildByType\";\nexport { useRequiredChildMatching } from \"./useRequiredChildMatching\";\nexport { childrenToElements } from \"./childrenToElements\";\nexport { isElementOfType } from \"./isElementOfType\";\nexport { isReactElement } from \"./isReactElement\";\nexport type {\n ChildrenCountBounds,\n ElementOfType,\n ValidationOptions\n} from \"./types\";\n","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 useRequired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AC,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":["import_react","import_react","import_react","import_react"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,38 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { ElementType, ReactNode, ReactElement } from 'react';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Inclusive minimum and maximum bounds used by the bounded validation hooks.
|
|
6
|
+
*/
|
|
7
|
+
type ChildrenCountBounds = {
|
|
8
|
+
/**
|
|
9
|
+
* The minimum number of matching direct child elements required.
|
|
10
|
+
*/
|
|
11
|
+
minimum: number;
|
|
12
|
+
/**
|
|
13
|
+
* The maximum number of matching direct child elements allowed.
|
|
14
|
+
*/
|
|
15
|
+
maximum: number;
|
|
16
|
+
};
|
|
4
17
|
/**
|
|
5
18
|
* Represents a React element whose props and element type are narrowed to the provided React element type.
|
|
6
19
|
*
|
|
7
20
|
* @typeParam T The React element type used to narrow the element's props and type.
|
|
8
21
|
*/
|
|
9
22
|
type ElementOfType<T extends React.ElementType> = React.ReactElement<React.ComponentProps<T>, T>;
|
|
23
|
+
/**
|
|
24
|
+
* Optional reporting metadata used by the validation hooks to derive thrown validation messages.
|
|
25
|
+
*/
|
|
26
|
+
type ValidationOptions = {
|
|
27
|
+
/**
|
|
28
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
29
|
+
*/
|
|
30
|
+
traceCode?: string;
|
|
31
|
+
/**
|
|
32
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
33
|
+
*/
|
|
34
|
+
childName?: string;
|
|
35
|
+
};
|
|
10
36
|
|
|
11
37
|
/**
|
|
12
38
|
* Returns the first direct child element whose React element type exactly matches the provided type.
|
|
@@ -24,8 +50,31 @@ declare function useChildByType<T extends ElementType>(children: ReactNode, type
|
|
|
24
50
|
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
25
51
|
* @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.
|
|
26
52
|
*/
|
|
27
|
-
declare function
|
|
28
|
-
declare function
|
|
53
|
+
declare function useChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T | null;
|
|
54
|
+
declare function useChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement | null;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when the count falls outside the inclusive bounds.
|
|
58
|
+
*
|
|
59
|
+
* @param children The React children value to inspect.
|
|
60
|
+
* @param type The element or component type to match against each direct child element.
|
|
61
|
+
* @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
|
|
62
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
63
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
64
|
+
*/
|
|
65
|
+
declare function useBoundedChildrenByType<T extends ElementType>(children: ReactNode, type: T, bounds: ChildrenCountBounds, options?: ValidationOptions): ElementOfType<T>[];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when the count falls outside the inclusive bounds.
|
|
69
|
+
*
|
|
70
|
+
* @param children The React children value to inspect.
|
|
71
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
72
|
+
* @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
|
|
73
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
74
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
75
|
+
*/
|
|
76
|
+
declare function useBoundedChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, bounds: ChildrenCountBounds, options?: ValidationOptions): T[];
|
|
77
|
+
declare function useBoundedChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, bounds: ChildrenCountBounds, options?: ValidationOptions): ReactElement[];
|
|
29
78
|
|
|
30
79
|
/**
|
|
31
80
|
* Returns the direct child elements whose React element type exactly matches the provided type.
|
|
@@ -43,8 +92,31 @@ declare function useChildrenByType<T extends ElementType>(children: ReactNode, t
|
|
|
43
92
|
* @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.
|
|
44
93
|
* @returns An array of direct child elements that satisfy the provided predicate.
|
|
45
94
|
*/
|
|
46
|
-
declare function
|
|
47
|
-
declare function
|
|
95
|
+
declare function useChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T[];
|
|
96
|
+
declare function useChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement[];
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when the exact count is not met.
|
|
100
|
+
*
|
|
101
|
+
* @param children The React children value to inspect.
|
|
102
|
+
* @param type The element or component type to match against each direct child element.
|
|
103
|
+
* @param exactCount The exact number of matching direct child elements required.
|
|
104
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
105
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
106
|
+
*/
|
|
107
|
+
declare function useExactChildrenByType<T extends ElementType>(children: ReactNode, type: T, exactCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.
|
|
111
|
+
*
|
|
112
|
+
* @param children The React children value to inspect.
|
|
113
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
114
|
+
* @param exactCount The exact number of matching direct child elements required.
|
|
115
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
116
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
117
|
+
*/
|
|
118
|
+
declare function useExactChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, exactCount: number, options?: ValidationOptions): T[];
|
|
119
|
+
declare function useExactChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, exactCount: number, options?: ValidationOptions): ReactElement[];
|
|
48
120
|
|
|
49
121
|
/**
|
|
50
122
|
* Determines whether any direct child element satisfies the provided predicate.
|
|
@@ -53,19 +125,43 @@ declare function useChildrenWhere(children: ReactNode, predicate: (element: Reac
|
|
|
53
125
|
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
54
126
|
* @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.
|
|
55
127
|
*/
|
|
56
|
-
declare function
|
|
57
|
-
declare function
|
|
128
|
+
declare function useHasChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): boolean;
|
|
129
|
+
declare function useHasChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): boolean;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when more than the maximum count are found.
|
|
133
|
+
*
|
|
134
|
+
* @param children The React children value to inspect.
|
|
135
|
+
* @param type The element or component type to match against each direct child element.
|
|
136
|
+
* @param maximumCount The maximum number of matching direct child elements allowed.
|
|
137
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
138
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
139
|
+
*/
|
|
140
|
+
declare function useMaximumChildrenByType<T extends ElementType>(children: ReactNode, type: T, maximumCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.
|
|
144
|
+
*
|
|
145
|
+
* @param children The React children value to inspect.
|
|
146
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
147
|
+
* @param maximumCount The maximum number of matching direct child elements allowed.
|
|
148
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
149
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
150
|
+
*/
|
|
151
|
+
declare function useMaximumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, maximumCount: number, options?: ValidationOptions): T[];
|
|
152
|
+
declare function useMaximumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, maximumCount: number, options?: ValidationOptions): ReactElement[];
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when fewer than the minimum count are found.
|
|
156
|
+
*
|
|
157
|
+
* @param children The React children value to inspect.
|
|
158
|
+
* @param type The element or component type to match against each direct child element.
|
|
159
|
+
* @param minimumCount The minimum number of matching direct child elements required.
|
|
160
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
161
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
162
|
+
*/
|
|
163
|
+
declare function useMinimumChildrenByType<T extends ElementType>(children: ReactNode, type: T, minimumCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
58
164
|
|
|
59
|
-
type UseMinimumChildrenWhereOptions = {
|
|
60
|
-
/**
|
|
61
|
-
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
62
|
-
*/
|
|
63
|
-
traceCode?: string;
|
|
64
|
-
/**
|
|
65
|
-
* An optional human-readable child name that is included in the thrown validation message.
|
|
66
|
-
*/
|
|
67
|
-
childName?: string;
|
|
68
|
-
};
|
|
69
165
|
/**
|
|
70
166
|
* Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.
|
|
71
167
|
*
|
|
@@ -75,19 +171,19 @@ type UseMinimumChildrenWhereOptions = {
|
|
|
75
171
|
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
76
172
|
* @returns The direct child elements that satisfy the provided predicate.
|
|
77
173
|
*/
|
|
78
|
-
declare function
|
|
79
|
-
declare function
|
|
174
|
+
declare function useMinimumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: ValidationOptions): T[];
|
|
175
|
+
declare function useMinimumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: ValidationOptions): ReactElement[];
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the first direct child element whose React element type exactly matches the provided type, or throws when no match is found.
|
|
179
|
+
*
|
|
180
|
+
* @param children The React children value to inspect.
|
|
181
|
+
* @param type The element or component type to match against each direct child element.
|
|
182
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
183
|
+
* @returns The first direct child element whose type matches the provided element type.
|
|
184
|
+
*/
|
|
185
|
+
declare function useRequiredChildByType<T extends ElementType>(children: ReactNode, type: T, options?: ValidationOptions): ElementOfType<T>;
|
|
80
186
|
|
|
81
|
-
type UseRequiredChildWhereOptions = {
|
|
82
|
-
/**
|
|
83
|
-
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
84
|
-
*/
|
|
85
|
-
traceCode?: string;
|
|
86
|
-
/**
|
|
87
|
-
* An optional human-readable child name that is included in the thrown validation message.
|
|
88
|
-
*/
|
|
89
|
-
childName?: string;
|
|
90
|
-
};
|
|
91
187
|
/**
|
|
92
188
|
* Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.
|
|
93
189
|
*
|
|
@@ -96,8 +192,8 @@ type UseRequiredChildWhereOptions = {
|
|
|
96
192
|
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
97
193
|
* @returns The first direct child element that satisfies the provided predicate.
|
|
98
194
|
*/
|
|
99
|
-
declare function
|
|
100
|
-
declare function
|
|
195
|
+
declare function useRequiredChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: ValidationOptions): T;
|
|
196
|
+
declare function useRequiredChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: ValidationOptions): ReactElement;
|
|
101
197
|
|
|
102
198
|
/**
|
|
103
199
|
* Normalizes a React children value into an array containing only valid direct child elements.
|
|
@@ -124,4 +220,4 @@ declare function isElementOfType<T extends ElementType>(element: ReactElement, t
|
|
|
124
220
|
*/
|
|
125
221
|
declare function isReactElement(node: ReactNode): node is ReactElement;
|
|
126
222
|
|
|
127
|
-
export { type
|
|
223
|
+
export { type ChildrenCountBounds, type ElementOfType, type ValidationOptions, childrenToElements, isElementOfType, isReactElement, useBoundedChildrenByType, useBoundedChildrenMatching, useChildByType, useChildMatching, useChildrenByType, useChildrenMatching, useExactChildrenByType, useExactChildrenMatching, useHasChildMatching, useMaximumChildrenByType, useMaximumChildrenMatching, useMinimumChildrenByType, useMinimumChildrenMatching, useRequiredChildByType, useRequiredChildMatching };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,38 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { ElementType, ReactNode, ReactElement } from 'react';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Inclusive minimum and maximum bounds used by the bounded validation hooks.
|
|
6
|
+
*/
|
|
7
|
+
type ChildrenCountBounds = {
|
|
8
|
+
/**
|
|
9
|
+
* The minimum number of matching direct child elements required.
|
|
10
|
+
*/
|
|
11
|
+
minimum: number;
|
|
12
|
+
/**
|
|
13
|
+
* The maximum number of matching direct child elements allowed.
|
|
14
|
+
*/
|
|
15
|
+
maximum: number;
|
|
16
|
+
};
|
|
4
17
|
/**
|
|
5
18
|
* Represents a React element whose props and element type are narrowed to the provided React element type.
|
|
6
19
|
*
|
|
7
20
|
* @typeParam T The React element type used to narrow the element's props and type.
|
|
8
21
|
*/
|
|
9
22
|
type ElementOfType<T extends React.ElementType> = React.ReactElement<React.ComponentProps<T>, T>;
|
|
23
|
+
/**
|
|
24
|
+
* Optional reporting metadata used by the validation hooks to derive thrown validation messages.
|
|
25
|
+
*/
|
|
26
|
+
type ValidationOptions = {
|
|
27
|
+
/**
|
|
28
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
29
|
+
*/
|
|
30
|
+
traceCode?: string;
|
|
31
|
+
/**
|
|
32
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
33
|
+
*/
|
|
34
|
+
childName?: string;
|
|
35
|
+
};
|
|
10
36
|
|
|
11
37
|
/**
|
|
12
38
|
* Returns the first direct child element whose React element type exactly matches the provided type.
|
|
@@ -24,8 +50,31 @@ declare function useChildByType<T extends ElementType>(children: ReactNode, type
|
|
|
24
50
|
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
25
51
|
* @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.
|
|
26
52
|
*/
|
|
27
|
-
declare function
|
|
28
|
-
declare function
|
|
53
|
+
declare function useChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T | null;
|
|
54
|
+
declare function useChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement | null;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when the count falls outside the inclusive bounds.
|
|
58
|
+
*
|
|
59
|
+
* @param children The React children value to inspect.
|
|
60
|
+
* @param type The element or component type to match against each direct child element.
|
|
61
|
+
* @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
|
|
62
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
63
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
64
|
+
*/
|
|
65
|
+
declare function useBoundedChildrenByType<T extends ElementType>(children: ReactNode, type: T, bounds: ChildrenCountBounds, options?: ValidationOptions): ElementOfType<T>[];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when the count falls outside the inclusive bounds.
|
|
69
|
+
*
|
|
70
|
+
* @param children The React children value to inspect.
|
|
71
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
72
|
+
* @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
|
|
73
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
74
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
75
|
+
*/
|
|
76
|
+
declare function useBoundedChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, bounds: ChildrenCountBounds, options?: ValidationOptions): T[];
|
|
77
|
+
declare function useBoundedChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, bounds: ChildrenCountBounds, options?: ValidationOptions): ReactElement[];
|
|
29
78
|
|
|
30
79
|
/**
|
|
31
80
|
* Returns the direct child elements whose React element type exactly matches the provided type.
|
|
@@ -43,8 +92,31 @@ declare function useChildrenByType<T extends ElementType>(children: ReactNode, t
|
|
|
43
92
|
* @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.
|
|
44
93
|
* @returns An array of direct child elements that satisfy the provided predicate.
|
|
45
94
|
*/
|
|
46
|
-
declare function
|
|
47
|
-
declare function
|
|
95
|
+
declare function useChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T[];
|
|
96
|
+
declare function useChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement[];
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when the exact count is not met.
|
|
100
|
+
*
|
|
101
|
+
* @param children The React children value to inspect.
|
|
102
|
+
* @param type The element or component type to match against each direct child element.
|
|
103
|
+
* @param exactCount The exact number of matching direct child elements required.
|
|
104
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
105
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
106
|
+
*/
|
|
107
|
+
declare function useExactChildrenByType<T extends ElementType>(children: ReactNode, type: T, exactCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.
|
|
111
|
+
*
|
|
112
|
+
* @param children The React children value to inspect.
|
|
113
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
114
|
+
* @param exactCount The exact number of matching direct child elements required.
|
|
115
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
116
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
117
|
+
*/
|
|
118
|
+
declare function useExactChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, exactCount: number, options?: ValidationOptions): T[];
|
|
119
|
+
declare function useExactChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, exactCount: number, options?: ValidationOptions): ReactElement[];
|
|
48
120
|
|
|
49
121
|
/**
|
|
50
122
|
* Determines whether any direct child element satisfies the provided predicate.
|
|
@@ -53,19 +125,43 @@ declare function useChildrenWhere(children: ReactNode, predicate: (element: Reac
|
|
|
53
125
|
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
54
126
|
* @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.
|
|
55
127
|
*/
|
|
56
|
-
declare function
|
|
57
|
-
declare function
|
|
128
|
+
declare function useHasChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): boolean;
|
|
129
|
+
declare function useHasChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean): boolean;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when more than the maximum count are found.
|
|
133
|
+
*
|
|
134
|
+
* @param children The React children value to inspect.
|
|
135
|
+
* @param type The element or component type to match against each direct child element.
|
|
136
|
+
* @param maximumCount The maximum number of matching direct child elements allowed.
|
|
137
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
138
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
139
|
+
*/
|
|
140
|
+
declare function useMaximumChildrenByType<T extends ElementType>(children: ReactNode, type: T, maximumCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.
|
|
144
|
+
*
|
|
145
|
+
* @param children The React children value to inspect.
|
|
146
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
147
|
+
* @param maximumCount The maximum number of matching direct child elements allowed.
|
|
148
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
149
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
150
|
+
*/
|
|
151
|
+
declare function useMaximumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, maximumCount: number, options?: ValidationOptions): T[];
|
|
152
|
+
declare function useMaximumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, maximumCount: number, options?: ValidationOptions): ReactElement[];
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the direct child elements whose React element type exactly matches the provided type, or throws when fewer than the minimum count are found.
|
|
156
|
+
*
|
|
157
|
+
* @param children The React children value to inspect.
|
|
158
|
+
* @param type The element or component type to match against each direct child element.
|
|
159
|
+
* @param minimumCount The minimum number of matching direct child elements required.
|
|
160
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
161
|
+
* @returns The direct child elements whose type matches the provided element type.
|
|
162
|
+
*/
|
|
163
|
+
declare function useMinimumChildrenByType<T extends ElementType>(children: ReactNode, type: T, minimumCount: number, options?: ValidationOptions): ElementOfType<T>[];
|
|
58
164
|
|
|
59
|
-
type UseMinimumChildrenWhereOptions = {
|
|
60
|
-
/**
|
|
61
|
-
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
62
|
-
*/
|
|
63
|
-
traceCode?: string;
|
|
64
|
-
/**
|
|
65
|
-
* An optional human-readable child name that is included in the thrown validation message.
|
|
66
|
-
*/
|
|
67
|
-
childName?: string;
|
|
68
|
-
};
|
|
69
165
|
/**
|
|
70
166
|
* Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.
|
|
71
167
|
*
|
|
@@ -75,19 +171,19 @@ type UseMinimumChildrenWhereOptions = {
|
|
|
75
171
|
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
76
172
|
* @returns The direct child elements that satisfy the provided predicate.
|
|
77
173
|
*/
|
|
78
|
-
declare function
|
|
79
|
-
declare function
|
|
174
|
+
declare function useMinimumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: ValidationOptions): T[];
|
|
175
|
+
declare function useMinimumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: ValidationOptions): ReactElement[];
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the first direct child element whose React element type exactly matches the provided type, or throws when no match is found.
|
|
179
|
+
*
|
|
180
|
+
* @param children The React children value to inspect.
|
|
181
|
+
* @param type The element or component type to match against each direct child element.
|
|
182
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
183
|
+
* @returns The first direct child element whose type matches the provided element type.
|
|
184
|
+
*/
|
|
185
|
+
declare function useRequiredChildByType<T extends ElementType>(children: ReactNode, type: T, options?: ValidationOptions): ElementOfType<T>;
|
|
80
186
|
|
|
81
|
-
type UseRequiredChildWhereOptions = {
|
|
82
|
-
/**
|
|
83
|
-
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
84
|
-
*/
|
|
85
|
-
traceCode?: string;
|
|
86
|
-
/**
|
|
87
|
-
* An optional human-readable child name that is included in the thrown validation message.
|
|
88
|
-
*/
|
|
89
|
-
childName?: string;
|
|
90
|
-
};
|
|
91
187
|
/**
|
|
92
188
|
* Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.
|
|
93
189
|
*
|
|
@@ -96,8 +192,8 @@ type UseRequiredChildWhereOptions = {
|
|
|
96
192
|
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
97
193
|
* @returns The first direct child element that satisfies the provided predicate.
|
|
98
194
|
*/
|
|
99
|
-
declare function
|
|
100
|
-
declare function
|
|
195
|
+
declare function useRequiredChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: ValidationOptions): T;
|
|
196
|
+
declare function useRequiredChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: ValidationOptions): ReactElement;
|
|
101
197
|
|
|
102
198
|
/**
|
|
103
199
|
* Normalizes a React children value into an array containing only valid direct child elements.
|
|
@@ -124,4 +220,4 @@ declare function isElementOfType<T extends ElementType>(element: ReactElement, t
|
|
|
124
220
|
*/
|
|
125
221
|
declare function isReactElement(node: ReactNode): node is ReactElement;
|
|
126
222
|
|
|
127
|
-
export { type
|
|
223
|
+
export { type ChildrenCountBounds, type ElementOfType, type ValidationOptions, childrenToElements, isElementOfType, isReactElement, useBoundedChildrenByType, useBoundedChildrenMatching, useChildByType, useChildMatching, useChildrenByType, useChildrenMatching, useExactChildrenByType, useExactChildrenMatching, useHasChildMatching, useMaximumChildrenByType, useMaximumChildrenMatching, useMinimumChildrenByType, useMinimumChildrenMatching, useRequiredChildByType, useRequiredChildMatching };
|
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/
|
|
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/
|
|
24
|
-
function
|
|
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,57 +30,136 @@ function useChildWhere(children, predicate) {
|
|
|
30
30
|
|
|
31
31
|
// src/useChildByType.ts
|
|
32
32
|
function useChildByType(children, type) {
|
|
33
|
-
return
|
|
33
|
+
return useChildMatching(
|
|
34
34
|
children,
|
|
35
35
|
(element) => isElementOfType(element, type)
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
// src/
|
|
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
|
|
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
|
|
91
|
+
return useChildrenMatching(
|
|
51
92
|
children,
|
|
52
93
|
(element) => isElementOfType(element, type)
|
|
53
94
|
);
|
|
54
95
|
}
|
|
55
96
|
|
|
56
|
-
// src/
|
|
97
|
+
// src/useExactChildrenMatching.ts
|
|
98
|
+
function useExactChildrenMatching(children, predicate, exactCount, options) {
|
|
99
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
100
|
+
if (matchingChildren.length === exactCount) {
|
|
101
|
+
return matchingChildren;
|
|
102
|
+
}
|
|
103
|
+
return reporter_default.fail("EXACT_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
104
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
105
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
106
|
+
actualCount: matchingChildren.length,
|
|
107
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
108
|
+
exactCount
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
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
|
|
57
123
|
import { useMemo as useMemo3 } from "react";
|
|
58
|
-
function
|
|
124
|
+
function useHasChildMatching(children, predicate) {
|
|
59
125
|
return useMemo3(
|
|
60
126
|
() => childrenToElements(children).some(predicate),
|
|
61
127
|
[children, predicate]
|
|
62
128
|
);
|
|
63
129
|
}
|
|
64
130
|
|
|
65
|
-
// src/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
131
|
+
// src/useMaximumChildrenMatching.ts
|
|
132
|
+
function useMaximumChildrenMatching(children, predicate, maximumCount, options) {
|
|
133
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
134
|
+
if (matchingChildren.length <= maximumCount) {
|
|
135
|
+
return matchingChildren;
|
|
136
|
+
}
|
|
137
|
+
return reporter_default.fail("MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
138
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
139
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
140
|
+
actualCount: matchingChildren.length,
|
|
141
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
142
|
+
maximumCount
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
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
|
+
}
|
|
76
155
|
|
|
77
|
-
// src/
|
|
78
|
-
function
|
|
79
|
-
const matchingChildren =
|
|
156
|
+
// src/useMinimumChildrenMatching.ts
|
|
157
|
+
function useMinimumChildrenMatching(children, predicate, minimumCount, options) {
|
|
158
|
+
const matchingChildren = useChildrenMatching(children, predicate);
|
|
80
159
|
if (matchingChildren.length >= minimumCount) {
|
|
81
160
|
return matchingChildren;
|
|
82
161
|
}
|
|
83
|
-
return reporter_default.fail("
|
|
162
|
+
return reporter_default.fail("MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
84
163
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
85
164
|
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
86
165
|
actualCount: matchingChildren.length,
|
|
@@ -89,27 +168,54 @@ function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
|
|
|
89
168
|
});
|
|
90
169
|
}
|
|
91
170
|
|
|
92
|
-
// src/
|
|
93
|
-
function
|
|
94
|
-
|
|
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);
|
|
95
184
|
if (child !== null) {
|
|
96
185
|
return child;
|
|
97
186
|
}
|
|
98
|
-
return reporter_default.fail("
|
|
187
|
+
return reporter_default.fail("REQUIRED_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
99
188
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
100
189
|
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
101
190
|
});
|
|
102
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
|
+
}
|
|
103
201
|
export {
|
|
104
202
|
childrenToElements,
|
|
105
203
|
isElementOfType,
|
|
106
204
|
isReactElement,
|
|
205
|
+
useBoundedChildrenByType,
|
|
206
|
+
useBoundedChildrenMatching,
|
|
107
207
|
useChildByType,
|
|
108
|
-
|
|
208
|
+
useChildMatching,
|
|
109
209
|
useChildrenByType,
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
210
|
+
useChildrenMatching,
|
|
211
|
+
useExactChildrenByType,
|
|
212
|
+
useExactChildrenMatching,
|
|
213
|
+
useHasChildMatching,
|
|
214
|
+
useMaximumChildrenByType,
|
|
215
|
+
useMaximumChildrenMatching,
|
|
216
|
+
useMinimumChildrenByType,
|
|
217
|
+
useMinimumChildrenMatching,
|
|
218
|
+
useRequiredChildByType,
|
|
219
|
+
useRequiredChildMatching
|
|
114
220
|
};
|
|
115
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/useHasChildWhere.ts","../src/reporter.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 { 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 { 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 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};\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 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,gBAAm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B,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,IAAU,CAAC,YAC/B,gBAAgB,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,SAAS,sBAAoD;AAE7D,IAAM,WA+CF;AAAA,EACA,0CACI;AAAA,EACJ,4CACI;AAAA,EACJ,4CACI;AAAA,EACJ,0CACI;AAAA,EACJ,4CACI;AACR;AAGA,IA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