Skip to content

Instantly share code, notes, and snippets.

@jordanthomas
Last active December 16, 2015 20:10
Show Gist options
  • Save jordanthomas/5490647 to your computer and use it in GitHub Desktop.
Save jordanthomas/5490647 to your computer and use it in GitHub Desktop.
Random string helpers.
// Some helpers to generate random testing data; a seeder for javascript.
(function() {
'use strict';
///////////////////////////////////////////////////////////////////////////
// UTILITIES
// Methods for stuff like generating numbers and transforming text.
///////////////////////////////////////////////////////////////////////////
var utils = {
toTitleCase: function(str) {
return str.replace( /\w*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
});
},
between: function(l, h) {
return Math.floor((Math.random() * (h + 1 - l)) + l);
},
// Returns a number around (+/- 15%) the number specified
around: function(number) {
var spread = Math.ceil(number * 0.15);
return utils.between( number - spread, number + spread );
}
}
var random = {
letters: 'abcdefghijklmnopqrstuvwxyz'.split(''),
dictionary: 'ad adipisicing aliqua aliquip amet anim aute cillum commodo consectetur consequat culpa cupidatat deserunt do dolor dolore duis ea eiusmod elit enim esse est et eu ex excepteur exercitation fugiat id in incididunt ipsum irure labore laboris laborum lorem magna minim mollit nisi non nostrud nulla occaecat officia pariatur proident qui quis reprehenderit sed sint sit sunt tempor ullamco ut velit veniam voluptate'.split(' '),
punc: '.?!'.split(''),
///////////////////////////////////////////////////////////////////////////
// NUMBER
// Exposes the number generating util methods.
///////////////////////////////////////////////////////////////////////////
number: {
between: utils.between,
around: utils.around
},
///////////////////////////////////////////////////////////////////////////
// WORDS
///////////////////////////////////////////////////////////////////////////
// Returns a single word.
word: function(options) {
var options = options || {},
dict = options.dictionary || Random.dictionary;
return dict[utils.between( 0, dict.length - 1 )];
},
// Returns a string of words close to the specified amount.
words: function(amount) {
var words = [],
amount = utils.around( amount );
while ( words.length < amount ) {
words.push( Random.word() );
}
return words.join(' ');
},
///////////////////////////////////////////////////////////////////////////
// SENTENCES
///////////////////////////////////////////////////////////////////////////
// Returns a single sentence.
sentence: function() {
var sentence = [],
amount = utils.between( 5, 15 );
while ( sentence.length < amount ) {
sentence.push( Random.word() );
}
sentence = sentence.join(' ');
return sentence.charAt(0).toUpperCase() + sentence.slice(1) + Random.punc[utils.between(0, (Random.punc.length - 1))];
},
// Returns the specified amount of sentences, give or take a few.
sentences: function(amount) {
var sentences = [],
amount = utils.around( amount );
while ( sentences.length < amount ) {
sentences.push( Random.sentence() );
}
return sentences.join(' ');
},
///////////////////////////////////////////////////////////////////////////
// PARAGRAPHS
///////////////////////////////////////////////////////////////////////////
// Returns a single paragraph.
paragraph: function() {
var sentences = [];
length = utils.between(4, 6);
while ( sentences.length < length ) {
sentences.push( Random.sentence() );
}
return sentences.join(' ');
},
// Returns the specified amount of paragraphs, give or take a few. The
// output is quite similar to sentences() except each paragraph is
// separated by "\n\n".
paragraphs: function(amount) {
var paragraphs = [],
amount = utils.around( amount );
while ( paragraphs.length < amount ) {
paragraphs.push( Random.paragraph() );
}
return paragraphs.join("\n\n");
},
///////////////////////////////////////////////////////////////////////////
// TITLE
// Returns a few capitalized words.
///////////////////////////////////////////////////////////////////////////
title: function() {
var title = [];
length = utils.around( 4 );
while ( title.length < length ) {
title.push( Random.word() );
}
return utils.toTitleCase( title.join(' ') );
},
///////////////////////////////////////////////////////////////////////////
// IMAGE
// Returns a URL for an image from one of the placeholder services.
///////////////////////////////////////////////////////////////////////////
image: function(w, h) {
var w = w || 200,
h = h || 200,
services = [
'http://placekitten.com/{w}/{h}',
'http://lorempixel.com/{w}/{h}',
'http://placedog.com/{w}/{h}',
'http://placehold.it/{w}x{h}'
];
return services[utils.between( 0, (services.length - 1) )]
.replace('{w}', w)
.replace('{h}', h);
},
///////////////////////////////////////////////////////////////////////////
// COLOR
// Returns a random hex color value.
///////////////////////////////////////////////////////////////////////////
color: function() {
// Thanks Paul Irish!
return '#' + Math.floor( Math.random() * 16777215 ).toString(16);
},
///////////////////////////////////////////////////////////////////////////
// OBJECTS
// Returns an array of objects created from the provided prototype.
///////////////////////////////////////////////////////////////////////////
objects: function(amount, constructor) {
var objects = [],
amount = utils.around( amount );
while ( objects.length < amount ) {
objects.push( new constructor );
}
return objects;
}
}
// Exports
window.Random = random;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment