@davekinkead/reloc
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

8FV65 - ND8SB - RELOC - 3ND0S - 0FU3M

Reloc is a small zero dependency library (1.44 kB when minified) for converting auto incrementing integers into deterministic, reversable, hard-to-guess, short IDs.

Installation

Install the node package.

	$ npm i @davekinkead/reloc

Then require it.

	import Reloc from '@davekinkead/reloc'

Usage

Reloc provides two functions - encode and decode.

	const reloc = new Reloc()
	reloc.encode(10027) // => 'rc8ig'
	reloc.decode('rc8ig') // => 10027

You can set a domain so short IDs are unique to you.

	const ex = new Reloc({ domain: 'www.example.com' })
	const an = new Reloc({ domain: 'www.another.com' })

	ex.encode(10027) // => 'diyci'
	an.encode(10027) // => '9cqq3'

Or set the minimum ID length (default is 5). Note that larger lengths are backwards compatible.

	const short = new Reloc({ length: 3 })
	const long = new Reloc({ length: 10 })

	short.encode(1)             // => 'iuk'
	long.encode(1)              // => 'iujafxpbrx'

	long.decode('iuk')          // => 1
	long.decode('iujafxpbrx')   // => 1
	short.decode('iuk')         // => 1
	short.decode('iujafxpbrx')  // => 1

You can even set the alphabet that will be used for encoding (default is base36 lowercase).

	const baseJP = new Reloc({ dictionary: 'あかがただなはぱまやらわきぎじちぢにひびぴみゆりを' })
	baseJP.encode(10027)       // => 'やちわきひ'
	baseJP.decode('やちわきひ')  // => 10027

Copyright 2023 Dave Kinkead. Licensed under MIT.

Package Sidebar

Install

npm i @davekinkead/reloc

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

25.6 kB

Total Files

7

Last publish

Collaborators

  • davekinkead