Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Last active September 27, 2016 15:00
Show Gist options
  • Save juanmaguitar/f230f9ff42c0c724254b006692c181ed to your computer and use it in GitHub Desktop.
Save juanmaguitar/f230f9ff42c0c724254b006692c181ed to your computer and use it in GitHub Desktop.
guidMaker using iterators (infinite)
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