Skip to content

Commit

Permalink
v2.0.0 – Refactor for ES6 + proper testing and coverage
Browse files Browse the repository at this point in the history
 * General refactoring (let/const, ===, etc)
 * Replaced prototypical classes with real ES6 classes
 * Removed `binaryToHex`
 * Removed `hexToBinary`
 * Updated BigNum to 0.13, and dev dependencies to latest
 * Added additional unit tests for coverage
 * Added eslint for code quality checks
 * Updated Travis for modern nodejs LTS releases
 * Updated docs
  • Loading branch information
kfitzgerald committed Mar 11, 2019
1 parent 1e9ebf4 commit a80341f
Show file tree
Hide file tree
Showing 16 changed files with 578 additions and 515 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/**
/coverage/**
/.nyc_output/**
21 changes: 21 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"env": {
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "module"
},
"plugins": [
],
"extends": "eslint:recommended",
"globals": {
"require": true,
"module": true,
"describe": true,
"it": true,
"before": true,
"after": true
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
node_modules
node_modules
package-lock.json
.nyc_output
coverage
6 changes: 5 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ test
examples
.idea
*.tgz
.travis.yml
.travis.yml
coverage
.nyc_output
docs
package-lock.json
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "0.10"
- lts/*
- '8'
- '6'
after_script:
- npm install coveralls@2.10.0 && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
27 changes: 27 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(c) Kevin Fitzgerald and contributors
http://kevinfitzgerald.net

----

TL;DR? see: http://www.tldrlegal.com/license/mit-license

The MIT License (MIT)
Copyright (c) 2019 Kevin Fitzgerald

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.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A basic base58 and base62 encoding and decoding system. Can optionally add a pre

Encodes hex-strings, byte arrays, numbers, big numbers, and MongoDB `ObjectId`s to base58 or base62.

[![Build Status](https://travis-ci.org/kfitzgerald/base-id.svg)](https://travis-ci.org/kfitzgerald/base-id)
[![Build Status](https://travis-ci.org/kfitzgerald/base-id.svg)](https://travis-ci.org/kfitzgerald/base-id) [![Coverage Status](https://coveralls.io/repos/github/kfitzgerald/base-id/badge.svg?branch=master)](https://coveralls.io/github/kfitzgerald/base-id?branch=master)

# Installation

Expand All @@ -18,32 +18,31 @@ npm install base-id

```js

var base = require('base-id');
const base = require('base-id');

// Generate a new crypto-random id with an arbitrary prefix
base.base58.generateToken(24, 'account_') // account_ifq8PeVV9J3weEtz5V14cr9H7AuKhndD
base.base58.generateToken(24, 'account_'); // account_ifq8PeVV9J3weEtz5V14cr9H7AuKhndD

// Generate a new crypto-random id with an arbitrary prefix
base.base62.generateToken(24, 'product_') // product_8egyAcmiJhK0pFThcYHYIojG9GIKK7A4
base.base62.generateToken(24, 'product_'); // product_8egyAcmiJhK0pFThcYHYIojG9GIKK7A4



// Encode a hex-string to base58
var hex = "0a372a50deadbeef";
var res = base.base58.encode(hex); // 2i6ye84HA6z;
const hex = "0a372a50deadbeef";
let res = base.base58.encode(hex); // 2i6ye84HA6z;

// Encode a hex-string to base62
res = base.base62.encode(hex); // SnmsvJ1ziv;



// Make a MongoDB ObjectId pretty:
var objId = new ObjectId();
const objId = new ObjectId();
res = base.base58.encodeWithPrefix(objId, 'charge_'); // charge_2d2yysrPLNBLYpWfK

// Change a pretty id back into an ObjectId
var objId2 = new ObjectId(base.base58.decodeWithPrefix('charge_2d2yysrPLNBLYpWfK', 'charge_')); // new ObjectId("55ea16f30c169b651ddf40ea")

new ObjectId(base.base58.decodeWithPrefix('charge_2d2yysrPLNBLYpWfK', 'charge_')); // new ObjectId("55ea16f30c169b651ddf40ea")

```

Expand All @@ -59,11 +58,15 @@ The module exports both base58 and base62 instances with the following members.
* `decodeWithPrefix(ecodedString, prefix)` – Decodes the given base-encoded string to a hex string, stripping the given prefix.
* `generateToken(bytes, prefix)` – Generates a secure random string, base encoded, with an optional prefix
* `generateBytes(count, options)` – Generates secure random bytes
* `options.array` – when true, will return a `Uint8Array` instead of a standard array
* `options.array` – when truthy, will return a `Uint8Array` instead of a standard array
* `bytesToHex(bytes)` – Convert a byte array to a hex string
* `decodeHexToNumeric(hex)` – Decodes a hex string to a BigInt number
* `encodeNumericToHex(dec)` – Encode a number to hex string
* `binaryToHex(string)` – Converts binary string to a hexadecimal string
* `hexToBinary(hex)` – Converts hexadecimal string to a binary string
* `getHexFromObject(mixed)` Gets the hex string from the given thing. Accepts a hex string, number, bignum, byte array, or MongoDB `ObjectId` instance.



## Breaking Changes

### v2.0.0
* Removed `binaryToHex`
* Removed `hexToBinary`
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
"use strict";

var Base58 = require('./lib/base58'),
Base62 = require('./lib/base62'),
const Base58 = require('./lib/base58');
const Base62 = require('./lib/base62');

base58 = new Base58(),
base62 = new Base62();

module.exports = exports = {
base58: base58,
base62: base62
};
exports.base58 = new Base58();
exports.base62 = new Base62();
Loading

0 comments on commit a80341f

Please sign in to comment.