Comparing version 0.1.1 to 0.2.0
@@ -62,2 +62,27 @@ // Chance.js 0.1 | ||
Chance.prototype.character = function (options) { | ||
options = options || {}; | ||
var lower = "abcdefghijklmnopqrstuvwxyz", | ||
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
numbers = "0123456789", | ||
chars = "!@#$%^&*()", | ||
pool = options.pool || (lower + upper + numbers + chars); | ||
// Refactor this to use this.natural() | ||
return pool.charAt(Math.floor(this.random() * pool.length)); | ||
}; | ||
Chance.prototype.string = function (options) { | ||
options = options || {}; | ||
var length = options.length || this.natural({min: 5, max: 20}), | ||
text = "", | ||
pool = options.pool; | ||
for (var i = 0; i < length; i++) { | ||
text += this.character({pool: pool}); | ||
} | ||
return text; | ||
}; | ||
// Address | ||
@@ -183,2 +208,4 @@ // Note: only returning US zip codes, internationalization will be a whole | ||
// Miscellaneous | ||
// Dice - For all the board game geeks out there, myself included ;) | ||
@@ -192,14 +219,14 @@ Chance.prototype.d4 = function () { return this.natural({min: 1, max: 4}); }; | ||
// Guid | ||
Chance.prototype.guid = function () { | ||
var guid_pool = "ABCDEF1234567890", | ||
guid = this.string({pool: guid_pool, length: 8}) + '-' + | ||
this.string({pool: guid_pool, length: 4}) + '-' + | ||
this.string({pool: guid_pool, length: 4}) + '-' + | ||
this.string({pool: guid_pool, length: 4}) + '-' + | ||
this.string({pool: guid_pool, length: 12}); | ||
return guid; | ||
}; | ||
Chance.prototype.str = function (length) { | ||
var text = ""; | ||
var possible = "abcdefghijklmnopqrstuvwxyz"; | ||
for (var i = 0; i < length; i++) { | ||
text += possible.charAt(Math.floor(this.random() * possible.length)); | ||
} | ||
return text; | ||
}; | ||
Chance.prototype.VERSION = 0.1; | ||
@@ -206,0 +233,0 @@ |
{ | ||
"name": "chance", | ||
"version": "0.1.1", | ||
"description": "ChanceJS - Random generator helper for JavaScript", | ||
"version": "0.2.0", | ||
"description": "Chance - Utility library to generate anything random", | ||
"homepage": "http://chancejs.com", | ||
@@ -38,4 +38,8 @@ "author": "Victor Quinn <mail@victorquinn.com>", | ||
"generator", | ||
"test" | ||
"test", | ||
"mersenne", | ||
"name", | ||
"address", | ||
"dice" | ||
] | ||
} |
@@ -1,6 +0,50 @@ | ||
ChanceJS | ||
======== | ||
# Chance | ||
ChanceJS - Random generator helper for JavaScript | ||
Chance - Random generator helper for JavaScript | ||
Homepage: [http://chancejs.com](http://chancejs.com) | ||
Many more details on [http://chancejs.com](http://chancejs.com) but this single | ||
library can generate random numbers, characters, strings, names, addresses, | ||
dice, and pretty much anything else. | ||
It includes the basic building blocks for all these items and is built on top | ||
of a Mersenne Twister so it can generate these things with repeatibility, if | ||
desired. | ||
## Usage | ||
### Browser | ||
Chance instantiates itself onto the window. This means that in the simplest case you can just include the script tag then use an instance of Chance immediately. | ||
<script src="chance.js"></script> | ||
<script> | ||
console.log(chance.string()); | ||
</script> | ||
[More info](http://chancejs.com#browser) | ||
### RequireJS | ||
Chance also includes an AMD define so it can be used with RequireJS. | ||
require(['Chance'], function(Chance) { | ||
// Instantiate | ||
var chance = new Chance(); | ||
// Then just use it: | ||
var my_random_integer = chance.integer(); | ||
}); | ||
### Node.js | ||
And it can be used in Node.js. | ||
var Chance = require('Chance'), | ||
chance = new Chance(); | ||
// Get a random zip code | ||
chance.zip(); | ||
define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
var assert = chai.assert, | ||
expect = chai.expect; | ||
var expect = chai.expect; | ||
describe("Basics", function () { | ||
var bool, integer, natural, chance = new Chance(); | ||
var bool, integer, natural, character, string, chance = new Chance(); | ||
@@ -11,5 +10,3 @@ describe("Bool", function () { | ||
bool = chance.bool(); | ||
assert.isBoolean(bool); | ||
assert.isNotNumber(bool); | ||
assert.isNotString(bool); | ||
expect(bool).to.be.a('boolean'); | ||
}); | ||
@@ -31,4 +28,3 @@ | ||
// test failing and submits a pull request adding it to this comment! | ||
expect(true_count).to.be.above(200); | ||
expect(true_count).to.be.below(800); | ||
expect(true_count).to.be.within(200, 800); | ||
}); | ||
@@ -40,5 +36,3 @@ }); | ||
integer = chance.integer(); | ||
assert.isNotBoolean(integer); | ||
assert.isNumber(integer); | ||
assert.isNotString(integer); | ||
expect(integer).to.be.a('number'); | ||
}); | ||
@@ -58,4 +52,3 @@ | ||
// willing to accept it. | ||
expect(positive_count).to.be.above(200); | ||
expect(positive_count).to.be.below(800); | ||
expect(positive_count).to.be.within(200, 800); | ||
}); | ||
@@ -73,4 +66,3 @@ | ||
integer = chance.integer({min: -25, max: -1}); | ||
expect(integer).to.be.above(-26); | ||
expect(integer).to.be.below(0); | ||
expect(integer).to.be.within(-26, 0); | ||
}); | ||
@@ -83,5 +75,3 @@ }); | ||
natural = chance.natural(); | ||
assert.isNotBoolean(natural); | ||
assert.isNumber(natural); | ||
assert.isNotString(natural); | ||
expect(natural).to.be.a('number'); | ||
}); | ||
@@ -118,7 +108,39 @@ | ||
natural = chance.natural({min: 90, max: 100}); | ||
expect(natural).to.be.below(101); | ||
expect(natural).to.be.above(89); | ||
expect(natural).to.be.within(89, 101); | ||
}); | ||
}); | ||
}); | ||
describe("Character", function () { | ||
it("returns a character", function () { | ||
character = chance.character(); | ||
expect(character).to.be.a('string'); | ||
expect(character).to.have.length(1); | ||
}); | ||
it("pulls only from pool, when specified", function () { | ||
var pool = 'abcde'; | ||
_(1000).times(function () { | ||
character = chance.character({pool: pool}); | ||
expect(character).to.match(/[abcde]/); | ||
}); | ||
}); | ||
}); | ||
describe("String", function () { | ||
it("returns a string", function () { | ||
string = chance.string(); | ||
expect(string).to.be.a('string'); | ||
}); | ||
it("obeys length, when specified", function () { | ||
var length; | ||
_(1000).times(function () { | ||
// Generate a random length from 1-25 | ||
length = chance.natural({min: 1, max: 25}); | ||
string = chance.string({length: length}); | ||
expect(string).to.have.length(length); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -125,0 +147,0 @@ |
define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai, _) { | ||
var assert = chai.assert, | ||
expect = chai.expect; | ||
var expect = chai.expect; | ||
@@ -11,4 +10,3 @@ describe("Dice", function () { | ||
die = chance.d4(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(5); | ||
expect(die).to.be.within(1, 4); | ||
}); | ||
@@ -20,4 +18,3 @@ }); | ||
die = chance.d6(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(7); | ||
expect(die).to.be.within(1, 6); | ||
}); | ||
@@ -29,4 +26,3 @@ }); | ||
die = chance.d8(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(9); | ||
expect(die).to.be.within(1, 8); | ||
}); | ||
@@ -38,4 +34,3 @@ }); | ||
die = chance.d10(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(11); | ||
expect(die).to.be.within(1, 10); | ||
}); | ||
@@ -47,4 +42,3 @@ }); | ||
die = chance.d12(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(13); | ||
expect(die).to.be.within(1, 12); | ||
}); | ||
@@ -56,7 +50,17 @@ }); | ||
die = chance.d20(); | ||
expect(die).to.be.above(0); | ||
expect(die).to.be.below(21); | ||
expect(die).to.be.within(1, 20); | ||
}); | ||
}); | ||
}); | ||
describe("Guid", function () { | ||
var guid, chance = new Chance(); | ||
it("returns a proper guid", function () { | ||
_(1000).times(function () { | ||
guid = chance.guid(); | ||
expect(guid).to.match(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
31406
679
51