Combines two 32bit integers into a single 64bit integer string. Can also reverse it back out again
- Then you can extract the original two 32bit numbers back out again
- NOTE: I use this to store Top 10 lists in a sorted set in redis as a single score
npm install number-smusher
var number_smusher = require("number-smusher"); // number_smusher.smush(left, right) // number_smusher.smush(left, right, inverseRight) // number_smusher.unsmush_left(smushed) // number_smusher.unsmush_right(smushed) // number_smusher.unsmush_right(smushed, inverseRight)
// Pretend the left number is the 'Fewest Moves to Win' and the right number is a timestamp // In the case of a tie for fewest moves, the smallest timestamp wins (first person to do it) // So we want the smallest left and the smallest right, this is the default. // Notice how output number maintains proper ordering smush(12, 2000) = 51539609552 smush(13, 3000) = 55834577848 smush(13, 3001) = 55834577849 smush(14, 3001) = 60129545145 // Now pretend the left number is 'Highest Score' and the right number is a timestamp // In the case of a tied score, the smallest timestamp wins (first person to achieve that score) // So we want the largest left and the smallest right // We mark the right number as 'reversed' to ensure the numbers coming out are properly sortable smush(14, 3001) = 64424506438 smush(13, 3000) = 60129539143 smush(13, 3001) = 60129539142 smush(12, 2000) = 55834572847
node-number-smusher is provided under the MIT License. See the LICENSE file for more details.