Skip to content

Instantly share code, notes, and snippets.

@vespoli
Created August 10, 2014 17:04
Show Gist options
  • Save vespoli/6a802878f5b799aec31b to your computer and use it in GitHub Desktop.
Save vespoli/6a802878f5b799aec31b to your computer and use it in GitHub Desktop.
Random content generator utility for jade templates. Accepts config options for min, max, type and punctuation
- var randomContent = function(config){
- var RandomContentConfig = function(config){ //- override defaults with passed in config
- var config = config || {};
- this.type = config.type || 'text';
- this.min = config.min || 10;
- this.max = config.max || 100;
- this.punctuation = typeof(config.punctuation) !== 'undefined' ? config.punctuation : true;
- };
- var puncChance = .0001;
- var c = new RandomContentConfig(config); //- config object
- if(c.type === 'text'){
- var lipsum = ['lorem','ipsum','dolor','sit','amet','consectetur','adipisicing','elit','sed','do','eiusmod','tempor','incididunt','ut','labore','et','dolore','magna','aliqua','enim','ad','minim','veniam','quis','nostrud','exercitation','ullamco','laboris','nisi','ut','aliquip','ex','ea','commodo','consequat','duis','aute','irure','dolor','in','reprehenderit','in','voluptate','velit','esse','cillum','dolore','eu','fugiat','nulla','pariatur','excepteur','sint','occaecat','cupidatat','non','proident','sunt','in','culpa','qui','officia','deserunt','mollit','anim','id','est','laborum','sed','ut','perspiciatis','unde','omnis','iste','natus','error','sit','voluptatem','accusantium','doloremque','laudantium','totam','rem','aperiam','eaque','ipsa','quae','ab','illo','inventore','veritatis','et','quasi','architecto','beatae','vitae','dicta','sunt','explicabo','nemo','enim','ipsam','voluptatem','quia','voluptas','sit','aspernatur','aut','odit','aut','fugit','sed','quia','consequuntur','magni','dolores','eos','qui','ratione','voluptatem','sequi','nesciunt','neque','porro','quisquam','est','qui','dolorem','ipsum','quia','dolor','sit','amet','consectetur','adipisci','velit','sed','quia','non','numquam','eius','modi','tempora','incidunt','ut','labore','et','dolore','magnam','aliquam','quaerat','voluptatem','ut','enim','ad','minima','veniam','quis','nostrum','exercitationem','ullam','corporis','suscipit','laboriosam','nisi','ut','aliquid','ex','ea','commodi','consequatur','quis','autem','vel','eum','iure','reprehenderit','qui','in','ea','voluptate','velit','esse','quam','nihil','molestiae','consequatur','vel','illum','qui','dolorem','eum','fugiat','quo','voluptas','nulla','pariatur','at','vero','eos','et','accusamus','et','iusto','odio','dignissimos','ducimus','qui','blanditiis','praesentium','voluptatum','deleniti','atque','corrupti','quos','dolores','et','quas','molestias','excepturi','sint','obcaecati','cupiditate','non','provident','similique','sunt','in','culpa','qui','officia','deserunt','mollitia','animi','id','est','laborum','et','dolorum','fuga','harum','quidem','rerum','facilis','est','et','expedita','distinctio','Nam','libero','tempore','cum','soluta','nobis','est','eligendi','optio','cumque','nihil','impedit','quo','minus','id','quod','maxime','placeat','facere','possimus','omnis','voluptas','assumenda','est','omnis','dolor','repellendus','temporibus','autem','quibusdam','aut','officiis','debitis','aut','rerum','necessitatibus','saepe','eveniet','ut','et','voluptates','repudiandae','sint','molestiae','non','recusandae','itaque','earum','rerum','hic','tenetur','a','sapiente','delectus','aut','reiciendis','voluptatibus','maiores','alias','consequatur','aut','perferendis','doloribus','asperiores','repellat'];
- var randomInt = Math.floor(Math.random()*(c.max - c.min)) + c.min;
- var r = ''; //- return value
- for(var i = 0; i < randomInt; i++) {
- var txt = lipsum[Math.floor(Math.random() * (lipsum.length - 1))];
- if(i === 0){
- txt = txt.substring(0,1).toUpperCase() + txt.substring(1, txt.length); //capitalize first word
- } else if((Math.random() < puncChance) && c.punctuation){ //starting a new sentence TODO: add possibility for question mark or comma
- txt = '. ' + txt.substring(0,1).toUpperCase() + txt.substring(1, txt.length);
- puncChance = .0001; //- reset chance for puncturation
- } else { //- increased chance we start a new sentence with each word
- puncChance += .05;
- txt = ' ' + txt;
- }
- r += txt;
- }
- r = c.punctuation ? r + '.' : r;
- return r;
- } //-text
- if(c.type === 'hex'){
- //- All my hexs live in Texas
- var hexs = ['aaaaaa','bbbbbb','cccccc','dddddd','eeeeee','111111','222222','333333','444444','555555','666666','777777','888888','999999']
- return hexs[Math.floor(Math.random() * (hexs.length - 1))];
- }
- if(c.type === 'firstName'){
- var firstNames = ['Frank','Jenny']
- return firstNames[Math.floor(Math.random() * (firstNames.length - 1))];
- }
- if(c.type === 'lastName'){
- var lastNames = ['Jones','Smith']
- return lastNames[Math.floor(Math.random() * (lastNames.length - 1))];
- }
- }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment