alphacounter
An alphanumeric incrementable counter with endless scale and suitable for URLs. ~40 lines of code, no dependencies.
Can also convert from/to the numerical equivalent.
Why?
alphacounter was created to provide sortable and URL-friendly strings for use in key-value database keys. With
zero-padded numbers you only have 10 usable characters. With alphacounter, you get 62, leading to much smaller keys.
See below how many keys you can get with a given padded length.
This is valuable for auto-incrementing database string ids that could be used in URLs. It may be useful for extremely
large numbers. It can reduce hexidecimal hash size (e.g. make a 32-length md5 hash 22 chars). Also supports zero-padding
for sorting in a DB index. It works great with key-value stores that are key-sorted.
Why another?
There are a few alphanumeric incrementing libraries available, but they are implemented poorly, don't produce
sortable strings, and are larger than they need to be. incstr, alphaplusplus, & alpha-inc all use the wrong alphabet
order so you can't check if one counter is larger than another.
Usage
A single function, inc
, handles creating and incrementing your counter from 0 to beyond Number.MAX_SAFE_INTEGER
.
import { inc } from 'alphacounter';
let counter = inc();
counter = inc(counter);
for (let i = 0; i < 1000; i++) {
counter = inc(counter);
}
Comparing 2 Counters
A helper allows comparing two counters, taking into account their string length so that 10 remains greater than 9,
even though it would sort differently as strings. It also handles an undefined/null value as a new counter, less
than any other counter.
import { inc } from 'alphacounter';
let counter = inc('F8');
console.log(inc.is(counter).gt('A'));
console.log(inc.is(counter).gt('00A'));
console.log(inc.is(counter).eq('000F9'));
console.log(inc.is(undefined).lt('0'));
With Padding
A second parameter, pad
, will zero-pad the counter for use in database indexes. Since databases can't use our gt/lt
helper, you may need to zero-pad the string to the max you think you will need. To determine the upper limit, use
61^n - 1 where n is the string length. These are some ranges:
Str Len | Max Count/Number | |
---|
2 | 3,720 | ~4k |
3 | 226,980 | ~225k |
4 | 13,845,840 | ~14M |
6 | 51,520,374,360 | ~51B |
8 | 191,707,312,997,280 | ~200T |
import { inc } from 'alphacounter';
let counter = inc(null, 3);
counter = inc(counter, 5);
counter = inc(counter, 2);
counter = inc(null, 6);
counter = inc(counter, 6);
counter = inc(counter);
for (let i = 0; i < 999; i++) {
counter = inc(counter);
}
Convert
If you have to convert between a numerical counter and alphacounter you can use these helpers.
import { inc } from 'alphacounter';
inc.from(1)
inc.from(1000)
inc.to('1')
inc.to('G8')
inc.from(1, 4)
inc.from(1000, 4)
inc.to('0001')
inc.to('00G8')
Invert
You may want to store records in your database in reverse order with the newest appearing at the top of your results. To
accomplish this, you may use invert with padded counters.
import { inc } from 'alphacounter';
const id = inc('', 4)
inc.invert(id);
saveDocument(`reverseIndex/${inc.invert('00F9')}`, { id: '00F9', ... });
Decrement
You may want reverse a counter.
import { inc } from 'alphacounter';
const id = inc('5', 4)
inc.dec(id);