Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created October 12, 2015 12:36
Show Gist options
  • Save lamchau/7aa035c390aa068f7a1e to your computer and use it in GitHub Desktop.
Save lamchau/7aa035c390aa068f7a1e to your computer and use it in GitHub Desktop.
(function(Factory, Lorem, underscore) {
var SHA_CHARS = "0123456789abcdef";
var ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var CATEGORIES = ["Low", "Medium", "High"];
var ONE_YEAR_IN_MILLISECONDS = 31536000000;
var _ = underscore;
var methods;
// http://www.ssa.gov/oact/babynames/decades/century.html
var FIRST_NAME = ["James", "John", "Robert", "Michael", "William", "David", "Richard", "Joseph", "Charles", "Thomas", "Christopher", "Daniel", "Matthew", "Donald", "Anthony", "Mark", "Paul", "Steven", "George", "Kenneth", "Mary", "Patricia", "Jennifer", "Elizabeth", "Linda", "Barbara", "Susan", "Margaret", "Jessica", "Sarah", "Dorothy", "Karen", "Nancy", "Betty", "Lisa", "Sandra", "Ashley", "Kimberly", "Donna", "Helen"];
// https://en.wikipedia.org/wiki/List_of_most_common_surnames_in_North_America
var LAST_NAME = ["Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson", "Martinez", "Anderson", "Taylor", "Thomas", "Hernandez", "Moore", "Martin", "Jackson", "Thompson", "White", "Lopez", "Lee", "Gonzalez", "Harris", "Clark", "Lewis", "Robinson", "Walker", "Perez", "Hall", "Young", "Allen", "Sanchez", "Wright", "King", "Scott", "Green", "Baker", "Adams", "Nelson"];
methods = {
nextBoolean: function() {
return Boolean(_.random(0, 1));
},
nextCategory: function() {
return _.sample(CATEGORIES);
},
nextDate: function(startDate, endDate) {
switch (arguments.length) {
case 0:
endDate = _.now();
startDate = endDate - ONE_YEAR_IN_MILLISECONDS;
return methods.nextDate(startDate, endDate);
case 1:
return methods.nextDate(0, startDate);
}
startDate = +startDate;
endDate = +endDate;
if (_.isInteger(startDate) && _.isInteger(endDate)) {
return new Date(_.random(startDate, endDate)).toISOString();
}
return methods.nextDate();
},
nextDomain: function() {
var tld = _.sample(["com", "net", "org", "co.uk", "nyc"]);
var domain = methods.nextLorem(_.random(1, 3), Lorem.TYPE.WORD).replace(/\s/g, "-");
return (domain + "." + tld).toLowerCase();
},
nextEmail: function() {
var name = methods.nextFullName().replace(/\s/g, ".");
return (name + "@" + methods.nextDomain()).toLowerCase();
},
nextFullName: function() {
return _.compact([
_.sample(FIRST_NAME),
_.sample(ALPHABET + " "),
_.sample(LAST_NAME)
]).join(" ");
},
nextLorem: function(count, type) {
switch (arguments.length) {
case 0:
count = 1;
type = Lorem.TYPE.SENTENCE;
break;
case 1:
type = Lorem.TYPE.SENTENCE;
break;
}
return new Lorem().createText(count, type);
},
nextSha: function(length) {
length = Math.max(5, Math.min(length, 40)) || 5;
return _.sample(SHA_CHARS, length).join("");
},
nextSequenceId: function() {
return methods.nextSha(16);
},
/**
* Resets a sequence for a defined factory.
*
* @param {string} [factoryName] - the defined factory name, if omitted will
* reset all defined sequences
*/
resetSequence: function(factoryName) {
// jshint forin: false
switch (arguments.length) {
case 0:
for (var name in Factory.factories) {
Factory.factories[name].sequences = {};
}
return;
case 1:
if (_.has(Factory.factories, factoryName)) {
Factory.factories[factoryName].sequences = {};
return;
}
throw new Error("Factory '" + factoryName + "' does not exist");
}
},
/**
* Allows overriding and restoring a custom sequence.
*
* Note: If the value does not match the original `Factory.define`, it'll be
* automatically be replaced by rosie.
*
* @param {string} factoryName - the defined factory name
* @param {object} [sequence] - the object with sequences to replace
* @param {boolean} [extendExisting] - if `true` extends stored sequence,
* otherwise replaces entire sequence object (same as calling
* resetSequence(..))
*/
setFactorySequence: function(factoryName, sequence, extendExisting) {
sequence = _.isObject(sequence) ? sequence : {};
// rosie persists sequences, this is a mechanism to temporarily reset them
if (_.has(Factory.factories, factoryName)) {
var factory = Factory.factories[factoryName],
oldSequence = factory.sequences;
// only override the sequences we want
factory.sequences = extendExisting ?
_.extend({}, oldSequence, sequence) : sequence;
return {
restore: function() {
factory.sequences = oldSequence;
}
};
}
throw new Error("Factory '" + factoryName + "' does not exist");
}
};
Object.keys(methods)
.forEach(function(functionName) {
if (Factory.hasOwnProperty(functionName)) {
// rosie is quite minimal, this should never occur
console.warn("Function name '" + functionName + "' already exists.");
} else {
Factory[functionName] = methods[functionName];
}
});
})(Factory, Lorem, _);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment