Last active
June 14, 2018 22:25
-
-
Save dabbott/7c507605b33e5f0b84dd7d278d5f696b to your computer and use it in GitHub Desktop.
String to random number
This file contains 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
const randomNumber = (string) => string | |
.split('') | |
.reduce((acc, char) => (acc * char.charCodeAt(0)) % 0xffffffff, 1); | |
// From https://github.com/airbnb/react-sketchapp/blob/master/src/jsonUtils/models.js | |
const lut = []; | |
for (let i = 0; i < 256; i += 1) { | |
lut[i] = (i < 16 ? '0' : '') + i.toString(16); | |
} | |
// Hack (http://stackoverflow.com/a/21963136) | |
function e7(seedString) { | |
const s1 = randomNumber(seedString); | |
const s2 = randomNumber(seedString.split('').reverse().join('')) | |
const d0 = s1 | 0; | |
const d1 = s2 | 0; | |
const d2 = s1 | 0; | |
const d3 = s2 | 0; | |
return `${lut[d0 & 0xff] + | |
lut[(d0 >> 8) & 0xff] + | |
lut[(d0 >> 16) & 0xff] + | |
lut[(d0 >> 24) & 0xff]}-${lut[d1 & 0xff]}${lut[(d1 >> 8) & 0xff]}-${ | |
lut[((d1 >> 16) & 0x0f) | 0x40] | |
}${lut[(d1 >> 24) & 0xff]}-${lut[(d2 & 0x3f) | 0x80]}${lut[(d2 >> 8) & 0xff]}-${ | |
lut[(d2 >> 16) & 0xff] | |
}${lut[(d2 >> 24) & 0xff]}${lut[d3 & 0xff]}${lut[(d3 >> 8) & 0xff]}${lut[(d3 >> 16) & 0xff]}${ | |
lut[(d3 >> 24) & 0xff] | |
}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment