Package Exports
- hashids
- hashids/cjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hashids) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Hashids is small JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user: http://hashids.org/javascript
Getting started
Install Hashids via:
(or just use the code at dist/hashids.js)
Use in ESM-compatible environments (webpack, modern browsers):
import Hashids from 'hashids'
const hashids = new Hashids()
console.log(hashids.encode(1))Use in Node.js:
const Hashids = require('hashids/cjs')
const hashids = new Hashids()
console.log(hashids.encode(1))Use in the browser without ESM (wherever ES5 is supported; 5KB):
<script type="text/javascript" src="hashids.min.js"></script>
<script type="text/javascript">
var hashids = new Hashids();
console.log(hashids.encode(1));
</script>Quick example
const hashids = new Hashids()
const id = hashids.encode(1, 2, 3) // o2fXhV
const numbers = hashids.decode(id) // [1, 2, 3]More options
A few more ways to pass to encode():
const hashids = new Hashids()
console.log(hashids.encode(1, 2, 3)) // o2fXhV
console.log(hashids.encode([1, 2, 3])) // o2fXhV
// strings containing integers are coerced to numbers:
console.log(hashids.encode('1', '2', '3')) // o2fXhV
console.log(hashids.encode(['1', '2', '3'])) // o2fXhV
// BigInt support:
console.log(hashids.encode([1n, 2n, 3n])) // o2fXhV
// Hex notation BigInt:
console.log(hashids.encode([0x1n, 0x2n, 0x3n])) // o2fXhVMake your ids unique:
Pass a "salt" to make your ids unique (e.g. a project name):
var hashids = new Hashids('My Project')
console.log(hashids.encode(1, 2, 3)) // Z4UrtW
var hashids = new Hashids('My Other Project')
console.log(hashids.encode(1, 2, 3)) // gPUasbUse padding to make your ids longer:
Note that ids are only padded to fit at least a certain length. It doesn't mean that your ids will be exactly that length.
const hashids = new Hashids() // no padding
console.log(hashids.encode(1)) // jR
const hashids = new Hashids('', 10) // pad to length 10
console.log(hashids.encode(1)) // VolejRejNmPass a custom alphabet:
const hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz') // all lowercase
console.log(hashids.encode(1, 2, 3)) // mdfphxDefault alphabet is abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.
Since v2.0 you can even use emojis as the alphabet.
Encode hex instead of numbers:
Useful if you want to encode numbers like Mongo's ObjectIds.
Note that there is no limit on how large of a hex number you can pass.
var hashids = new Hashids()
var id = hashids.encodeHex('507f1f77bcf86cd799439011') // y42LW46J9luq3Xq9XMly
var hex = hashids.decodeHex(id) // 507f1f77bcf86cd799439011Please note that this is not the equivalent of:
const hashids = new Hashids()
const id = Hashids.encode(BigInt('0x507f1f77bcf86cd799439011')) // y8qpJL3ZgzJ8lWk4GEV
const hex = Hashids.decode(id)[0].toString(16) // 507f1f77bcf86cd799439011The difference between the two is that the built-in encodeHex will
always result in the same length, even if it contained leading zeros.
For example hashids.encodeHex('00000000') would encode toqExOgK7and decode back to'00000000'` (length information is preserved).
Pitfalls
When decoding, output is always an array of numbers (even if you encode only one number):
const hashids = new Hashids() const id = hashids.encode(1) console.log(hashids.decode(id)) // [1]
Encoding negative numbers is not supported.
If you pass bogus input to
encode(), an empty string will be returned:const hashids = new Hashids() const id = hashids.encode('123a') console.log(id === '') // true
Do not use this library as a security tool and do not encode sensitive data. This is not an encryption library.
Randomness
The primary purpose of Hashids is to obfuscate ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:
No repeating patterns showing there are 3 identical numbers in the id:
const hashids = new Hashids()
console.log(hashids.encode(5, 5, 5)) // A6t1tQSame with incremented numbers:
const hashids = new Hashids()
console.log(hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) // wpfLh9iwsqt0uyCEFjHM
console.log(hashids.encode(1)) // jR
console.log(hashids.encode(2)) // k5
console.log(hashids.encode(3)) // l5
console.log(hashids.encode(4)) // mO
console.log(hashids.encode(5)) // nRCurses! #$%@
This code was written with the intent of placing created ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:
c, f, h, i, s, t, uBigInt
If your environment supports BigInt, you can use the standard API
to encode and decode them the same way as ordinary numbers.
Trying to decode a BigInt-encoded hashid on an unsupported environment will throw an error.
License
MIT License. See the LICENSE file. You can use Hashids in open source projects and commercial products. Don't break the Internet. Kthxbye.
