MCPcopy Create free account
hub / github.com/arktypeio/arktype / spreadVariadicElements

Function spreadVariadicElements

ark/fast-check/arbitraries/array.ts:126–220  ·  view source on GitHub ↗
(
	elementArbitraries: fc.Arbitrary<unknown>[],
	node: nodeOfKind<"sequence">,
	refinements: fc.ArrayConstraints,
	ctx: Ctx
)

Source from the content-addressed store, hash-verified

124}
125
126export const spreadVariadicElements = (
127 elementArbitraries: fc.Arbitrary<unknown>[],
128 node: nodeOfKind<"sequence">,
129 refinements: fc.ArrayConstraints,
130 ctx: Ctx
131): fc.Arbitrary<unknown[]> => {
132 const tupleElements = node.tuple
133 const omittableCount = tupleElements.filter(isOmittable).length
134 const options: TupleLengthOption[] = []
135
136 for (
137 let omittablesIncluded = 0;
138 omittablesIncluded <= omittableCount;
139 omittablesIncluded++
140 ) {
141 // omitting an element omits every element after it, so those are never
142 // generated, e.g. a cyclic optional element
143 let end = 0
144 let omittablesSeen = 0
145 while (
146 end < tupleElements.length &&
147 !(
148 isOmittable(tupleElements[end]) &&
149 omittablesSeen++ === omittablesIncluded
150 )
151 )
152 end++
153
154 const included = tupleElements.slice(0, end)
155 const hasVariadic = included.some(el => el.kind === "variadic")
156 const fixedCount = hasVariadic ? included.length - 1 : included.length
157
158 if (!hasVariadic) {
159 if (
160 (refinements.minLength === undefined ||
161 fixedCount >= refinements.minLength) &&
162 (refinements.maxLength === undefined ||
163 fixedCount <= refinements.maxLength)
164 )
165 options.push({ end })
166 continue
167 }
168
169 const minLength = Math.max(0, (refinements.minLength ?? 0) - fixedCount)
170 const maxLength =
171 refinements.maxLength !== undefined ?
172 refinements.maxLength - fixedCount
173 : undefined
174 if (maxLength === undefined || minLength <= maxLength) {
175 options.push({
176 end,
177 variadicConstraints:
178 maxLength !== undefined ? { minLength, maxLength } : { minLength }
179 })
180 }
181 }
182
183 if (!options.length) {

Callers 1

sequenceArbitraryForFunction · 0.90

Calls 6

isOmittableFunction · 0.85
getPossiblyWeightedArrayFunction · 0.85
filterMethod · 0.80
arrayMethod · 0.80
mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected