2.1.1 • Published 5 years ago
number-smusher v2.1.1
node-number-smusher
- Allows you to combine two 32bit javascript numbers into a single 64bit number store in a 'string'
- 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
Installation with NPM
npm install number-smusher
Usage
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)
Example Output
// 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
License
node-number-smusher is provided under the MIT License. See the LICENSE file for more details.