sortable base64 encoding of string, int32 and boolean
npm install firebase64Combines two closure library functions with the d64 module to encode/decode (array of) boolean, 32 bit integer, string to/from a ascii embeddable, lexicographic sortable value that can be used as firebase key.
The generated encoded strings can be used to create a firebase:
- (multi-)field filter index
- (multi-)field sort values
The implementation uses the following character set: ,PYFGCRLAOEUIDHTNSQJKXBMWVZ_pyfgcrlaoeuidhtnsqjkxbmwvz1234567890
Array items are separated by the character: ~
Example:
````
const assert = require('assert');
const {encode, decode} = require('./index');
const sHello = encode('Hello');
assert.equal(sHello, 'sH5KgQ5w');
const sWorld = encode('World');
assert.equal(sWorld, 'sKqxmQ5F');
assert.deepEqual([sWorld, sHello].sort().map(decode), ['Hello', 'World']);
const nSmall = encode(100000);
assert.equal(nSmall, 'n,,55c,');
const nBig = encode(100016);
assert.equal(nBig, 'n,,55g,');
assert.deepEqual([nBig, nSmall].sort().map(decode), [100000, 100016]);
const bTrue = encode(true);
assert.equal(bTrue, 'b1');
assert.equal(decode(bTrue), true);
const bFalse = encode(false);
assert.equal(bFalse, 'b0');
assert.equal(decode(bFalse), false);
function multiFieldSort(low, high) {
const encoded = [encode(high), encode(low)];
assert.deepEqual(encoded.sort().map(decode), [low, high]);
}
multiFieldSort(['a', 0], ['a', 1]);
multiFieldSort(['a', 0], ['b', 0]);
multiFieldSort(['a', 1], ['aa', 0]);
multiFieldSort(['a', 'a'], ['a', 'b']);
multiFieldSort(['a', 'a'], ['b', 'a']);
multiFieldSort(['a', 'b'], ['aa', 'a']);
multiFieldSort(['a', false], ['a', true]);
multiFieldSort(['a', false], ['b', false]);
multiFieldSort(['a', true], ['aa', false]);
Convert (array of) boolean or 32 bit integer or utf8 string to base64 string
Convert encoded base64 string to (array of) boolean or 32 bit integer or utf8 string
Convert string to byte array
Convert array of bytes to string
Convert 32 bit integer number to 4 character byte array
Convert 4 character byte array to 32 bit integer
Convert utf8 string to base64 string
Convert 32 bit integer to base64 string
Convert base64 string to 32 bit integer
Convert boolean or 32 bit integer or utf8 string to base64 string
Convert base64 string to boolean or 32 bit integer or utf8 string