[p
,index]
=[
newMap()
,0]
;varbase62
=(n)
=>{letstr
='0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ';letlen
=str.
length;letret
='';do{ret
+=str[n
%len]
;n
=~~(n / len)
;}while(n)
;returnret
;};/** * Encodes a URL to a shortened URL. * * @param {string} longUrl * @return {string} */varencode
=function(longUrl)
{letshortUrl
=base62(index
++)
;p.
set(shortUrl
,longUrl)
;returnshortUrl
;};/** * Decodes a shortened URL to its original URL. * * @param {string} shortUrl * @return {string} */vardecode
=function(shortUrl)
{returnp.
get(shortUrl)
;};来源: http://www.cnblogs.com/zichi/p/6648649.html