Skip to content

Commit

Permalink
tidy up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
alanclarke committed Dec 8, 2015
1 parent 86f1ae8 commit 588bcb4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,2 +1,2 @@
/node_modules
.DS_Store
node_modules
.DS_Store
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

20 changes: 11 additions & 9 deletions README.md
Expand Up @@ -13,27 +13,29 @@ some bad uses of this utility include:
- number obfuscation


## Getting Started
Install the module with: `npm install int-encoder`
## usage
```
npm install int-encoder
```

```javascript
var encoder = require('int-encoder');
var encoder = require('int-encoder')

encoder.encode(12345678); // "ZXP0"
encoder.decode('ZXP0'); // 12345678
encoder.encode(12345678) // "ZXP0"
encoder.decode('ZXP0') // 12345678

//invoke custom alphabet option
encoder.alphabet('0123456789abcdef') // hex alphabet

encoder.encode(255); // "ff"
encoder.decode('ff'); // 255
encoder.encode(255) // "ff"
encoder.decode('ff') // 255

//check what alphabet is being used
encoder.alphabet() // 0123456789abcdef

//convert big hex number using optional base argument
encoder.encode('e6c6b53d3c8160b22dad35a0f705ec09', 16); // 'hbDcW9aE89tzLYjDgyzajJ'
encoder.decode('hbDcW9aE89tzLYjDgyzajJ', 16); // 'e6c6b53d3c8160b22dad35a0f705ec09'
encoder.encode('e6c6b53d3c8160b22dad35a0f705ec09', 16) // 'hbDcW9aE89tzLYjDgyzajJ'
encoder.decode('hbDcW9aE89tzLYjDgyzajJ', 16) // 'e6c6b53d3c8160b22dad35a0f705ec09'

```
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
15 changes: 6 additions & 9 deletions lib/int-encoder.js
@@ -1,8 +1,8 @@
/*
* int-encoder
* https://github.com/alanclarke/int-encoder
* 'hex': '0123456789abcdef',
* 'reset': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
* hex: 0123456789abcdef
* base64: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_
*/

var bignum = require('bignum')
Expand All @@ -22,11 +22,12 @@ function encode (value, base) {
}

function decode (str, base) {
var i, index
var result = bignum(0)
each(str, function (val, i) {
var index = bignum(alphabet.indexOf(val))
for (i = 0; i < str.length; i++) {
index = bignum(alphabet.indexOf(str[i]))
result = result.add(index.mul(bignum(alphabet.length).pow(str.length - 1 - i)))
})
}
return result.toString(base)
}

Expand All @@ -35,10 +36,6 @@ function alphabetr (val) {
return alphabet
}

function each (arr, fn) {
for (var i = 0; i < arr.length; i++) fn(arr[i], i)
}

module.exports = {
encode: encode,
decode: decode,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"node": ">= 0.6.0"
},
"scripts": {
"test": "node_modules/.bin/standard && node_modules/.bin/nodeunit test/int-encoder_test.js"
"test": "node_modules/.bin/standard && node_modules/.bin/nodeunit test/test-int-encoder.js"
},
"dependencies": {
"bignum": "^0.11.0"
Expand Down
4 changes: 2 additions & 2 deletions test/int-encoder_test.js → test/test-int-encoder.js
@@ -1,6 +1,6 @@
var intEncoder = require('../lib/int-encoder.js')
var intEncoder = require('../lib/int-encoder')
intEncoder.alphabet('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
exports['encode'] = {
exports.encode = {
encoder: function (test) {
var hex = 'e6c6b53d3c8160b22dad35a0f705ec09'
var values = [0, 1, 2, 3, 62, 100, 1000, 354324523453245]
Expand Down

0 comments on commit 588bcb4

Please sign in to comment.