Skip to content

Commit

Permalink
cleaned up helpers, added new method for doing symbol replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed May 15, 2010
1 parent 8e5f08a commit 7953e25
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
34 changes: 17 additions & 17 deletions helper.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
(function (Helper) {

Helper.randomNumber = function(number) {
r = Math.floor(Math.random()*number);
// returns a single random number based on a range
Helper.randomNumber = function(range) {
r = Math.floor(Math.random()*range);
return r;
};


// takes an array and returns the array randomly sorted
Helper.randomize = function(array) {
r = Math.floor(Math.random()*array.length);
return array[r];
};

Helper.numerify = function(number_string){
// parses string for a symbol and replace it with a random number from 1-10
Helper.replaceSymbolWithNumber = function(string, symbol){

// default symbol is '#'
if(typeof symbol == 'undefined'){
var symbol = '#';
}

var str = '';
for(var i = 0; i < number_string.length; i++){
if(number_string[i] == "#"){
for(var i = 0; i < string.length; i++){
if(string[i] == symbol){
str += Math.floor(Math.random()*10);
}
else{
str += number_string[i];
str += string[i];
}
}
return str;
};

// takes an array and returns it randomized
Helper.shuffle = function(o){
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};

Helper.letterify = function(letter_string){
//return letter_string.gsub(/\?/) { ('a'..'z').to_a.rand }
return 'zzz';
};
Helper.bothify = function(string){
// self.letterify(self.numerify(string))
//letterify
return 'zz11zz';
};

})(
// exports will be set in any commonjs platform; use it if it's available
typeof exports !== "undefined" ?
Expand Down
11 changes: 5 additions & 6 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Helper = require('../helper');
var definitions = require('../lib/definitions');

exports.zip_code = function() {
return Helper.numerify(Helper.randomize(["#####", '#####-####']));
return Helper.replaceSymbolWithNumber(Helper.randomize(["#####", '#####-####']));
};

exports.city = function() {
Expand Down Expand Up @@ -41,22 +41,21 @@ exports.streetAddress = function(i) {
switch(Helper.randomNumber(2))
{
case 0:
address = Helper.numerify("#####") + " " + this.streetName();
address = Helper.replaceSymbolWithNumber("#####") + " " + this.streetName();
break;
case 1:
address = Helper.numerify("####") + " " + this.streetName();
address = Helper.replaceSymbolWithNumber("####") + " " + this.streetName();
break;
case 2:
address = Helper.numerify("###") + " " + this.streetName();
address = Helper.replaceSymbolWithNumber("###") + " " + this.streetName();
break;
}
var full_address = i ? address + " " + this.secondaryAddress() : address;
return full_address;
};


exports.secondaryAddress = function() {
return Helper.numerify(Helper.randomize(
return Helper.replaceSymbolWithNumber(Helper.randomize(
[
'Apt. ###',
'Suite ###'
Expand Down
2 changes: 1 addition & 1 deletion lib/internet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ exports.email = function() {
};

exports.userName = function() {

};
2 changes: 1 addition & 1 deletion lib/phone_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var definitions = require('./definitions');

exports.phoneNumber = function(){

return Helper.numerify(Helper.randomize(definitions.phone_formats));
return Helper.replaceSymbolWithNumber(Helper.randomize(definitions.phone_formats));

};

0 comments on commit 7953e25

Please sign in to comment.