2.1.1 • Published 6 years ago

number-smusher v2.1.1

Weekly downloads
2
License
-
Repository
github
Last release
6 years ago

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.

2.1.1

6 years ago

2.1.0

6 years ago

2.0.10

6 years ago

2.0.9

6 years ago

2.0.8

7 years ago

2.0.7

8 years ago

2.0.6

9 years ago

2.0.5

9 years ago

2.0.4

10 years ago

2.0.3

10 years ago

2.0.2

10 years ago

2.0.0

10 years ago

1.0.0

12 years ago

0.1.3

12 years ago

0.1.2

14 years ago

0.1.1

14 years ago

0.1.0

14 years ago