Generate short id's from MongoDB Object ID's for use in url's or other applications.
The module can be used in few modes. You can configure a generation of very short ids based on mongoDB ObjectIds using only timestamp + counter and dropping machine id and process id. In this case a reverse operation is not possible. There is also a way to generate absolute indentical ids that include machine id and process id. This is a reversible conversion.
You can use and configure any set of characters for generation. The most problem of similar modules is a requirement of charset with length of power of 2. This module is free from this limitation. It is very useful for generation short urls based on charset of only digits and letters.
Use NPM:
$ npm install id-shorter
or Git:
$ git clone [email protected]:AHgPuK/id-shorter.git
var ShortId = require('id-shorter');
var mongoDBShortId = ShortId();
var shortId = mongoDBShortId.encode('565ffd0edf3d990540b3134c');
Result: 48yD4jA45
var ShortId = require('id-shorter');
var mongoDBId = ShortId({
isFullId: true
});
var shortId = mongoDBId.encode('565ffd0edf3d990540b3134c');
Result: cdNI0lgCZ0YJ3Z0Z2Bl
var ShortId = require('id-shorter');
var mongoDBId = ShortId({
isFullId: true
});
var shortId = mongoDBId.decode('cdNI0lgCZ0YJ3Z0Z2Bl');
Result: 565ffd0edf3d990540b3134c
var ShortId = require('id-shorter');
var Bin2Hex = ShortId({
base: '01',
encoding: '0123456789abcdef',
isInverse: false,
isFullId: true,
});
var result = Bin2Hex.encode('0000000100100011010001010110011110001001101010111100110111101111');
Result: 0123456789abcdef
MIT (see License)