simpleflake
Advanced tools
Comparing version 0.1.1 to 0.1.2
module.exports = require('./lib/simpleflake'); |
var assert = require('assert'); | ||
var base58 = require('base58-native'); | ||
var bignum = require('bignum'); | ||
var crypto = require('crypto'); | ||
try { | ||
var base58 = require('base58-native'); | ||
} catch (e) { | ||
base58 = null | ||
} | ||
var opt = { | ||
@@ -32,3 +37,3 @@ epoch: Date.UTC(2000, 0, 1), | ||
// Augment returned buffer with base58 encoding option. | ||
// Augment returned buffer with additional encoding option. | ||
buf.toArray = toArray; | ||
@@ -44,4 +49,6 @@ buf.toString = toString; | ||
function toString(enc) { | ||
if (enc == 'base58') { | ||
if (enc == 'base58' && base58) { | ||
return base58.encode(this); | ||
} else if (enc == 'base10') { | ||
return bignum.fromBuffer(this).toString(10); | ||
} else { | ||
@@ -54,4 +61,6 @@ return Buffer.prototype.toString.apply(this, arguments); | ||
if (!Buffer.isBuffer(buf)) { | ||
if (enc == 'base58') { | ||
if (enc == 'base58' && base58) { | ||
buf = base58.decode(buf); | ||
} else if (enc == 'base10') { | ||
buf = bignum(buf).toBuffer() | ||
} else { | ||
@@ -58,0 +67,0 @@ buf = new Buffer(buf, enc); |
{ | ||
"name": "simpleflake", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Distributed id generation for the lazy.", | ||
@@ -17,6 +17,9 @@ | ||
"dependencies": { | ||
"base58-native": "^0.1", | ||
"bignum": "^0.6.2" | ||
}, | ||
"optionalDependencies": { | ||
"base58-native": "^0.1" | ||
}, | ||
"devDependencies": { | ||
@@ -23,0 +26,0 @@ "expectations": "*", |
@@ -12,3 +12,3 @@ node-simpleflake | ||
epoch) followed 23 random bits. Result is a `Buffer` with an added feature | ||
of base58 conversion for producing compact and readable strings. | ||
of base58 and base10 conversions for producing compact and readable strings. | ||
@@ -24,8 +24,2 @@ Custom epoch starts on 2000-01-01T00:00:00.000Z, which should be good until | ||
Test | ||
---- | ||
``` | ||
npm test | ||
``` | ||
Use | ||
@@ -39,2 +33,3 @@ --- | ||
id.toString('base58'); // '9mCpPjW7D5a' | ||
id.toString('base10'); // '3774763974302006457' | ||
``` | ||
@@ -49,4 +44,5 @@ | ||
```javascript | ||
flake.parse(id); // [ 1396671731598, 3570873 ] | ||
flake.parse('9mCpPjW7D5a', 'base58'); // [ 1396671731598, 3570873 ] | ||
flake.parse(id); // [ 1396671731598, 3570873 ] | ||
flake.parse('9mCpPjW7D5a', 'base58'); // [ 1396671731598, 3570873 ] | ||
flake.parse('3774763974302006457', 'base10'); // [ 1396671731598, 3570873 ] | ||
``` | ||
@@ -53,0 +49,0 @@ |
@@ -71,2 +71,7 @@ var flake = require('..'); | ||
it('can generate a base10 encoding', function() { | ||
var id = flake(Date.UTC(2014, 0, 1), 42); | ||
expect(id.toString('base10')).toBe('3706503089356800042'); | ||
}); | ||
it('produces distinct ids on subsequent calls', function() { | ||
@@ -113,2 +118,6 @@ var id1 = flake(); | ||
it('can parse a base10 string', function() { | ||
expect(flake.parse('3706503089356800042', 'base10')).toEqual([1388534400000, 42]); | ||
}); | ||
it('can round-trip', function() { | ||
@@ -115,0 +124,0 @@ expect(flake.parse(flake(1388534400000, 42))).toEqual([1388534400000, 42]); |
Sorry, the diff of this file is not supported yet
12323
197
76
- Removedbase58-native@^0.1