Skip to content

Instantly share code, notes, and snippets.

@Error601
Last active April 1, 2022 18:48
Show Gist options
  • Save Error601/b585cccfa7521bc10c3a to your computer and use it in GitHub Desktop.
Save Error601/b585cccfa7521bc10c3a to your computer and use it in GitHub Desktop.
Hipster filler text generator.
/*!
* Hipster filler text generator.
* Inspired by (and words taken from):
* http://hipsum.co/
*
* Usage:
*
* // sentences only
* APP.utils.hipster.sentence(); // generates 1 sentence with 9, 12, or 18 words
* APP.utils.hipster.sentences(3); // generates 3 sentences
* APP.utils.hipster.sentences(4, 12); // generates 4 sentences with 12 words each
*
* // paragraphs
* APP.utils.hipster.paragraph(); // generates 1 paragraph with 2, 3, or 5 sentences with 9, 12, or 18 words
* APP.utils.hipster.paragraphs(); // generates 1, 2, or 3 paragraphs with 2, 3, or 5 sentences with 9, 12, or 18 words
* APP.utils.hipster.paragraphs(3); // generates 3 paragraphs
* APP.utils.hipster.paragraphs(2, 4); // generates 2 paragraphs with 4 sentences each
* APP.utils.hipster.paragraphs(3, 5, 15); // generates 3 paragraphs with 5 sentences each, with 15 words per sentence
*/
var APP = (typeof APP == 'object') ? APP : {};
(function(APP){
var lingo, punc, utils, hipster;
lingo =
('trust fund|PBR&B|street art|mustache|roof party|health|goth|selfies|next level|hashtag|' +
'post-ironic|Vice|sriracha|banh mi|art party|paleo|flexitarian|tweet|street|art|scenester|' +
'whatever|chillwave|tousled|tofu|raw|denim|cred|cold-pressed|Echo Park|migas|listicle|' +
'crucifix|90s|Odd Future|farm-to-table|four loko|iPhone|actually|craft beer|beard oil|' +
'banjo|VHS|vinyl|8-bit|Thundercats|readymade|blog|locavore|pickled|skateboard|disrupt|' +
'salvia|millenial|bitters|cassette|stumptown|Tumblr|butcher|lomo|single-origin|coffee|' +
'Etsy|master cleanse|American Apparel|yr mixtape|messenger bag|jean shorts|synth|retro|' +
'narwhal|occupy|Austin|tote bag|pug|umami|viral|semiotics|Banksy|small batch|gluten-free|' +
'fingerstache|YOLO|ethical|DIY|typewriter|organic|mumblecore|fixie|vegan|four dollar toast|' +
'keytar|meggings|cray-cray|polaroid|truffaut|flannel|hella|gastropub|food truck').split('|');
punc = ('. . . ? . . . ! . . . ? . . .').split(' '); // weight periods?
APP.utils = utils =
getObject(APP.utils);
APP.utils.hipster = hipster =
getObject(APP.utils.hipster);
// generate sentences (text only)
hipster.sentences = function(sentences, words){
var output = [], temp = [], i = -1, ii = -1;
sentences = sentences || 1;
words = words || randomFromArray([9, 12, 18]);
while (++i < sentences) {
temp = [];
ii = -1;
while (++ii < words) {
temp.push(randomFromArray(lingo));
}
output.push(sentenceCase(temp.join(' ')) + randomFromArray(punc) + ' ');
}
return output.join(' ').replace(/\s+$/, '');
};
// generate ONE sentence (text only),
// optionally specifying word count
hipster.sentence = function(words){
return hipster.sentences(1, words);
};
// generate string appropriate for use
// in an HTML element id attribute:
// starts with letter and has only
// letters, numbers, and hyphens
// optionally appending the time in ms
hipster.id = function(words, prefix, time){
var id = hipster.sentences(1, words || 2);
if (time) {
id += ('-' + Date.now());
}
if (prefix) {
id = prefix + '-' + id;
}
return id.
replace(/^[^A-Za-z]/, 'x-$&'). // starts with other than a letter
replace(/[^A-Za-z0-9]/g, '-'). // remove non-alphanumeric
replace(/\-+/g, '-'). // remove any multiple hyphens that were created
slice(0, -1).toLowerCase(); // remove punctuation and lowercaserize
};
// generate paragraphs (HTML with <p> tags)
hipster.paragraphs = function(paragraphs, sentences, words){
var output = '', i = -1;
paragraphs = paragraphs || randomFromArray([1, 2, 3]);
while (++i < paragraphs) {
sentences = sentences || randomFromArray([2, 3, 5]);
output += ('<p class="hipster">' + hipster.sentences(sentences, words) + '</p>');
}
return output;
};
// generate ONE paragraph (with <p></p>),
// optionally specifying sentence and word count
hipster.paragraph = function(sentences, words){
return hipster.paragraphs(1, sentences, words);
};
function randomFromArray(arr){
return arr[Math.floor(Math.random() * (arr.length))]
}
function sentenceCase(string){
return string.charAt(0).toUpperCase() + string.slice(1);
}
function isPlainObject(obj){
return Object.prototype.toString.call(obj) === '[object Object]';
}
function getObject(obj){
return isPlainObject(obj) ? obj : {};
}
})(APP);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment