Skip to content

Commit

Permalink
refactor: indexer conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 8, 2024
1 parent d57705d commit d30d74f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,9 @@ const convertModel = (
}

if (sourceModels.size === 0) {
if (!model.indexer) {
return convertRecord(program, model, constraints)
}

const modelWithIndexer = model as Model & { indexer: ModelIndexer }
return model.indexer.key.name === `integer`
? convertArray(program, modelWithIndexer, constraints)
: convertDictionary(program, modelWithIndexer, constraints)
return model.indexer
? convertModelIndexer(program, model.indexer, constraints)
: convertRecord(program, model, constraints)
}

const arbitraries = pipe(
Expand All @@ -323,9 +318,20 @@ const convertModel = (
return memoize({ type: `merged`, arbitraries })
}

const convertModelIndexer = (
program: Program,
indexer: ModelIndexer,
constraints: Constraints,
): Arbitrary =>
(indexer.key.name === `integer` ? convertArray : convertDictionary)(
program,
indexer,
constraints,
)

const convertArray = (
program: Program,
model: Model & { indexer: ModelIndexer },
indexer: ModelIndexer,
constraints: Constraints,
): ArrayArbitrary => {
let minItems = constraints.minItems?.asNumber() ?? undefined
Expand All @@ -335,21 +341,21 @@ const convertArray = (

return memoize({
type: `array`,
value: convertType(program, model.indexer.value, constraints),
value: convertType(program, indexer.value, constraints),
minItems,
maxItems: constraints.maxItems?.asNumber() ?? undefined,
})
}

const convertDictionary = (
program: Program,
model: Model & { indexer: ModelIndexer },
indexer: ModelIndexer,
constraints: Constraints,
): DictionaryArbitrary =>
memoize({
type: `dictionary`,
key: convertType(program, model.indexer.key, constraints),
value: convertType(program, model.indexer.value, constraints),
key: convertType(program, indexer.key, constraints),
value: convertType(program, indexer.value, constraints),
})

const convertRecord = (
Expand Down

0 comments on commit d30d74f

Please sign in to comment.