Skip to content

Commit

Permalink
fix: calling generate with count=0 should not generate anything (#38)
Browse files Browse the repository at this point in the history
* failing test for generate count=0

* fix generate count=0
  • Loading branch information
gmaclennan authored Jan 23, 2025
1 parent 7e5c940 commit a41f29c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ function getFakerSchema(schemaName) {
* @param {{version?: string, count?: number}} [options]
* @returns {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>}
*/
export function generate(schemaName, { count } = {}) {
export function generate(schemaName, { count = 0 } = {}) {
isValidSchemaName(schemaName)

const schema = getFakerSchema(schemaName)

const numberToGenerate = count || 1

/** @type {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>} */
const result = []

for (let i = 0; i < numberToGenerate; i++) {
for (let i = 0; i < count; i++) {
result.push(
/** @type {Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>} */ (
JSONSchemaFaker.generate(schema)
Expand Down
8 changes: 8 additions & 0 deletions test/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ test('generates valid data', { concurrency: true }, async (t) => {
}
})
})

test('passing count=0 returns an empty array', () => {
const schemaNames = Object.keys(listSchemas())

for (const schemaName of schemaNames) {
assert.deepEqual(generate(schemaName, { count: 0 }), [])
}
})

0 comments on commit a41f29c

Please sign in to comment.