[Pkg-javascript-commits] [node-base62] 01/02: Imported Upstream version 1.1.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Sun Mar 27 15:27:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-base62.

commit e1a11e86acffd04543706c56b32b54225f73d5ad
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sun Mar 27 17:27:11 2016 +0200

    Imported Upstream version 1.1.0
---
 .gitignore   | 20 ++++++++++++++++
 .travis.yml  |  4 ++++
 LICENSE      | 20 ++++++++++++++++
 Readme.md    | 53 +++++++++++++++++++++++++++++++++++++++++++
 base62.js    | 38 +++++++++++++++++++++++++++++++
 package.json | 29 ++++++++++++++++++++++++
 test/test.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 238 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5428831
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,20 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+
+build
+.lock-wscript
+
+.idea
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..09d3ef3
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - 0.8
+  - 0.10
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6856bce
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2014 Andrew Nesbitt
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..4d712a2
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,53 @@
+# Base62.js
+[![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)
+[![Gitter chat](http://img.shields.io/badge/gitter-andrew/base62.js-brightgreen.svg)](https://gitter.im/andrew/base62.js)
+
+A javascript Base62 encode/decoder for node.js
+
+## Install
+
+```bash
+npm install base62
+```
+
+## Usage
+
+### Default Character Set Example
+
+```javascript
+Base62 = require('base62')
+Base62.encode(999)  // 'g7'
+Base62.decode('g7') // 999
+```
+
+### Custom Character Set Example
+
+The default character set is `0-9a-zA-Z`. This can be updated to a custom character set. Naturally, it must be 62 characters long.
+
+Instead of the character set `0-9a-zA-Z` you want to use `0-9A-Za-z`, call the `setCharacterSet()` method on the Base62 object passing in the string `"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"`. Note that all characters must be unique.
+
+```javascript
+Base62 = require('base62')
+Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
+Base62.encode(999)  // 'G7'
+Base62.decode('G7') // 999
+```
+
+## Development
+
+Source hosted at [GitHub](http://github.com/andrew/base62.js).
+Report Issues/Feature requests on [GitHub Issues](http://github.com/andrew/base62.js).
+
+### Note on Patches/Pull Requests
+
+ * Fork the project.
+ * Make your feature addition or bug fix.
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
+ * Send me a pull request. Bonus points for topic branches.
+
+## Copyright
+
+Copyright (c) 2014 Andrew Nesbitt. See [LICENSE](https://github.com/andrew/base62.js/blob/master/LICENSE) for details.
diff --git a/base62.js b/base62.js
new file mode 100644
index 0000000..8b0ec2b
--- /dev/null
+++ b/base62.js
@@ -0,0 +1,38 @@
+module.exports = (function (Base62) {
+    var DEFAULT_CHARACTER_SET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+    Base62.encode = function(integer){
+        if (integer === 0) {return '0';}
+        var s = '';
+        while (integer > 0) {
+            s = Base62.characterSet[integer % 62] + s;
+            integer = Math.floor(integer/62);
+        }
+        return s;
+    };
+
+    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);
+        });
+        return val;
+    };
+
+    Base62.setCharacterSet = function(chars) {
+        var arrayOfChars = chars.split(""), uniqueCharacters = [];
+
+        if(arrayOfChars.length != 62) throw Error("You must supply 62 characters");
+
+        arrayOfChars.forEach(function(char){
+            if(!~uniqueCharacters.indexOf(char)) uniqueCharacters.push(char);
+        });
+
+        if(uniqueCharacters.length != 62) throw Error("You must use unique characters.");
+
+        Base62.characterSet = arrayOfChars;
+    };
+
+    Base62.setCharacterSet(DEFAULT_CHARACTER_SET);
+    return Base62;
+}({}));
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..fc6162e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+  "author": "Andrew Nesbitt <andrewnez at gmail.com> (http://nesbitt.io)",
+  "name": "base62",
+  "description": "Javascript Base62 encode/decoder",
+  "keywords": [
+    "base-62"
+  ],
+  "licenses": [
+    {
+      "type": "MIT",
+      "url": "https://github.com/andrew/base62.js/blob/master/LICENSE"
+    }
+  ],
+  "version": "1.1.0",
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/andrew/base62.js.git"
+  },
+  "main": "base62.js",
+  "engines": {
+    "node": "*"
+  },
+  "scripts": {
+    "test": "mocha test"
+  },
+  "devDependencies": {
+    "mocha": "~2.1.0"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..be4e184
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,74 @@
+var assert = require('assert');
+var Base62 = require('../base62');
+
+describe("encode", function() {
+    it("should encode a number to a Base62 string", function() {
+        assert.equal(Base62.encode(999), 'g7');
+        assert.equal(Base62.encode(65), '13');
+        //test big numbers
+        assert.equal(Base62.encode(10000000000001), "2Q3rKTOF");
+        assert.equal(Base62.encode(10000000000002), "2Q3rKTOG");
+
+    });
+});
+
+describe("decode", function() {
+    it("should decode a number from a Base62 string", function() {
+        assert.equal(Base62.decode('g7'), 999);
+        assert.equal(Base62.decode('13'), 65);
+        //test big numbers
+        assert.equal(Base62.decode("2Q3rKTOF"), 10000000000001);
+        assert.equal(Base62.decode("2Q3rKTOH"), 10000000000003);
+    });
+});
+
+describe("setCharacterSequence", function(){
+    it("should update the character sequence", function(){
+        Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
+
+        //Test default character set is not intact
+        assert.notEqual(Base62.encode(999), 'g7');
+
+        //Test new character set test cases
+        var testCases = {
+            "G7": 999,
+            "Lxf7": 5234233,
+            "qx": 3283,
+            "29": 133,
+            "1S": 90,
+            "3k": 232,
+            "4I": 266,
+            "2X": 157,
+            "1E": 76,
+            "1L": 83
+        };
+
+        Object.keys(testCases).forEach(function(base62String){
+            assert.equal(Base62.encode(testCases[base62String]), base62String);
+            assert.equal(Base62.decode(base62String), testCases[base62String]);
+        });
+
+    });
+
+    it("should throw exceptions on invalid strings", function(){
+        var errorCheck = function(err) {
+            if ( (err instanceof Error) && /value/.test(err) ) {
+                return true;
+            }
+        };
+
+        assert.throws(function(){
+            Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy");
+        }, /You must supply 62 characters/);
+
+        assert.throws(function(){
+            Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;");
+        }, /You must supply 62 characters/);
+
+
+        assert.throws(function(){
+            Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzz");
+        }, /You must use unique characters/);
+
+    });
+});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-base62.git



More information about the Pkg-javascript-commits mailing list