Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: victorquinn/chancejs
base: 0.3.1
...
head fork: victorquinn/chancejs
compare: 0.3.2
  • 12 commits
  • 6 files changed
  • 1 commit comment
  • 2 contributors
View
3  .travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - "0.10"
View
4 README.md
@@ -1,5 +1,7 @@
# Chance
+[![Build Status](https://travis-ci.org/victorquinn/chancejs.png)](https://travis-ci.org/victorquinn/chancejs)
+
Chance - Random generator helper for JavaScript
Homepage: [http://chancejs.com](http://chancejs.com)
@@ -42,7 +44,7 @@ Chance also includes an AMD define so it can be used with RequireJS.
And it can be used in Node.js.
- var Chance = require('Chance'),
+ var Chance = require('chance'),
chance = new Chance();
// Get a random zip code
View
28 chance.js
@@ -1,4 +1,4 @@
-// Chance.js 0.3.1
+// Chance.js 0.3.2
// http://chancejs.com
// (c) 2013 Victor Quinn
// Chance may be freely distributed or modified under the MIT license.
@@ -32,10 +32,10 @@
Chance.prototype.natural = function (options) {
options = options || {};
- options.min = options.min || 0;
+ options.min = (typeof options.min !== "undefined") ? options.min : 0;
// 9007199254740992 (2^53) is the max integer number in JavaScript
// See: http://vq.io/132sa2j
- options.max = options.max || 9007199254740992;
+ options.max = (typeof options.max !== "undefined") ? options.max : 9007199254740992;
return Math.floor(this.random() * (options.max - options.min + 1) + options.min);
};
@@ -49,7 +49,7 @@
// Greatest of absolute value of either max or min so we know we're
// including the entire search domain.
- range = Math.max(Math.abs(options.min), Math.abs(options.min));
+ range = Math.max(Math.abs(options.min), Math.abs(options.max));
// Probably a better way to do this...
do {
@@ -216,11 +216,20 @@
// -- Name --
+ // Perhaps make this more intelligent at some point
+ Chance.prototype.first = function (options) {
+ return this.capitalize(this.word());
+ };
+
+ Chance.prototype.last = function (options) {
+ return this.capitalize(this.word());
+ };
+
Chance.prototype.name = function (options) {
options = options || {};
- var first = this.capitalize(this.word()),
- last = this.capitalize(this.word()),
+ var first = this.first(),
+ last = this.last(),
name;
if (options.middle) {
@@ -247,6 +256,11 @@
];
};
+ // Alias for name_prefix
+ Chance.prototype.prefix = function (options) {
+ return this.name_prefix(options);
+ };
+
Chance.prototype.name_prefix = function (options) {
options = options || {};
return options.full ?
@@ -487,7 +501,7 @@
// -- End Miscellaneous --
- Chance.prototype.VERSION = "0.3.1";
+ Chance.prototype.VERSION = "0.3.2";
// Mersenne Twister from https://gist.github.com/banksean/300494
var MersenneTwister = function (seed) {
View
5 package.json
@@ -1,6 +1,6 @@
{
"name": "chance",
- "version": "0.3.1",
+ "version": "0.3.2",
"description": "Chance - Utility library to generate anything random",
"homepage": "http://chancejs.com",
"author": "Victor Quinn <mail@victorquinn.com>",
@@ -32,6 +32,9 @@
"uglify-js": "1.2.6",
"underscore": "*"
},
+ "scripts": {
+ "test": "./node_modules/mocha-phantomjs/bin/mocha-phantomjs test/runner.html"
+ },
"keywords": [
"random",
"generator",
View
20 test/test.basic.js
@@ -69,6 +69,19 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai,
expect(integer).to.be.within(-26, 0);
});
});
+
+ it("can take a min with absolute value less than the max and return in range above", function () {
+ var count = 0;
+ _(1000).times(function () {
+ // With a range this large we'd expect most values to be
+ // greater than 1 if this works correctly.
+ integer = chance.integer({min: -1, max: 1000000});
+ if (Math.abs(integer) < 2) {
+ count++;
+ }
+ });
+ expect(count).to.not.be.above(900);
+ });
});
describe("Natural", function () {
@@ -109,6 +122,13 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai,
expect(natural).to.be.within(89, 101);
});
});
+
+ it("works with both bounds 0", function () {
+ _(1000).times(function () {
+ natural = chance.natural({min: 0, max: 0});
+ expect(natural).to.equal(0);
+ });
+ });
});
describe("Character", function () {
View
33 test/test.name.js
@@ -2,7 +2,7 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai,
var expect = chai.expect;
describe("Name", function () {
- var name, prefix, chance = new Chance();
+ var name, first, last, prefix, chance = new Chance();
describe("name()", function () {
it("returns a random name", function () {
@@ -31,6 +31,37 @@ define(['Chance', 'mocha', 'chai', 'underscore'], function (Chance, mocha, chai,
expect(name.indexOf('.')).to.be.ok;
});
});
+ it("can have the prefix specified", function () {
+ _(1000).times(function () {
+ name = chance.name({prefix: true});
+ expect(name).to.be.a('string');
+ expect(name.split(' ')).to.have.length(3);
+ });
+ });
+
+ });
+
+ describe("first()", function () {
+ it("returns a random first name", function () {
+ _(1000).times(function () {
+ first = chance.first();
+ expect(first).to.be.a('string');
+ expect(first).to.have.length.within(2, 20);
+ expect(first.split(' ')).to.have.length(1);
+ });
+ });
+
+ });
+
+ describe("last()", function () {
+ it("returns a random last name", function () {
+ _(1000).times(function () {
+ last = chance.last();
+ expect(last).to.be.a('string');
+ expect(last).to.have.length.within(2, 20);
+ expect(last.split(' ')).to.have.length(1);
+ });
+ });
});

Showing you all comments on commits in this comparison.

@nicolashery

Thanks!

Something went wrong with that request. Please try again.