Added random/guid and random/randByte #70

Merged
merged 9 commits into from Aug 20, 2012

Conversation

Projects
None yet
2 participants
@jsoverson
Contributor

jsoverson commented Aug 19, 2012

Added the ability to generate random guid strings. Added randByte to make a piece of it more reusable.

guid(); // "17836975-db96-d8e2-48c7-d2e550172ad7"
guid(); // "213cf024-0894-0363-96c4-c859e5fa1281"

Added docs and specs using existing code as templates.

If you want to go over the code, I'm on irc in the requirejs channel generally.

@millermedeiros

This comment has been minimized.

Show comment
Hide comment
@millermedeiros

millermedeiros Aug 19, 2012

Owner

what do you think about creating a method that returns a random hex string following any pattern?

var _chars = '0123456789abcdef'.split('');
var _defaultPattern = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx';

function guid(pattern){
 var str = pattern || _defaultPattern;
 str = str.replace(/x/g, function(){
   return _chars[ ~~(chars.length * Math.random()) ];
 });
 return str;
}

so the user could do:

guid();          // "0b89907a-6344-6038-a496-5e5ca110e2"
guid('xx-xx');   // "fd-6d"
guid('#xxxxxx'); // "#b7f16c"

I usually fell that it's better to provide generic methods than very specific ones.

Owner

millermedeiros commented Aug 19, 2012

what do you think about creating a method that returns a random hex string following any pattern?

var _chars = '0123456789abcdef'.split('');
var _defaultPattern = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx';

function guid(pattern){
 var str = pattern || _defaultPattern;
 str = str.replace(/x/g, function(){
   return _chars[ ~~(chars.length * Math.random()) ];
 });
 return str;
}

so the user could do:

guid();          // "0b89907a-6344-6038-a496-5e5ca110e2"
guid('xx-xx');   // "fd-6d"
guid('#xxxxxx'); // "#b7f16c"

I usually fell that it's better to provide generic methods than very specific ones.

@millermedeiros

This comment has been minimized.

Show comment
Hide comment
@millermedeiros

millermedeiros Aug 19, 2012

Owner

I was thinking about it, usually when I need to generate unique IDs I use some kind of counter to avoid generating the same value twice. I think we should probably name the method that returns a random hexadecimal value as randHex() and default it to a 6 char string (hex color without the #) and do it like I did above allowing a custom format for the string.

What do you think?

Owner

millermedeiros commented Aug 19, 2012

I was thinking about it, usually when I need to generate unique IDs I use some kind of counter to avoid generating the same value twice. I think we should probably name the method that returns a random hexadecimal value as randHex() and default it to a 6 char string (hex color without the #) and do it like I did above allowing a custom format for the string.

What do you think?

millermedeiros added a commit that referenced this pull request Aug 20, 2012

@millermedeiros

This comment has been minimized.

Show comment
Hide comment
@millermedeiros

millermedeiros Aug 20, 2012

Owner

@jsoverson I added the randHex() method so now it's very easy to create a guid() method like you want:

function guid(){
 return randHex(8) +'-'+ randHex(4) +'-' randHex(4) +'-'+ randHex(4) +'-'+ randHex(12);
}

or even:

function guid(){
  return ('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx').replace(/x/g, function(){ return randHex(1) });
}

I don't have plans on merging the guid method as is since it is too specific and it has a very small chance of generating the same value twice (1 in 16^32).

Do you fell the randByte would be useful enough to deserve it's own method? specially since it is basically a curried version of randHex(2) ?

Thanks for the pull request and feedback.

Owner

millermedeiros commented Aug 20, 2012

@jsoverson I added the randHex() method so now it's very easy to create a guid() method like you want:

function guid(){
 return randHex(8) +'-'+ randHex(4) +'-' randHex(4) +'-'+ randHex(4) +'-'+ randHex(12);
}

or even:

function guid(){
  return ('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx').replace(/x/g, function(){ return randHex(1) });
}

I don't have plans on merging the guid method as is since it is too specific and it has a very small chance of generating the same value twice (1 in 16^32).

Do you fell the randByte would be useful enough to deserve it's own method? specially since it is basically a curried version of randHex(2) ?

Thanks for the pull request and feedback.

@jsoverson

This comment has been minimized.

Show comment
Hide comment
@jsoverson

jsoverson Aug 20, 2012

Contributor

Well, the term "guid" is generally mean to refer to a formal Globally Unique Identifier which is formatted the same way everywhere. The possibility of generating identical guids in that format is considered negligible.

JavaScript can't make a genuine guid so the best it can do is a random hex (I ignored the the fixed bytes denoting a randomly generated guid but that would be a good addition).

Having a consistent format allows you to manage resources in a consistent way without having to accomodate multiple ID styles.

The random hex is fine and better. The existing implementation was just due to me being blinded working with bits and bytes for another project.

Contributor

jsoverson commented Aug 20, 2012

Well, the term "guid" is generally mean to refer to a formal Globally Unique Identifier which is formatted the same way everywhere. The possibility of generating identical guids in that format is considered negligible.

JavaScript can't make a genuine guid so the best it can do is a random hex (I ignored the the fixed bytes denoting a randomly generated guid but that would be a good addition).

Having a consistent format allows you to manage resources in a consistent way without having to accomodate multiple ID styles.

The random hex is fine and better. The existing implementation was just due to me being blinded working with bits and bytes for another project.

@millermedeiros

This comment has been minimized.

Show comment
Hide comment
@millermedeiros

millermedeiros Aug 20, 2012

Owner

@jsoverson makes sense. do you want to refactor it or should I do it? I think we should add the "4" as first char on the 3rd group like the wikipedia article says about GUID v4.

Owner

millermedeiros commented Aug 20, 2012

@jsoverson makes sense. do you want to refactor it or should I do it? I think we should add the "4" as first char on the 3rd group like the wikipedia article says about GUID v4.

@jsoverson

This comment has been minimized.

Show comment
Hide comment
@jsoverson

jsoverson Aug 20, 2012

Contributor

I just pulled in your random hex and changed the spec to account for the 4

Contributor

jsoverson commented Aug 20, 2012

I just pulled in your random hex and changed the spec to account for the 4

@millermedeiros millermedeiros merged commit 51d9a75 into millermedeiros:master Aug 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment