Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change Faye.random() so we stay within the float accuracy bounds in JS.

  • Loading branch information...
commit 265b9e5bc8877b054dc26dd48b5bdb7f24ce723a 1 parent c7086e1
@jcoglan jcoglan authored
Showing with 7 additions and 5 deletions.
  1. +6 −4 javascript/faye.js
  2. +1 −1  spec/javascript/faye_spec.js
View
10 javascript/faye.js
@@ -26,11 +26,13 @@ Faye.extend(Faye, {
random: function(bitlength) {
bitlength = bitlength || this.ID_LENGTH;
- if (bitlength > 40) {
- var parts = Math.ceil(bitlength / 40),
+ if (bitlength > 32) {
+ var parts = Math.ceil(bitlength / 32),
string = '';
- while (parts--) string += this.random(40);
- return string;
+ while (parts--) string += this.random(32);
+ var chars = string.split(''), result = '';
+ while (chars.length > 0) result += chars.pop();
+ return result;
}
var limit = Math.pow(2, bitlength) - 1,
maxSize = limit.toString(36).length,
View
2  spec/javascript/faye_spec.js
@@ -8,7 +8,7 @@ JS.ENV.FayeSpec = JS.Test.describe("Faye", function() { with(this) {
it("always produces the same length of string", function() { with(this) {
var ids = $R(1,100).map(function() { return Faye.random().length })
- var expected = $R(1,100).map(function() { return 32 })
+ var expected = $R(1,100).map(function() { return 35 })
assertEqual( expected, ids )
}})
}})
Please sign in to comment.
Something went wrong with that request. Please try again.