fn.js 718 B

1234567891011121314151617181920212223
  1. module.exports = {
  2. parseCount: ( i ) => Math.min( Math.max( parseInt( i, 10 ) || 1, 1 ), 500 ),
  3. randomId: () => Math.floor( Math.random() * 10000 ) + 1,
  4. randomUniqueIds: ( count ) => {
  5. const used = new Map()
  6. const ids = []
  7. for( let i = 0; i < count; i++ ) {
  8. let id = module.exports.randomId()
  9. if( used.has(id) ) {
  10. for( let j = 0; j < 10000 - 1; j++ ) {
  11. if( !used.has(id) ) break
  12. id++
  13. if( id > 10000 ) {
  14. id = 1
  15. }
  16. }
  17. }
  18. used.set(id, true)
  19. ids.push(id)
  20. }
  21. return ids
  22. },
  23. }