Last active
September 4, 2015 06:41
-
-
Save maryleloisa/dcb43841e624da0b61e8 to your computer and use it in GitHub Desktop.
Underscore.js Demo - Run Fiddle at http://jsfiddle.net/binarychef/BL3rj/4/
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
<html> | |
<head> | |
<title></title> | |
<style type="text/css"> | |
#output { | |
font-family: Courier; | |
font-size: 12px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="output"></div> | |
<script src="http://underscorejs.org/underscore-min.js"></script> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
<script src="lib.js"></script> | |
</body> | |
</html> |
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
function logOutput(msg) { | |
var html = $("#output").html(); | |
$("#output").html(html + "<br/>" + msg); | |
} | |
// return a random integer, no less than lower and no greater than upper | |
function randomInt(lower, upper) { | |
var range = upper - lower; | |
return Math.round(Math.random() * range + lower); | |
} | |
// generate an array of random integers of length n, no less that lb and no greater than ub | |
function randomRange(n, lb, ub) { | |
var data = _.range(n); | |
// replace each element in data with a random integer | |
return _.map(data, function(e, i, l) { return randomInt(lb, ub); }); | |
} | |
// return a string representation of an array, split across multiple lines | |
function dumpArray(arr, it_len) { | |
// by default split the array across 4 lines | |
if (it_len == null) | |
it_len = 4 | |
if (it_len == 0) | |
it_len = 1; | |
var out = '[' + '<br/>'; | |
// get one line of elements, join them with a comma, and add then line to the output | |
for (var i = 0; i < arr.length / it_len; i++) { | |
out += ' ' + arr.slice(i * it_len, i * it_len + it_len).join(',') + ',' + '<br/>'; | |
} | |
// close off and return the output | |
return out + ']'; | |
} | |
// return a string representation of an integer padded on the left with zeros | |
function padIntLeft(i, maxDigits) { | |
// default to a total length of 3 | |
if (maxDigits == null) | |
maxDigits = 3; | |
// if we need to pad | |
if (i.toString().length < maxDigits) { | |
// create a string of '0' of the needed length and add it to the front of the int | |
i = Array(maxDigits + 1 - i.toString().length).join('0') + i.toString(); | |
} | |
return i; | |
} | |
// return a string representation of an integer padded on the right with spaces | |
function padIntRight(i, maxSpace) { | |
// default to a total length of 3 | |
if (maxSpace == null) | |
maxSpace = 3; | |
// if we need to pad | |
if (i.toString().length < maxSpace) { | |
// create a string of ' ' of the needed length and add it to the end of the int | |
i = i.toString() + Array(maxSpace + 1 - i.toString().length).join(' '); | |
} | |
return i; | |
} | |
// return only the random numbers which are perfect squares | |
var filtered = _.filter( | |
randomRange(100, 0, 1000), | |
function (e) { | |
// if a number is a perfect square there will be no decimal to truncate | |
var z = Math.sqrt(e) - Math.floor(Math.sqrt(e)); | |
return z == 0; | |
} | |
); | |
// generate random strings of between 2 and 5 words | |
function TextGenerator() { | |
// source words .. one word from each array to be randomly selected | |
this.firstWords = ['One', 'The', 'My', 'Your', 'Our']; | |
this.secondWords = ['red', 'green', 'blue', 'yellow', 'clear']; | |
this.thirdWords = ['dirty', 'clean', 'smooth', 'shiny', 'old']; | |
this.fourthWords = ['small', 'medium', 'large', 'tiny', 'huge']; | |
this.fifthWords = ['sphere', 'cube', 'cone', 'line', 'blob']; | |
// the 1st and 5th will always be used, the others are optional | |
this.words = [this.secondWords, this.thirdWords, this.fourthWords]; | |
// generate a random phrases of length n [2 .. 5] | |
this.randomString = function(len) { | |
// keep underscore happy | |
var copyWords = this.words; | |
// if len is out of bounds, default to 5 | |
if (len < 2 || len > 5 || len == null) | |
len = 5; | |
// the number of middle words we need to get | |
len -= 2; | |
// a list of indicies into the source word arrays | |
var indicies = randomRange(5, 0, 4); | |
// to hold the random words selected | |
var phrase = []; | |
// select the first word | |
phrase.push(this.firstWords[indicies[0]]); | |
_.each( | |
// get the indices for the middle words | |
indicies.slice(1, 1 + len), | |
// e: element, i: index, l: list | |
function (e, i, l) { | |
// random index for this middle word | |
var index = indicies[i+1]; | |
// get the word array | |
var localWords = copyWords[i] | |
// get the word | |
var randomWord = localWords[index]; | |
// add to the phrase array | |
phrase.push(randomWord); | |
} | |
); | |
// add the last word | |
phrase.push(this.fifthWords[indicies[4]]); | |
// join the phrase | |
return phrase.join(' ') + '.'; | |
}; | |
}; | |
var generator = new TextGenerator(); | |
//logOutput(generator.randomString(5)); | |
//logOutput(dumpArray(randomRange(17, 0, 10), 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment