lamchau
2/7/2015 - 9:36 AM

Mixins for https://github.com/bkeepers/rosie

(function(Factory) {
  function isInteger(n) {
    return typeof n === 'number' &&
      isFinite(n) &&
      n > -9007199254740992 &&
      n < 9007199254740992 &&
      Math.floor(n) === n;
  }

  var SHA_CHARS = '0123456789abcdef';
  var CATEGORIES = ['low', 'medium', 'high'];
  var ONE_YEAR_IN_MILLISECONDS = 31536000000;

  // Factory mixin
  var methods = {
    generateList: function(size, generator) {
      return Array.apply(null, new Array(size))
        .map(generator);
    },

    nextBoolean: function() {
      return Boolean(methods.nextInt(0, 1));
    },

    nextDate: function(startDate, endDate) {
      switch (arguments.length) {
        case 0:
          endDate = Date.now();
          startDate = endDate - ONE_YEAR_IN_MILLISECONDS;
          return methods.nextDate(startDate, endDate);

        case 1:
          return methods.nextDate(0, startDate);
      }

      startDate = +startDate;
      endDate = +endDate;

      var date;
      if (isInteger(startDate) && isInteger(endDate)) {
        date = methods.nextInt(startDate, endDate);
        return new Date(date).toISOString();
      }
      return methods.nextDate();
    },

    nextInt: function(min, max) {
      if (max === null || typeof max === 'undefined') {
        max = min;
        min = 0;
      }
      min = +min || 0;
      max = +max || 0;
      return Math.round(Math.random() * (max - min) + min);
    },

    nextSha: function(length) {
      length = Math.max(5, Math.min(length, 40)) || 5;
      var result = '',
        index = 0;

      for (var i = 0; i < length; i++) {
        index = methods.nextInt(0, SHA_CHARS.length - 1);
        result += SHA_CHARS.charAt(index);
      }
      return result;
    },

    nextCategory: function() {
      return methods.sample(CATEGORIES);
    },

    nextSequenceId: function() {
      return methods.nextSha(16);
    },

    sample: function(array) {
      if (Array.isArray(array)) {
        return array[methods.nextInt(0, array.length - 1)];
      } else if (arguments.length > 0) {
        return methods.sample(Array.prototype.slice.call(arguments));
      }
      return null;
    },
  };

  Object.keys(methods)
    .forEach(function(functionName) {
      if (Factory.hasOwnProperty(functionName)) {
        console.warn('Function name \'' + functionName + '\' already exists.');
      } else {
        Factory[functionName] = methods[functionName];
      }
    });
})(Factory || (Factory = {}));

Factory.define('dog')
  .attr('id', Factory.nextSequenceId)
  .attr('adoptable', Factory.nextBoolean)
  .attr('color', function() {
    return Factory.sample(['black', 'brown', 'mixed', 'white'])
  })
  .attr('energy_level', Factory.nextCategory)
  .attr('age', function() {
    return Factory.nextInt(1, 12);
  })
  .attr('gender', function() {
    return Factory.nextBoolean() ? 'male' : 'female';
  })
  .attr('hours_slept', function() {
    var days = 7;
    return Factory.generateList(days, function() {
      return Factory.nextInt(6, 8);
    });
  });

var dog = Factory.build('dog');
// {
//   "id": "b5cc86b77886",
//   "adoptable": false,
//   "color": "white",
//   "energy_level": "Medium",
//   "age": 7,
//   "gender": "male",
//   "hours_slept": [7, 7, 6, 7, 6, 7, 8]
// }