Last active
September 27, 2016 15:00
-
-
Save juanmaguitar/f230f9ff42c0c724254b006692c181ed to your computer and use it in GitHub Desktop.
guidMaker using iterators (infinite)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function guidMaker(){ | |
function generator() { | |
const S4 = () => (((1+Math.random())*0x10000)|0).toString(16).substring(1); | |
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); | |
} | |
const done = false; | |
let generated = 0; | |
return { | |
next: function() { return { value: generator(), done, generated: ++generated } } | |
}; | |
} | |
var guid = guidMaker(); | |
console.log( JSON.stringify(guid.next()) ); | |
console.log( JSON.stringify(guid.next()) ); | |
console.log( JSON.stringify(guid.next()) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment