How to use random-item - 10 common examples

To help you get started, we’ve selected a few random-item examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github welovekpop / munar / packages / munar-plugin-greetings / src / index.js View on Github external
greet = (source, user) => {
    if (this.lastGreeted === user.id ||
        // guest users
        user.username === '') {
      return
    }

    this.lastGreeted = user.id

    const greeting = random(this.options.greetings)
    const message = greeting.replace(/@/g, `@${user.username}`) +
      ` ${random(this.options.emoji)}`
    setTimeout(() => {
      source.send(message)
    }, 2 * 1000)
  }
}
github welovekpop / munar / packages / munar-plugin-greetings / src / index.js View on Github external
greet = (source, user) => {
    if (this.lastGreeted === user.id ||
        // guest users
        user.username === '') {
      return
    }

    this.lastGreeted = user.id

    const greeting = random(this.options.greetings)
    const message = greeting.replace(/@/g, `@${user.username}`) +
      ` ${random(this.options.emoji)}`
    setTimeout(() => {
      source.send(message)
    }, 2 * 1000)
  }
}
github Yoctol / bottender-compose / src / __tests__ / random.spec.js View on Github external
xit('should pass extra args to underlying action', () => {
  const Haha = jest.fn();
  const Wow = jest.fn();

  const actions = [Haha, Wow];

  randomItem.mockReturnValueOnce(Haha);
  const action = random(actions);

  const context = {
    sendText: jest.fn(),
  };

  const extraArg = {};

  action(context, extraArg);

  expect(randomItem).toBeCalledWith(actions);
  expect(Haha).toBeCalledWith(context, extraArg);
});
github Yoctol / bottender-compose / src / __tests__ / random.spec.js View on Github external
it('should create action that will call sendText', async () => {
  const Haha = sendText('Haha');
  const Wow = sendText('Wow');
  const Cool = sendText('Cool');
  const actions = [Haha, Wow, Cool];

  randomItem.mockReturnValueOnce(Cool);
  const Random = random([Haha, Wow, Cool]);

  const context = {
    sendText: jest.fn(),
  };

  const Action = await Random(context);

  expect(randomItem).toBeCalledWith(actions);
  expect(Action).toEqual(Cool);
});
github welovekpop / munar / packages / munar-plugin-waitlist-raffle / src / index.js View on Github external
async onEnd () {
    this.running = false
    clearTimeout(this.timer)
    this.timer = null
    const players = await this.getPlayers()
    if (this.players.length === 0) {
      this.source.send('Nobody participated in the raffle... Do I get to win now?')
    } else if (players.length === 0) {
      this.source.send('Nobody is eligible to win the raffle... Too bad!')
    } else {
      const winner = random(this.players)
      debug('winner', winner.toString())
      this.source.send(
        this.options.winMessage.replace(/\$winner/g, winner.username)
      )
      const waitlist = this.source.getWaitlist()
      await waitlist.move(winner, this.options.winnerPosition - 1)
    }
    this.players = []
    this.source = null
  }
}
github welovekpop / munar / src / modules / UserKarma.module.js View on Github external
async fistbump (message) {
    const reasonList = await Karma.find({ target: message.user.id, reason: { $ne: null } })
      .where('amount').gt(0)
      .populate('giver')

    if (reasonList.length === 0) {
      message.reply('no one can explain your mysterious allure.')
    }
    let chosen = random(reasonList)
    message.reply(`you were bumped by @${chosen.giver.username} ` +
                  `${chosen.reason}" ${moment(chosen.date).fromNow()}`)
  }
github welovekpop / munar / src / modules / Roulette.module.js View on Github external
onEnd () {
    this._running = false
    this._timer = null
    let roulette = this._roulette
    let players = this.players()
    roulette.entrants = this._players.map((p) => p.id)
    if (this._players.length === 0) {
      this.source.send('Nobody participated in the roulette... Do I get to win now?')
    } else if (players.length === 0) {
      this.source.send('Nobody is eligible to win the roulette... Too bad!')
    } else {
      let winner = random(players)
      debug('winner', winner.username)
      this.source.send(`Roulette winner: @${winner.username}. Congratulations! https://i.imgur.com/TXKz7mt.gif`)
      this.bot.moveDJ(winner.id, this.options.winnerPosition - 1, () => {
        debug('winner moved')
      })

      roulette.winner = winner.id
    }
    RouletteHistory.create(roulette)
    this._players = []
  }
github welovekpop / munar / packages / munar-plugin-karma / src / index.js View on Github external
async fistbump (message) {
    const User = this.model('User')
    const Karma = this.model('Karma')

    const user = await User.from(message.user)
    const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
      .where('amount').gt(0)
      .populate('giver')

    if (reasonList.length > 0) {
      let chosen = random(reasonList)
      message.reply(`you were bumped by @${chosen.giver.username} ` +
                    `"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
    } else {
      message.reply('no one can explain your mysterious allure.')
    }
  }
github welovekpop / munar / packages / munar-plugin-karma / src / index.js View on Github external
async fistthump (message) {
    const User = this.model('User')
    const Karma = this.model('Karma')

    const user = await User.from(message.user)
    const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
      .where('amount').lt(0)
      .populate('giver')
    if (reasonList.length > 0) {
      let chosen = random(reasonList)
      message.reply(`you were thumped by @${chosen.giver.username} ` +
                    `"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
    } else {
      message.reply('no reason was ever given.')
    }
  }
}
github welovekpop / munar / src / modules / TriviaCore.js View on Github external
nextQuestion () {
    if (this.isRunning()) {
      let question
      do {
        question = random(this.questions)
      } while (this._history.indexOf(question) !== -1)

      return this.askQuestion(question)
    }
    return Promise.reject(new Error('Trivia is not running'))
  }

random-item

Get a random item from an array

MIT
Latest version published 4 years ago

Package Health Score

53 / 100
Full package analysis

Popular random-item functions