Comparing version 1.1.2 to 1.2.0
module.exports = (function (Base62) { | ||
"use strict"; | ||
var DEFAULT_CHARACTER_SET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
@@ -14,14 +16,48 @@ | ||
Base62.decode = function(base62String){ | ||
var val = 0, base62Chars = base62String.split("").reverse(); | ||
base62Chars.forEach(function(character, index){ | ||
val += Base62.characterSet.indexOf(character) * Math.pow(62, index); | ||
}); | ||
var defaultCharsetDecode = function defaultCharsetDecode (base62String) { | ||
var value = 0, | ||
i = 0, | ||
length = base62String.length, | ||
charValue; | ||
for (; i < length; i++) { | ||
charValue = base62String.charCodeAt(i); | ||
if (charValue < 58) { | ||
charValue = charValue - 48; | ||
} else if (charValue < 91) { | ||
charValue = charValue - 29; | ||
} else { | ||
charValue = charValue - 87; | ||
} | ||
value += charValue * Math.pow(62, length - i - 1); | ||
} | ||
return value; | ||
}; | ||
var customCharsetDecode = function customCharsetDecode (base62String) { | ||
var val = 0, | ||
i = 0, | ||
length = base62String.length, | ||
characterSet = Base62.characterSet; | ||
for (; i < length; i++) { | ||
val += characterSet.indexOf(base62String[i]) * Math.pow(62, length - i - 1); | ||
} | ||
return val; | ||
}; | ||
var decodeImplementation = null; | ||
Base62.decode = function(base62String){ | ||
return decodeImplementation(base62String); | ||
}; | ||
Base62.setCharacterSet = function(chars) { | ||
var arrayOfChars = chars.split(""), uniqueCharacters = []; | ||
if(arrayOfChars.length != 62) throw Error("You must supply 62 characters"); | ||
if(arrayOfChars.length !== 62) throw Error("You must supply 62 characters"); | ||
@@ -32,5 +68,7 @@ arrayOfChars.forEach(function(char){ | ||
if(uniqueCharacters.length != 62) throw Error("You must use unique characters."); | ||
if(uniqueCharacters.length !== 62) throw Error("You must use unique characters."); | ||
Base62.characterSet = arrayOfChars; | ||
decodeImplementation = chars === DEFAULT_CHARACTER_SET ? defaultCharsetDecode : customCharsetDecode; | ||
}; | ||
@@ -37,0 +75,0 @@ |
@@ -11,3 +11,3 @@ { | ||
"license": "MIT", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"repository": { | ||
@@ -26,7 +26,8 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"benchmark": "node benchmark/benchmarks.js" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~3.1.0" | ||
"mocha": "~3.4.1" | ||
} | ||
} |
# [Base62.js](http://libraries.io/npm/base62) | ||
[![build status](https://secure.travis-ci.org/andrew/base62.js.svg)](http://travis-ci.org/andrew/base62.js) | ||
[![npm version](https://badge.fury.io/js/base62.svg)](http://badge.fury.io/js/base62) | ||
[![Dependency Status](https://david-dm.org/andrew/base62.js.svg?theme=shields.io)](https://david-dm.org/andrew/base62.js) | ||
[![devDependency Status](https://david-dm.org/andrew/base62.js/dev-status.svg?theme=shields.io)](https://david-dm.org/andrew/base62.js#info=devDependencies) | ||
@@ -10,2 +9,13 @@ [![Gitter chat](http://img.shields.io/badge/gitter-andrew/base62.js-brightgreen.svg)](https://gitter.im/andrew/base62.js) | ||
## What is Base62 encoding? | ||
From [wikipedia](https://de.wikipedia.org/wiki/Base62): | ||
> Base62 is a priority system to the base 62, which for encoding large numbers using ASCII is character. The digits 0-9 (value 0-9), uppercase letters A-Z (value 10-35) and lowercase letters a-z (value 36-61) are used. | ||
> Due to the high number of base formed shorter strings than with the decimal or hexadecimal system , which mainly offers two advantages: | ||
> - They can be entered by a human being faster and with a smaller risk of error. In this case, a font should be selected in which characters that can be confused, such as small L and large i, or zero, and large O, are distinguishable. | ||
> - Length restrictions, eg when a number is to be used as part of an identifier or file name, can be bypassed. However, it should be noted that the processing system is case-sensitive. | ||
## Install | ||
@@ -16,3 +26,8 @@ | ||
``` | ||
or for yarn: | ||
```bash | ||
yarn add base62 | ||
``` | ||
## Usage | ||
@@ -19,0 +34,0 @@ |
@@ -6,2 +6,3 @@ var assert = require('assert'); | ||
it("should encode a number to a Base62 string", function() { | ||
assert.equal(Base62.encode(0), '0'); | ||
assert.equal(Base62.encode(999), 'g7'); | ||
@@ -18,4 +19,7 @@ assert.equal(Base62.encode(65), '13'); | ||
it("should decode a number from a Base62 string", function() { | ||
assert.equal(Base62.decode('0'), 0); | ||
assert.equal(Base62.decode('g7'), 999); | ||
assert.equal(Base62.decode('13'), 65); | ||
//zero padded strings | ||
assert.equal(Base62.decode('0013'), 65); | ||
//test big numbers | ||
@@ -76,2 +80,2 @@ assert.equal(Base62.decode("2Q3rKTOF"), 10000000000001); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25889
14
161
69