Comparing version 0.2.1 to 0.3.0
181
chance.js
@@ -1,2 +0,2 @@ | ||
// Chance.js 0.2.1 | ||
// Chance.js 0.3.0 | ||
// http://chancejs.com | ||
@@ -69,5 +69,28 @@ // (c) 2013 Victor Quinn | ||
numbers = "0123456789", | ||
chars = "!@#$%^&*()", | ||
pool = options.pool || (lower + upper + numbers + chars); | ||
symbols = "!@#$%^&*()[]", | ||
letters, pool; | ||
if (options.alpha && options.symbols) { | ||
throw new RangeError("Chance: Cannot specify both alpha and symbols."); | ||
} | ||
if (options.casing === 'lower') { | ||
letters = lower; | ||
} else if (options.casing === 'upper') { | ||
letters = upper; | ||
} else { | ||
letters = lower + upper; | ||
} | ||
if (options.pool) { | ||
pool = options.pool; | ||
} else if (options.alpha) { | ||
pool = letters; | ||
} else if (options.symbols) { | ||
pool = symbols; | ||
} else { | ||
pool = letters + numbers + symbols; | ||
} | ||
return pool.charAt(this.natural({max: (pool.length - 1)})); | ||
@@ -91,4 +114,87 @@ }; | ||
// -- Helpers -- | ||
Chance.prototype.capitalize = function (word) { | ||
return word.charAt(0).toUpperCase() + word.substr(1); | ||
}; | ||
Chance.prototype.pick = function (arr) { | ||
return arr[this.natural({max: arr.length - 1})]; | ||
}; | ||
// -- End Helpers -- | ||
// -- Text -- | ||
Chance.prototype.name_prefixes = function () { | ||
return [ | ||
{name: 'Doctor', abbreviation: 'Dr.'}, | ||
{name: 'Miss', abbreviation: 'Miss'}, | ||
{name: 'Misses', abbreviation: 'Mrs.'}, | ||
{name: 'Mister', abbreviation: 'Mr.'} | ||
]; | ||
}; | ||
Chance.prototype.name_prefix = function (options) { | ||
options = options || {}; | ||
return options.full ? | ||
this.pick(this.name_prefixes()).name : | ||
this.pick(this.name_prefixes()).abbreviation; | ||
}; | ||
Chance.prototype.name = function (options) { | ||
options = options || {}; | ||
var first = this.capitalize(this.word()), | ||
last = this.capitalize(this.word()), | ||
name; | ||
if (options.middle) { | ||
name = first + ' ' + this.capitalize(this.word()) + ' ' + last; | ||
} else if (options.middle_initial) { | ||
name = first + ' ' + this.character({alpha: true, casing: 'upper'}) + '. ' + last; | ||
} else { | ||
name = first + ' ' + last; | ||
} | ||
if (options.prefix) { | ||
name = this.prefix() + ' ' + name; | ||
} | ||
return name; | ||
}; | ||
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(' '); | ||
}; | ||
// 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 = this.capitalize(text) + '.'; | ||
return text; | ||
}; | ||
Chance.prototype.syllable = function (options) { | ||
@@ -149,46 +255,26 @@ options = options || {}; | ||
// 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 || {}; | ||
// -- End Text -- | ||
var words = options.words || this.natural({min: 12, max: 18}), | ||
text = '', word_array = []; | ||
// -- Address -- | ||
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.address = function (options) { | ||
options = options || {}; | ||
return this.natural({min: 5, max: 2000}) + ' ' + this.street(options); | ||
}; | ||
Chance.prototype.paragraph = function (options) { | ||
Chance.prototype.city = 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(' '); | ||
return this.capitalize(this.word({syllables: 3})); | ||
}; | ||
// -- End Text -- | ||
// -- Address -- | ||
// Address | ||
Chance.prototype.address = function (options) { | ||
Chance.prototype.phone = function (options) { | ||
options = options || {}; | ||
return this.natural({min: 5, max: 2000}) + ' ' + this.street(options); | ||
return this.areacode() + ' ' + this.natural({min: 200, max: 999}) + '-' + this.natural({min: 1000, max: 9999}); | ||
}; | ||
// Street | ||
Chance.prototype.areacode = function (options) { | ||
// Don't want area codes to start with 1 | ||
return '(' + this.natural({min: 2, max: 9}) + this.natural({min: 10, max: 98}) + ')'; | ||
}; | ||
Chance.prototype.street = function (options) { | ||
@@ -198,3 +284,3 @@ options = options || {}; | ||
var street = this.word({syllables: 2}); | ||
street = street.charAt(0).toUpperCase() + street.substr(1); | ||
street = this.capitalize(street); | ||
street += ' '; | ||
@@ -207,4 +293,4 @@ street += options.short_suffix ? | ||
// Street Suffix | ||
Chance.prototype.street_suffixes = function () { | ||
// These are the most common suffixes. | ||
return [ | ||
@@ -250,6 +336,3 @@ {name: 'Avenue', abbreviation: 'Ave'}, | ||
Chance.prototype.street_suffix = function (options) { | ||
// These are the most common suffixes. | ||
var suffixes = this.street_suffixes(options), | ||
suffix = suffixes[this.natural({max: suffixes.length - 1})]; | ||
return suffix; | ||
return this.pick(this.street_suffixes(options)); | ||
}; | ||
@@ -324,8 +407,5 @@ | ||
Chance.prototype.state = function (options) { | ||
var states = this.states(), | ||
state = (options && options.full) ? | ||
states[this.natural({max: states.length - 1})].name : | ||
states[this.natural({max: states.length - 1})].abbreviation; | ||
return state; | ||
return (options && options.full) ? | ||
this.pick(this.states()).name : | ||
this.pick(this.states()).abbreviation; | ||
}; | ||
@@ -363,2 +443,3 @@ | ||
Chance.prototype.d20 = function () { return this.natural({min: 1, max: 20}); }; | ||
Chance.prototype.d100 = function () { return this.natural({min: 1, max: 100}); }; | ||
@@ -382,3 +463,3 @@ // Guid | ||
Chance.prototype.VERSION = "0.2.1"; | ||
Chance.prototype.VERSION = "0.3.0"; | ||
@@ -385,0 +466,0 @@ // Mersenne Twister from https://gist.github.com/banksean/300494 |
{ | ||
"name": "chance", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Chance - Utility library to generate anything random", | ||
@@ -5,0 +5,0 @@ "homepage": "http://chancejs.com", |
@@ -27,3 +27,3 @@ require.config({ | ||
var assert = chai.assert; | ||
require(['test.basic', 'test.text', 'test.address', 'test.misc'], function () { | ||
require(['test.basic', 'test.text', 'test.address', 'test.misc', 'test.name', 'test.helpers'], function () { | ||
mocha.reporter('html'); | ||
@@ -30,0 +30,0 @@ |
@@ -6,3 +6,3 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
describe("Address", function () { | ||
var zip, suffix, suffixes, state, address, chance = new Chance(); | ||
var zip, suffix, suffixes, state, address, phone, chance = new Chance(); | ||
@@ -99,3 +99,28 @@ describe("Zip", function () { | ||
}); | ||
describe("Phone Number", function () { | ||
it("areacode() looks right", function () { | ||
expect(chance.areacode()).to.be.a('string'); | ||
_(1000).times(function () { | ||
expect(chance.areacode()).to.match(/\(([0-9]{3})\)/); | ||
}); | ||
}); | ||
it("phone() returns a string", function () { | ||
expect(chance.phone()).to.be.a('string'); | ||
}); | ||
it("phone() looks like an actual phone number", function () { | ||
_(1000).times(function () { | ||
expect(chance.phone()).to.match(/\(([0-9]{3})\)?[\-. ]?([0-9]{3})[\-. ]?([0-9]{4})/); | ||
}); | ||
}); | ||
}); | ||
describe("City", function () { | ||
it("city() looks right", function () { | ||
expect(chance.city()).to.be.a('string'); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -121,2 +121,29 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
}); | ||
it("allows only alpha", function () { | ||
_(1000).times(function () { | ||
character = chance.character({alpha: true}); | ||
expect(character).to.match(/[a-zA-Z]/); | ||
}); | ||
expect(function () { chance.character({alpha: true, symbols: true}); }).to.throw(RangeError); | ||
}); | ||
it("obeys case", function () { | ||
_(1000).times(function () { | ||
character = chance.character({alpha: true}); | ||
expect(character).to.match(/[a-zA-Z]/); | ||
}); | ||
_(1000).times(function () { | ||
character = chance.character({alpha: true, casing: 'upper'}); | ||
expect(character).to.match(/[A-Z]/); | ||
}); | ||
_(1000).times(function () { | ||
character = chance.character({alpha: true, casing: 'lower'}); | ||
expect(character).to.match(/[a-z]/); | ||
}); | ||
}); | ||
}); | ||
@@ -123,0 +150,0 @@ |
@@ -48,2 +48,9 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
}); | ||
it("returns a properly bounded d100", function () { | ||
_(1000).times(function () { | ||
die = chance.d100(); | ||
expect(die).to.be.within(1, 100); | ||
}); | ||
}); | ||
}); | ||
@@ -50,0 +57,0 @@ |
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
48052
13
1078