faker.helpers.unique调用datatype.number时不生效传入的options参数
问题目标
我希望使用@faker-js/faker的helpers.unique()函数生成一个指定最小值和最大值的唯一整数数组。根据官方文档,我编写了如下代码:
// import faker import { faker } from '@faker-js/faker' // extract the functions I plan to use (to make the next part more readable) const { helpers: { unique }, datatype: { number } } = faker // create array of 10 `undefined` elements and replace them with unique numbers const numbers = Array.from({ length: 10 }).map(() => unique(number, { min: 1, max: 10 })) // EXPECTED output console.log(numbers) // [5, 2, 4, 10, 8, 9, 1, 7, 3, 6] // ACTUAL output console.log(numbers) // [17530, 15198, 10760, 38070, 84874, 89011, 4254, 43244, 21142, 79435]
已尝试的操作
我进行了以下多种尝试来排查问题:
// actual output same as above const options = { max: 10, min: 1 } // save options in a var in same order as docs const numbers = Array.from({ length: 10 }).map(() => unique(number, options)) // try submitting options as separate params => ERROR (as expected) const numbers = Array.from({ length: 10 }).map(() => unique(number, 10, 1)) // calling OUTSIDE of the `map()` function unique(f.datatype.number, {min: 1, max: 10}) // yields e.g. 47858 // calling just `number()` to make sure it honors options if called normally number({ min: 1, max: 10 }) // correctly returns integers between 1 and 10
此外,我在@faker-js/faker仓库中搜索“unique”后发现,helpers.unique()函数预计将在v8.x版本中被弃用(截至2023-01-27)。
我将此问题留存于此,或许有人知道解决方案,也可能对仍在使用<=v7.6版本的开发者有所帮助。
内容的提问来源于stack exchange,提问作者morphatic
相关产品推荐
相关产品推荐

