Created
September 30, 2010 20:43
-
-
Save dvv/605284 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
http://develobert.blogspot.com/2007/11/automated-lorem-ipsum-generator.html | |
*/ | |
LoremIpsum = { | |
fullstop: '.', /* Character(s) to add to the end of sentences */ | |
min_wps: 10, /* Minimum words per sentence */ | |
max_wps: 25, /* Maximum words per sentence */ | |
min_spp: 2, /* Minimum sentences per paragraph */ | |
max_spp: 8, /* Maximum sentences per paragraph */ | |
/* Word bank | |
Words are selected from this array randomly | |
to generate complete text nodes. | |
NOTE: This array __can__ be altered provided it remains a single-dimension array. | |
*/ | |
src: new Array('a','adipiscing','amet','arcu','at','auctor','bibendum','commodo','congue','curabitur','cursus','diam','donec','duis','eget','elementum','enim','eros','et','eu','fusce','gravida','in','integer','ipsum','justo','lectus','leo','ligula','lorem','maecenas','magna','malesuada','massa','mattis','mauris','metus','molestie','morbi','nam','nec','nibh','non','nulla','odio','orci','ornare','pellentesque','pharetra','porta','porttitor','proin','quam','quisque','risus','rutrum','sagittis','sapien','sed','sem','sit','sodales','tellus','tempus','ultricies','urna','ut','vitae','vivamus','vulputate'), | |
/* words, getWords, get_words | |
@param words: Number of words to return | |
@param fullstop: String to append to the words, such as sentence ending punctuation. | |
@returns String: String of whitespace delimited words. | |
*/ | |
words: function(words, fullstop){ | |
var lastword, nextword, str, wc = (LoremIpsum.src.length - 1); | |
lastword = nextword = Math.round(Math.random() * wc); | |
str = LoremIpsum.src[lastword].charAt(0).toUpperCase() + LoremIpsum.src[lastword].substr(1); | |
words--; | |
while(words > 0){ | |
while(lastword == nextword){ | |
nextword = Math.round(Math.random() * wc); | |
} | |
str += (' ' + LoremIpsum.src[nextword]); | |
lastword = nextword; | |
words--; | |
} | |
return str + (fullstop ? fullstop : ''); | |
}, | |
/* sentences, getSentences, get_sentences | |
@param sentences: Number of sentences to return. | |
@returns String: String of sentences delimited by current value of LoremIpsum.fullstop | |
*/ | |
sentences: function(sentences){ | |
var wpsos = LoremIpsum.max_wps - LoremIpsum.min_wps; | |
var str = LoremIpsum.words((LoremIpsum.min_wps + Math.round(Math.random() * wpsos)), LoremIpsum.fullstop); | |
sentences--; | |
while(sentences > 0){ | |
str += (' ' + LoremIpsum.words((LoremIpsum.min_wps + Math.round(Math.random() * wpsos)), LoremIpsum.fullstop)); | |
sentences--; | |
} | |
return str; | |
}, | |
/* paragraphs, getParagraphs, get_paragraphs | |
@param paragraphs: Number of paragraphs to return. | |
@param format: How to format each paragraph, where %s is the text. | |
For instance: <p class="myclass">%s</p> | |
@returns String: String of paragraphs formated using @format. | |
*/ | |
paragraphs: function(paragraphs, format){ | |
if(!format){ | |
format = "%s\n\n"; | |
} | |
var sppos = LoremIpsum.max_spp - LoremIpsum.min_spp; | |
var str = format.replace(/%s/i, LoremIpsum.sentences(LoremIpsum.min_spp + Math.round(Math.random() * sppos))); | |
paragraphs--; | |
while(paragraphs > 0){ | |
str += format.replace(/%s/i, LoremIpsum.sentences(LoremIpsum.min_spp + Math.round(Math.random() * sppos))); | |
paragraphs--; | |
} | |
return str; | |
} | |
}; | |
/** | |
* generic fixture. Vladimir Dronnikov <[email protected]>, 2010 | |
* require('fixture').fixture(model.Obj, 100) | |
*/ | |
function dir(){var sys=require('sys');for(var i=0,l=arguments.length;i<l;i++)sys.debug(sys.inspect(arguments[i]));} | |
var fixture = exports.fixture = function(cls, count, subcount) { | |
function lorem(prop) { | |
function randint(a, b) { | |
return Math.floor(Math.random() * (b - a) + a); | |
} | |
function randint0(n) { | |
return randint(0, n); | |
} | |
function randintoptional(n) { | |
return randint(prop.optional !== true ? 1 : 0, n); | |
} | |
function choice(arr) { | |
return arr[randint0(arr.length)]; | |
} | |
var v = undefined; | |
if (prop['enum'] && prop['enum'].length) { | |
v = choice(prop['enum']); | |
} else { | |
var type = prop.type || 'any'; | |
if (type === 'any') { | |
return; | |
//type = choice(['string', 'number', 'integer', 'boolean', 'array', 'object']); | |
} | |
switch (type) { | |
case 'string': | |
switch (prop.format || 'none') { | |
case 'date-time': | |
case 'date': | |
case 'time': | |
var m = randint(1, 12); | |
var d = 30 + (m in [1, 3, 5, 7, 8, 10, 12]); | |
if (m == 2) d = 28; // TODO: leaps? | |
v = new Date(randint(1970, 2030), m, randint(1, d), randint0(24), randint0(60), randint0(60)); | |
v = v.toISOString(); | |
break; | |
default: | |
// TODO: respect min/maxlength | |
v = LoremIpsum.sentences(randint(1, 1+4*!prop.maxlength)); | |
if (prop.maxlength) | |
v = v.substr(0, prop.maxlength); | |
} | |
break; | |
case 'number': | |
v = 10000 * Math.random(); | |
if (prop.maxDecimal !== undefined) { | |
var f = Math.pow(10, prop.maxDecimal); | |
v = Math.round(v * f) / f; | |
} | |
break; | |
case 'integer': | |
v = randint(prop.minimum || 0, prop.maximum || 2147483647); | |
break; | |
case 'boolean': | |
v = Math.random() >= 0.5; | |
break; | |
case 'date': | |
var m = randint(1, 12); | |
var d = 30 + (m in [1, 3, 5, 7, 8, 10, 12]); | |
if (m == 2) d = 28; // TODO: leaps? | |
v = new Date(randint(1970, 2030), m, randint(1, d), randint0(24), randint0(60), randint0(60)); | |
v = v.toISOString(); | |
break; | |
case 'array': | |
var t = prop.items; | |
if (typeof t.type === 'function') { | |
// direct javascript reference | |
v = fixture(t.type, randintoptional(5)); | |
} else if (t.type && t.type.$ref) { | |
// JSON reference: $ref: '/Class/<Entity>/' | |
// TODO: how to fetch Entity given "Entity" (string)? | |
} else { | |
// ordinal | |
v = []; | |
//console.log('FIXTURING:' + JSON.stringify(t)); | |
for (var i = randintoptional(5); --i >= 0;) | |
v.push(lorem(t)); | |
} | |
break; | |
case 'object': | |
v = {}; | |
for (var i in prop.properties) { | |
v[i] = lorem(prop.properties[i]); | |
} | |
break; | |
default: | |
// TODO: type is schema: self reference, e.g. | |
// TODO: possible endless recursion! | |
} | |
} | |
// try assign default value to non-optional property | |
// TODO: undefine 20% percent of optional fields? | |
if (prop.optional !== true && v === undefined) | |
v = prop['default']; | |
return v; | |
} | |
var result = []; | |
//dir('FIX:', arguments); | |
while (--count >= 0) { | |
var data = {}; | |
// N.B. mongodb has weird _id: ObjectId() property. To cope we must specify _id ourselves | |
//data._id = Math.random().toString().substring(2); | |
// assign properties | |
for (var key in cls.properties) { | |
var prop = cls.properties[key]; | |
var x = lorem(prop); | |
if (x !== undefined) { | |
//print("\n###\n" + key + ": [" + x + "]\n###\n"); | |
data[key] = x; | |
} | |
} | |
//var x = new cls(data); | |
//if (x.save) x.save(); | |
var x = cls.put(data, {insert: true}); | |
//dir(x); | |
result.push(x); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment