forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base36.coffee
58 lines (49 loc) · 1.51 KB
/
base36.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Description:
# Base36 encoding and decoding
#
# Dependencies:
# "big-integer": "1.1.5"
#
# Configuration:
# None
#
# Commands:
# hubot base36 e(ncode)|d(ecode) <query> - Base36 encode or decode <query>
#
# Author:
# plytro
module.exports = (robot) ->
robot.hear /base36 e(ncode)?( me)? (.*)/i, (msg) ->
try
msg.send Base36.encode(msg.match[3])
catch e
throw e unless e.message == 'Value passed is not an integer.'
msg.send "Base36 encoding only works with Integer values."
robot.hear /base36 d(ecode)?( me)? (.*)/i, (msg) ->
try
msg.send (String) Base36.decode(msg.match[3])
catch e
throw e unless e.message == 'Value passed is not a valid Base36 string.'
msg.send "Not a valid base36 encoded string."
class Base36Builder
bigInt = require("big-integer");
constructor: ->
@alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
@base = @alphabet.length
encode: (strIn) ->
num = bigInt(strIn)
str = ""
while num.greaterOrEquals(@base)
mod = bigInt(num.mod(@base))
str = @alphabet[mod.toString()] + str
num = num.subtract(mod).divide(@base)
str = @alphabet[num.toString()] + str
decode: (str) ->
num = bigInt("0")
power = bigInt(@base)
for char, index in str.split("").reverse()
if (char_index = @alphabet.indexOf(char)) == -1
throw new Error('Value passed is not a valid Base36 string.')
num = num.plus(power.pow(index).multiply(char_index))
num.toString()
Base36 = new Base36Builder()