random.js 424 B

123456789101112131415
  1. 'use strict';
  2. var values = require('../helpers/values');
  3. module.exports = function random() {
  4. var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  5. var items = values(this.items);
  6. var collection = new this.constructor(items).shuffle(); // If not a length was specified
  7. if (length !== parseInt(length, 10)) {
  8. return collection.first();
  9. }
  10. return collection.take(length);
  11. };