Comparing version 0.2.0 to 0.2.1
337
chance.js
@@ -1,2 +0,2 @@ | ||
// Chance.js 0.1 | ||
// Chance.js 0.2.1 | ||
// http://chancejs.com | ||
@@ -13,3 +13,3 @@ // (c) 2013 Victor Quinn | ||
} | ||
this.mt = new MersenneTwister(seed); | ||
this.mt = this.mersenne_twister(seed); | ||
}; | ||
@@ -22,3 +22,4 @@ | ||
// Building Blocks/Basics | ||
// -- Basics -- | ||
Chance.prototype.bool = function () { | ||
@@ -66,2 +67,3 @@ return this.random() * 100 < 50; | ||
options = options || {}; | ||
var lower = "abcdefghijklmnopqrstuvwxyz", | ||
@@ -73,4 +75,3 @@ upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
// Refactor this to use this.natural() | ||
return pool.charAt(Math.floor(this.random() * pool.length)); | ||
return pool.charAt(this.natural({max: (pool.length - 1)})); | ||
}; | ||
@@ -80,4 +81,5 @@ | ||
options = options || {}; | ||
var length = options.length || this.natural({min: 5, max: 20}), | ||
text = "", | ||
text = '', | ||
pool = options.pool; | ||
@@ -91,41 +93,158 @@ | ||
// -- End Basics -- | ||
// Address | ||
// Note: only returning US zip codes, internationalization will be a whole | ||
// other beast to tackle at some point. | ||
Chance.prototype.zip = function (options) { | ||
var zip = ""; | ||
// -- Text -- | ||
for (var i = 0; i < 5; i++) { | ||
zip += this.natural({min: 0, max: 9}).toString(); | ||
} | ||
Chance.prototype.syllable = function (options) { | ||
options = options || {}; | ||
if (options && options.plusfour === true) { | ||
zip += '-'; | ||
for (i = 0; i < 4; i++) { | ||
zip += this.natural({min: 0, max: 9}).toString(); | ||
var length = options.length || this.natural({min: 2, max: 3}), | ||
consanants = 'bcdfghjklmnprstvwz', // consonants except hard to speak ones | ||
vowels = 'aeiou', // vowels | ||
all = consanants + vowels, // all | ||
text = '', | ||
chr, pool; | ||
// I'm sure there's a more elegant way to do this, but this works | ||
// decently well. | ||
for (var i = 0; i < length; i++) { | ||
if (i === 0) { | ||
// First character can be anything | ||
chr = this.character({pool: all}); | ||
} else if (consanants.indexOf(chr) === -1) { | ||
// Last charcter was a vowel, now we want a consanant | ||
chr = this.character({pool: consanants}); | ||
} else { | ||
// Last charcter was a consanant, now we want a vowel | ||
chr = this.character({pool: vowels}); | ||
} | ||
text += chr; | ||
} | ||
return zip; | ||
return text; | ||
}; | ||
// Street Suffix | ||
Chance.prototype.street_suffixes = function (options) { | ||
var suffixes = []; | ||
if (options && options.full) { | ||
suffixes = ['Avenue', 'Boulevard', 'Center', 'Circle', 'Court', 'Drive', | ||
'Extension', 'Glen', 'Grove', 'Heights', 'Highway', 'Junction', | ||
'Key', 'Lane', 'Loop', 'Manor', 'Mill', 'Park', 'Parkway', 'Pass', | ||
'Path', 'Pike', 'Place', 'Plaza', 'Point', 'Ridge', 'River', 'Road', | ||
'Square', 'Street', 'Terrace', 'Trail', 'Turnpike', 'View', 'Way']; | ||
Chance.prototype.word = function (options) { | ||
options = options || {}; | ||
if (options.syllables && options.length) { | ||
throw new RangeError("Chance: Cannot specify both syllables AND length."); | ||
} | ||
var syllables = options.syllables || this.natural({min: 1, max: 3}), | ||
text = ''; | ||
if (options.length) { | ||
// Either bound word by length | ||
do { | ||
text += this.syllable(); | ||
} while (text.length < options.length); | ||
text = text.substring(0, options.length); | ||
} else { | ||
suffixes = ['Ave', 'Blvd', 'Ctr', 'Cir', 'Ct', 'Dr', 'Ext', 'Gln', 'Grv', | ||
'Hts', 'Hwy', 'Jct', 'Key', 'Ln', 'Loop', 'Mnr', 'Mill', 'Park', | ||
'Pkwy', 'Pass', 'Path', 'Pike', 'Pl', 'Plz', 'Pt', 'Rdg', 'Riv', | ||
'Rd', 'Sq', 'St', 'Ter', 'Trl', 'Tpke', 'Vw', 'Way']; | ||
// Or by number of syllables | ||
for (var i = 0; i < syllables; i++) { | ||
text += this.syllable(); | ||
} | ||
} | ||
return suffixes; | ||
return text; | ||
}; | ||
// Could get smarter about this than generating random words and | ||
// chaining them together. Such as: http://vq.io/1a5ceOh | ||
Chance.prototype.sentence = function (options) { | ||
options = options || {}; | ||
var words = options.words || this.natural({min: 12, max: 18}), | ||
text = '', word_array = []; | ||
for (var i = 0; i < words; i++) { | ||
word_array.push(this.word()); | ||
} | ||
text = word_array.join(' '); | ||
// Capitalize first letter of sentence, add period at end | ||
text = text.charAt(0).toUpperCase() + text.substr(1) + '.'; | ||
return text; | ||
}; | ||
Chance.prototype.paragraph = function (options) { | ||
options = options || {}; | ||
var sentences = options.sentences || this.natural({min: 3, max: 7}), | ||
sentence_array = []; | ||
for (var i = 0; i < sentences; i++) { | ||
sentence_array.push(this.sentence()); | ||
} | ||
return sentence_array.join(' '); | ||
}; | ||
// -- End Text -- | ||
// -- Address -- | ||
// Address | ||
Chance.prototype.address = function (options) { | ||
options = options || {}; | ||
return this.natural({min: 5, max: 2000}) + ' ' + this.street(options); | ||
}; | ||
// Street | ||
Chance.prototype.street = function (options) { | ||
options = options || {}; | ||
var street = this.word({syllables: 2}); | ||
street = street.charAt(0).toUpperCase() + street.substr(1); | ||
street += ' '; | ||
street += options.short_suffix ? | ||
this.street_suffix().abbreviation : | ||
this.street_suffix().name; | ||
return street; | ||
}; | ||
// Street Suffix | ||
Chance.prototype.street_suffixes = function () { | ||
return [ | ||
{name: 'Avenue', abbreviation: 'Ave'}, | ||
{name: 'Boulevard', abbreviation: 'Blvd'}, | ||
{name: 'Center', abbreviation: 'Ctr'}, | ||
{name: 'Circle', abbreviation: 'Cir'}, | ||
{name: 'Court', abbreviation: 'Ct'}, | ||
{name: 'Drive', abbreviation: 'Dr'}, | ||
{name: 'Extension', abbreviation: 'Ext'}, | ||
{name: 'Glen', abbreviation: 'Gln'}, | ||
{name: 'Grove', abbreviation: 'Grv'}, | ||
{name: 'Heights', abbreviation: 'Hts'}, | ||
{name: 'Highway', abbreviation: 'Hwy'}, | ||
{name: 'Junction', abbreviation: 'Jct'}, | ||
{name: 'Key', abbreviation: 'Key'}, | ||
{name: 'Lane', abbreviation: 'Ln'}, | ||
{name: 'Loop', abbreviation: 'Loop'}, | ||
{name: 'Manor', abbreviation: 'Mnr'}, | ||
{name: 'Mill', abbreviation: 'Mill'}, | ||
{name: 'Park', abbreviation: 'Park'}, | ||
{name: 'Parkway', abbreviation: 'Pkwy'}, | ||
{name: 'Pass', abbreviation: 'Pass'}, | ||
{name: 'Path', abbreviation: 'Path'}, | ||
{name: 'Pike', abbreviation: 'Pike'}, | ||
{name: 'Place', abbreviation: 'Pl'}, | ||
{name: 'Plaza', abbreviation: 'Plz'}, | ||
{name: 'Point', abbreviation: 'Pt'}, | ||
{name: 'Ridge', abbreviation: 'Rdg'}, | ||
{name: 'River', abbreviation: 'Riv'}, | ||
{name: 'Road', abbreviation: 'Rd'}, | ||
{name: 'Square', abbreviation: 'Sq'}, | ||
{name: 'Street', abbreviation: 'St'}, | ||
{name: 'Terrace', abbreviation: 'Ter'}, | ||
{name: 'Trail', abbreviation: 'Trl'}, | ||
{name: 'Turnpike', abbreviation: 'Tpke'}, | ||
{name: 'View', abbreviation: 'Vw'}, | ||
{name: 'Way', abbreviation: 'Way'} | ||
]; | ||
}; | ||
Chance.prototype.street_suffix = function (options) { | ||
@@ -140,63 +259,63 @@ // These are the most common suffixes. | ||
return [ | ||
{ name: "Alabama", abbreviation: "AL" }, | ||
{ name: "Alaska", abbreviation: "AK" }, | ||
{ name: "American Samoa", abbreviation: "AS" }, | ||
{ name: "Arizona", abbreviation: "AZ" }, | ||
{ name: "Arkansas", abbreviation: "AR" }, | ||
{ name: "Armed Forces Europe", abbreviation: "AE" }, | ||
{ name: "Armed Forces Pacific", abbreviation: "AP" }, | ||
{ name: "Armed Forces the Americas", abbreviation: "AA" }, | ||
{ name: "California", abbreviation: "CA" }, | ||
{ name: "Colorado", abbreviation: "CO" }, | ||
{ name: "Connecticut", abbreviation: "CT" }, | ||
{ name: "Delaware", abbreviation: "DE" }, | ||
{ name: "District of Columbia", abbreviation: "DC" }, | ||
{ name: "Federated States of Micronesia", abbreviation: "FM" }, | ||
{ name: "Florida", abbreviation: "FL" }, | ||
{ name: "Georgia", abbreviation: "GA" }, | ||
{ name: "Guam", abbreviation: "GU" }, | ||
{ name: "Hawaii", abbreviation: "HI" }, | ||
{ name: "Idaho", abbreviation: "ID" }, | ||
{ name: "Illinois", abbreviation: "IL" }, | ||
{ name: "Indiana", abbreviation: "IN" }, | ||
{ name: "Iowa", abbreviation: "IA" }, | ||
{ name: "Kansas", abbreviation: "KS" }, | ||
{ name: "Kentucky", abbreviation: "KY" }, | ||
{ name: "Louisiana", abbreviation: "LA" }, | ||
{ name: "Maine", abbreviation: "ME" }, | ||
{ name: "Marshall Islands", abbreviation: "MH" }, | ||
{ name: "Maryland", abbreviation: "MD" }, | ||
{ name: "Massachusetts", abbreviation: "MA" }, | ||
{ name: "Michigan", abbreviation: "MI" }, | ||
{ name: "Minnesota", abbreviation: "MN" }, | ||
{ name: "Mississippi", abbreviation: "MS" }, | ||
{ name: "Missouri", abbreviation: "MO" }, | ||
{ name: "Montana", abbreviation: "MT" }, | ||
{ name: "Nebraska", abbreviation: "NE" }, | ||
{ name: "Nevada", abbreviation: "NV" }, | ||
{ name: "New Hampshire", abbreviation: "NH" }, | ||
{ name: "New Jersey", abbreviation: "NJ" }, | ||
{ name: "New Mexico", abbreviation: "NM" }, | ||
{ name: "New York", abbreviation: "NY" }, | ||
{ name: "North Carolina", abbreviation: "NC" }, | ||
{ name: "North Dakota", abbreviation: "ND" }, | ||
{ name: "Northern Mariana Islands", abbreviation: "MP" }, | ||
{ name: "Ohio", abbreviation: "OH" }, | ||
{ name: "Oklahoma", abbreviation: "OK" }, | ||
{ name: "Oregon", abbreviation: "OR" }, | ||
{ name: "Pennsylvania", abbreviation: "PA" }, | ||
{ name: "Puerto Rico", abbreviation: "PR" }, | ||
{ name: "Rhode Island", abbreviation: "RI" }, | ||
{ name: "South Carolina", abbreviation: "SC" }, | ||
{ name: "South Dakota", abbreviation: "SD" }, | ||
{ name: "Tennessee", abbreviation: "TN" }, | ||
{ name: "Texas", abbreviation: "TX" }, | ||
{ name: "Utah", abbreviation: "UT" }, | ||
{ name: "Vermont", abbreviation: "VT" }, | ||
{ name: "Virgin Islands, U.S.", abbreviation: "VI" }, | ||
{ name: "Virginia", abbreviation: "VA" }, | ||
{ name: "Washington", abbreviation: "WA" }, | ||
{ name: "West Virginia", abbreviation: "WV" }, | ||
{ name: "Wisconsin", abbreviation: "WI" }, | ||
{ name: "Wyoming", abbreviation: "WY" } | ||
{name: 'Alabama', abbreviation: 'AL'}, | ||
{name: 'Alaska', abbreviation: 'AK'}, | ||
{name: 'American Samoa', abbreviation: 'AS'}, | ||
{name: 'Arizona', abbreviation: 'AZ'}, | ||
{name: 'Arkansas', abbreviation: 'AR'}, | ||
{name: 'Armed Forces Europe', abbreviation: 'AE'}, | ||
{name: 'Armed Forces Pacific', abbreviation: 'AP'}, | ||
{name: 'Armed Forces the Americas', abbreviation: 'AA'}, | ||
{name: 'California', abbreviation: 'CA'}, | ||
{name: 'Colorado', abbreviation: 'CO'}, | ||
{name: 'Connecticut', abbreviation: 'CT'}, | ||
{name: 'Delaware', abbreviation: 'DE'}, | ||
{name: 'District of Columbia', abbreviation: 'DC'}, | ||
{name: 'Federated States of Micronesia', abbreviation: 'FM'}, | ||
{name: 'Florida', abbreviation: 'FL'}, | ||
{name: 'Georgia', abbreviation: 'GA'}, | ||
{name: 'Guam', abbreviation: 'GU'}, | ||
{name: 'Hawaii', abbreviation: 'HI'}, | ||
{name: 'Idaho', abbreviation: 'ID'}, | ||
{name: 'Illinois', abbreviation: 'IL'}, | ||
{name: 'Indiana', abbreviation: 'IN'}, | ||
{name: 'Iowa', abbreviation: 'IA'}, | ||
{name: 'Kansas', abbreviation: 'KS'}, | ||
{name: 'Kentucky', abbreviation: 'KY'}, | ||
{name: 'Louisiana', abbreviation: 'LA'}, | ||
{name: 'Maine', abbreviation: 'ME'}, | ||
{name: 'Marshall Islands', abbreviation: 'MH'}, | ||
{name: 'Maryland', abbreviation: 'MD'}, | ||
{name: 'Massachusetts', abbreviation: 'MA'}, | ||
{name: 'Michigan', abbreviation: 'MI'}, | ||
{name: 'Minnesota', abbreviation: 'MN'}, | ||
{name: 'Mississippi', abbreviation: 'MS'}, | ||
{name: 'Missouri', abbreviation: 'MO'}, | ||
{name: 'Montana', abbreviation: 'MT'}, | ||
{name: 'Nebraska', abbreviation: 'NE'}, | ||
{name: 'Nevada', abbreviation: 'NV'}, | ||
{name: 'New Hampshire', abbreviation: 'NH'}, | ||
{name: 'New Jersey', abbreviation: 'NJ'}, | ||
{name: 'New Mexico', abbreviation: 'NM'}, | ||
{name: 'New York', abbreviation: 'NY'}, | ||
{name: 'North Carolina', abbreviation: 'NC'}, | ||
{name: 'North Dakota', abbreviation: 'ND'}, | ||
{name: 'Northern Mariana Islands', abbreviation: 'MP'}, | ||
{name: 'Ohio', abbreviation: 'OH'}, | ||
{name: 'Oklahoma', abbreviation: 'OK'}, | ||
{name: 'Oregon', abbreviation: 'OR'}, | ||
{name: 'Pennsylvania', abbreviation: 'PA'}, | ||
{name: 'Puerto Rico', abbreviation: 'PR'}, | ||
{name: 'Rhode Island', abbreviation: 'RI'}, | ||
{name: 'South Carolina', abbreviation: 'SC'}, | ||
{name: 'South Dakota', abbreviation: 'SD'}, | ||
{name: 'Tennessee', abbreviation: 'TN'}, | ||
{name: 'Texas', abbreviation: 'TX'}, | ||
{name: 'Utah', abbreviation: 'UT'}, | ||
{name: 'Vermont', abbreviation: 'VT'}, | ||
{name: 'Virgin Islands, U.S.', abbreviation: 'VI'}, | ||
{name: 'Virginia', abbreviation: 'VA'}, | ||
{name: 'Washington', abbreviation: 'WA'}, | ||
{name: 'West Virginia', abbreviation: 'WV'}, | ||
{name: 'Wisconsin', abbreviation: 'WI'}, | ||
{name: 'Wyoming', abbreviation: 'WY'} | ||
]; | ||
@@ -214,4 +333,25 @@ }; | ||
// Miscellaneous | ||
// Note: only returning US zip codes, internationalization will be a whole | ||
// other beast to tackle at some point. | ||
Chance.prototype.zip = function (options) { | ||
var zip = ""; | ||
for (var i = 0; i < 5; i++) { | ||
zip += this.natural({min: 0, max: 9}).toString(); | ||
} | ||
if (options && options.plusfour === true) { | ||
zip += '-'; | ||
for (i = 0; i < 4; i++) { | ||
zip += this.natural({min: 0, max: 9}).toString(); | ||
} | ||
} | ||
return zip; | ||
}; | ||
// -- End Address -- | ||
// -- Miscellaneous -- | ||
// Dice - For all the board game geeks out there, myself included ;) | ||
@@ -236,5 +376,10 @@ Chance.prototype.d4 = function () { return this.natural({min: 1, max: 4}); }; | ||
Chance.prototype.mersenne_twister = function (seed) { | ||
return new MersenneTwister(seed); | ||
}; | ||
Chance.prototype.VERSION = 0.1; | ||
// -- End Miscellaneous -- | ||
Chance.prototype.VERSION = "0.2.1"; | ||
// Mersenne Twister from https://gist.github.com/banksean/300494 | ||
@@ -241,0 +386,0 @@ var MersenneTwister = function (seed) { |
{ | ||
"name": "chance", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Chance - Utility library to generate anything random", | ||
@@ -5,0 +5,0 @@ "homepage": "http://chancejs.com", |
@@ -6,6 +6,3 @@ require.config({ | ||
'mocha': '../node_modules/mocha/mocha', | ||
'underscore': '../node_modules/underscore/underscore', | ||
// Test libraries | ||
'test.basic.js': 'test.basic' | ||
'underscore': '../node_modules/underscore/underscore' | ||
}, | ||
@@ -31,3 +28,3 @@ shim: { | ||
var assert = chai.assert; | ||
require(['test.basic.js', 'test.misc.js', 'test.address.js'], function () { | ||
require(['test.basic', 'test.text', 'test.address', 'test.misc'], function () { | ||
mocha.reporter('html'); | ||
@@ -34,0 +31,0 @@ |
@@ -6,3 +6,3 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
describe("Address", function () { | ||
var zip, suffix, state, chance = new Chance(); | ||
var zip, suffix, suffixes, state, address, chance = new Chance(); | ||
@@ -31,20 +31,19 @@ describe("Zip", function () { | ||
expect(suffixes).to.be.an('array'); | ||
_.each(suffixes, function (suffix) { | ||
expect(suffix).to.have.keys('name', 'abbreviation'); | ||
}); | ||
}); | ||
it("street suffixes are short", function () { | ||
_(1000).times(function () { | ||
suffixes = chance.street_suffixes(); | ||
expect(suffixes[0].length).to.be.below(5); | ||
suffixes = chance.street_suffixes(); | ||
_.each(suffixes, function (suffix) { | ||
expect(suffix.abbreviation).to.have.length.below(5); | ||
}); | ||
}); | ||
it("full street suffixes returns the suffix array", function () { | ||
suffixes = chance.street_suffixes({full: true}); | ||
expect(suffixes).to.be.an('array'); | ||
}); | ||
it("full street suffixes are longish", function () { | ||
_(1000).times(function () { | ||
suffixes = chance.street_suffixes(); | ||
expect(suffixes[0].length).to.be.above(2); | ||
suffixes = chance.street_suffixes(); | ||
_.each(suffixes, function (suffix) { | ||
expect(suffix.name).to.have.length.above(2); | ||
}); | ||
@@ -56,6 +55,7 @@ }); | ||
suffix = chance.street_suffix(); | ||
expect(suffix.length).to.be.below(5); | ||
expect(suffix).to.be.an('object'); | ||
expect(suffix.name).to.be.a('string'); | ||
expect(suffix.abbreviation).to.be.a('string'); | ||
}); | ||
}); | ||
}); | ||
@@ -82,3 +82,22 @@ | ||
}); | ||
describe("Address", function () { | ||
it("address() returns a string", function () { | ||
expect(chance.address()).to.be.an('string'); | ||
}); | ||
it("address() starts with a number", function () { | ||
_(1000).times(function () { | ||
expect(chance.address()).to.match(/[0-9]+.+/); | ||
}); | ||
}); | ||
it("address() can take short_suffix arg and obey it", function () { | ||
_(1000).times(function () { | ||
address = chance.address({short_suffix: true}); | ||
expect(address.split(' ')[2].length).to.be.below(5); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
40506
11
894