Skip to content

Commit

Permalink
Merge pull request #1 from diasdavid/revamp
Browse files Browse the repository at this point in the history
Revamp
  • Loading branch information
daviddias committed Feb 14, 2016
2 parents eafa032 + 5282d81 commit d111872
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 206 deletions.
4 changes: 0 additions & 4 deletions .jscsrc

This file was deleted.

1 change: 0 additions & 1 deletion .jshintignore

This file was deleted.

29 changes: 0 additions & 29 deletions .jshintrc

This file was deleted.

14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dht-id
webrtc-explorer-peer-id
======

> dht-id generator and operator, designed with circular Id namespaces in mind, used in [`webrtc-explorer`](https://github.com/diasdavid/webrtc-explorer). Currently only supports 48 bits, but I'm happy to make accept PR that make it Id size agnostic.
> Peer Id generator and operator, designed with circular Id namespaces in mind, used in [`webrtc-explorer`](https://github.com/diasdavid/webrtc-explorer). Currently only supports 48 bits, but I'm happy to make accept PR that make it Id size agnostic.
## Project Information

Expand All @@ -21,9 +21,9 @@ David Dias and Luís Veiga. browserCloud.js A federated community cloud served b

# Badgers

[![NPM](https://nodei.co/npm/dht-id.png?downloads=true&stars=true)](https://nodei.co/npm/dht-id/)

[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/diasdavid/dht-id?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)[![Dependency Status](https://david-dm.org/diasdavid/dht-id.svg)](https://david-dm.org/diasdavid/dht-id)[![Build Status](https://travis-ci.org/diasdavid/dht-id.svg)](https://travis-ci.org/diasdavid/dht-id)
[![NPM](https://nodei.co/npm/webrtc-explorer-peer-id.png?downloads=true&stars=true)](https://nodei.co/npm/webrtc-explorer-peer-id/)
[![Dependency Status](https://david-dm.org/diasdavid/webrtc-explorer-peer-id.svg)](https://david-dm.org/diasdavid/webrtc-explorer-peer-id)
[![Build Status](https://travis-ci.org/diasdavid/webrtc-explorer-peer-id.svg)](https://travis-ci.org/diasdavid/webrtc-explorer-peer-id)

# Properties

Expand All @@ -33,7 +33,7 @@ David Dias and Luís Veiga. browserCloud.js A federated community cloud served b

# How to use

```
```JavaScript
var Id = require('dht-id);
var idA = new Id(); // generates a new random Id with 48 bits length
Expand All @@ -46,7 +46,5 @@ idA.toDec(); // returns the dec value of the Id in a number
idA.next(); // basically this id + 1, useful to send to Sucessor
Id.spin(); // returns an id larger than the max possible in Hex, useful when the message has to spin the ring
Id.hash(content); // convinient way to find the Id of a content and guarantee that it has our ideal id length
```
34 changes: 12 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
{
"name": "dht-id",
"name": "webrtc-explorer-peer-id",
"version": "1.0.3",
"description": "DHT circular IDs generator and methods",
"description": "webrtc-explorer utility for peer Id generation",
"main": "src/index.js",
"scripts": {
"requiresafe": "./node_modules/.bin/requiresafe check",
"codestyle": "./node_modules/.bin/jscs src/index.js tests/*.js",
"lint": "./node_modules/.bin/jshint .",
"test": "node ./node_modules/.bin/lab -r tap tests/*-test.js | ./node_modules/.bin/tap-spec",
"test-cov": "node ./node_modules/.bin/lab -t 100 tests/*-test.js",
"test-cov-html": "node ./node_modules/.bin/lab -r html -o coverage.html tests/*-test.js"
"lint": "standard",
"test": "mocha tests/test-*.js"
},
"precommit": [
"codestyle",
"lint",
"test",
"test-cov",
"requiresafe"
"test"
],
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/dht-id.git"
"url": "https://github.com/diasdavid/webrtc-explorer-peer-id.git"
},
"keywords": [
"uuid",
Expand All @@ -33,19 +26,16 @@
"author": "David Dias <daviddias.p@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/dht-id/issues"
"url": "https://github.com/diasdavid/webrtc-explorer-peer-id/issues"
},
"homepage": "https://github.com/diasdavid/dht-id",
"homepage": "https://github.com/diasdavid/webrtc-explorer-peer-id",
"dependencies": {
"git-sha1": "^0.1.2"
},
"devDependencies": {
"code": "^1.2.1",
"jscs": "^1.10.0",
"jshint": "^2.5.11",
"lab": "^5.0.2",
"precommit-hook": "^1.0.7",
"requiresafe": "^1.2.2",
"tap-spec": "^1.0.1"
"chai": "^3.5.0",
"mocha": "^2.4.5",
"pre-commit": "^1.1.2",
"standard": "^6.0.5"
}
}
100 changes: 46 additions & 54 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,51 @@
var sha1 = require('git-sha1');

exports = module.exports = Id;

var maxHex = 'ffffffffffff';
var maxDec = parseInt(maxHex, 16);

function Id(_id) {
var dec;
var hex;

if (typeof _id === 'number') {
dec = _id;
var tmp = ('00000000000000' + _id.toString(16));
hex = tmp.substring(tmp.length - 12, tmp.length);
var sha1 = require('git-sha1')

exports = module.exports = Id

var maxHex = 'ffffffffffff'
// var maxDec = parseInt(maxHex, 16)

function Id (_id) {
var dec
var hex

if (typeof _id === 'number') {
dec = _id
var tmp = ('00000000000000' + _id.toString(16))
hex = tmp.substring(tmp.length - 12, tmp.length)
}
if (typeof _id === 'string') {
dec = parseInt(_id, 16)
hex = _id
}
if (typeof _id === 'undefined') {
hex = sha1((~~(Math.random() * 1e9)).toString(36) + Date.now())
.substring(0, 12)
dec = parseInt(hex, 16)
}

this.toHex = function () {
return hex
}

this.toDec = function () {
return dec
}

this.next = function () {
if (hex === maxHex) {
return '000000000000'
} else {
var a = ('000000000000' + ((dec + 1).toString(16)))
return a.substring(a.length - 12, a.length)
}
if (typeof _id === 'string') {
dec = parseInt(_id, 16);
hex = _id;
}
if (typeof _id === 'undefined') {
hex = sha1((~~(Math.random() * 1e9)).toString(36) + Date.now())
.substring(0, 12);
dec = parseInt(hex, 16);
}

this.toHex = function() {
return hex;
};
}

this.toDec = function() {
return dec;
};

this.next = function() {
if (hex === maxHex) {
return '000000000000';
} else {
var a = ('000000000000' + ((dec + 1).toString(16)));
return a.substring(a.length - 12, a.length);
}
};

return this;
return this
}

//
// bigger Id than available to make the message spin the ring
//
exports.spin = function() {
return (maxDec + 1).toString(16);
};

//
// returns the Id in a hex value, which correspondes to the hash of the content
//
exports.hash = function(content) {
return sha1(content).substring(0, 12);
};
// returns the Id in a hex value, which corresponds to the hash of the content
exports.hash = function (content) {
return sha1(content).substring(0, 12)
}
88 changes: 0 additions & 88 deletions tests/id-test.js

This file was deleted.

Loading

0 comments on commit d111872

Please sign in to comment.