This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
constants.js
68 lines (61 loc) · 2.69 KB
/
constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict'
const baseX = require('@multiformats/base-x')
const Base = require('./base.js')
const { rfc4648 } = require('./rfc4648')
const { decodeText, encodeText } = require('./util')
/** @typedef {import('./types').CodecFactory} CodecFactory */
/** @typedef {import('./types').Codec} Codec */
/** @typedef {import('./types').BaseName} BaseName */
/** @typedef {import('./types').BaseCode} BaseCode */
/** @type {CodecFactory} */
const identity = () => {
return {
encode: decodeText,
decode: encodeText
}
}
/**
*
* name, code, implementation, alphabet
*
* @type {Array<[BaseName, BaseCode, CodecFactory, string]>}
*/
const constants = [
['identity', '\x00', identity, ''],
['base2', '0', rfc4648(1), '01'],
['base8', '7', rfc4648(3), '01234567'],
['base10', '9', baseX, '0123456789'],
['base16', 'f', rfc4648(4), '0123456789abcdef'],
['base16upper', 'F', rfc4648(4), '0123456789ABCDEF'],
['base32hex', 'v', rfc4648(5), '0123456789abcdefghijklmnopqrstuv'],
['base32hexupper', 'V', rfc4648(5), '0123456789ABCDEFGHIJKLMNOPQRSTUV'],
['base32hexpad', 't', rfc4648(5), '0123456789abcdefghijklmnopqrstuv='],
['base32hexpadupper', 'T', rfc4648(5), '0123456789ABCDEFGHIJKLMNOPQRSTUV='],
['base32', 'b', rfc4648(5), 'abcdefghijklmnopqrstuvwxyz234567'],
['base32upper', 'B', rfc4648(5), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'],
['base32pad', 'c', rfc4648(5), 'abcdefghijklmnopqrstuvwxyz234567='],
['base32padupper', 'C', rfc4648(5), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567='],
['base32z', 'h', rfc4648(5), 'ybndrfg8ejkmcpqxot1uwisza345h769'],
['base36', 'k', baseX, '0123456789abcdefghijklmnopqrstuvwxyz'],
['base36upper', 'K', baseX, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'],
['base58btc', 'z', baseX, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'],
['base58flickr', 'Z', baseX, '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'],
['base64', 'm', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'],
['base64pad', 'M', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='],
['base64url', 'u', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'],
['base64urlpad', 'U', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=']
]
/** @type {Record<BaseName,Base>} */
const names = constants.reduce((prev, tupple) => {
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3])
return prev
}, /** @type {Record<BaseName,Base>} */({}))
/** @type {Record<BaseCode,Base>} */
const codes = constants.reduce((prev, tupple) => {
prev[tupple[1]] = names[tupple[0]]
return prev
}, /** @type {Record<BaseCode,Base>} */({}))
module.exports = {
names,
codes
}