Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 13, 2019
1 parent 706fd94 commit 620c26c
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 492 deletions.
440 changes: 0 additions & 440 deletions src/base-table.js

This file was deleted.

429 changes: 429 additions & 0 deletions src/base-table.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'

const table = require('./base-table')
const table = require('./base-table.json')

// map for codecConstant -> code
const constants = {}

for (const [k, v] of Object.entries(table)) {
constants[k.toUpperCase().replace(/-/g, '_')] = parseInt(v.toString('hex'), 16)
for (const [name, code] of Object.entries(table)) {
constants[name.toUpperCase().replace(/-/g, '_')] = code
}

module.exports = Object.freeze(constants)
10 changes: 5 additions & 5 deletions src/name-table.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
const baseTable = require('./base-table')

// this creates a map for code as hexString -> codecName
const baseTable = require('./base-table.json')

// map for hexString -> codecName
const nameTable = {}
module.exports = nameTable

for (const encodingName in baseTable) {
const code = baseTable[encodingName]
nameTable[code.toString('hex')] = encodingName
nameTable[code.toString(16)] = encodingName
}

module.exports = Object.freeze(nameTable)
13 changes: 7 additions & 6 deletions src/print.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const table = require('./base-table')
const e = {}
const table = require('./base-table.json')

for (const [k, v] of Object.entries(table)) {
const key = parseInt(v.toString('hex'), 16)
if (typeof e[key] === 'undefined') e[key] = k
// map for code -> print friendly name
const tableByCode = {}

for (const [name, code] of Object.entries(table)) {
if (tableByCode[code] === undefined) tableByCode[code] = name
}

module.exports = Object.freeze(e)
module.exports = Object.freeze(tableByCode)
7 changes: 6 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
numberToBuffer,
bufferToNumber,
varintBufferEncode,
varintBufferDecode
varintBufferDecode,
varintEncode
}

function bufferToNumber (buf) {
Expand All @@ -27,3 +28,7 @@ function varintBufferEncode (input) {
function varintBufferDecode (input) {
return numberToBuffer(varint.decode(input))
}

function varintEncode (num) {
return Buffer.from(varint.encode(num))
}
11 changes: 6 additions & 5 deletions src/varint-table.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'
const baseTable = require('./base-table')
const varintBufferEncode = require('./util').varintBufferEncode

// this creates a map for codecName -> codeVarintBuffer
const baseTable = require('./base-table.json')
const varintEncode = require('./util').varintEncode

// map for codecName -> codeVarintBuffer
const varintTable = {}
module.exports = varintTable

for (const encodingName in baseTable) {
const code = baseTable[encodingName]
varintTable[encodingName] = varintBufferEncode(code)
varintTable[encodingName] = varintEncode(code)
}

module.exports = Object.freeze(varintTable)
45 changes: 13 additions & 32 deletions tools/update-table.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
const bent = require('bent')
const HEADER = `
// THIS FILE IS GENERATED, DO NO EDIT MANUALLY
// For more information see the README.md
'use strict'

const { numberToBuffer } = require('../src/util')
const table = {
`
const FOOTER = `
}
for (const [k, v] of Object.entries(table)) {
table[k] = numberToBuffer(v)
}
module.exports = Object.freeze(table)
`
const bent = require('bent')
const path = require('path')

const get = bent('string')
const url = 'https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv'
const fs = require('fs')

const parse = async function * () {
let str = await get(url)
let lines = str.split('\n')
let header = lines.shift()
for (let line of lines) {
const str = await get(url)
const lines = str.split('\n')
lines.shift()
for (const line of lines) {
if (!line.length) continue
let [name, tag, code, desc] = line.split(',')
const [name, tag, code] = line.split(',')
yield { name: name.trim(), tag: tag.trim(), code: parseInt(code.trim(), 16) }
}
}

const run = async () => {
let str = HEADER
let first = true
for await (let entry of parse()) {
key = entry.name
if (key.includes('-')) key = `'${key}'`
console.log(entry)
if (!first) str += ',\n '
else str += ' '
str += `${key}: ${entry.code}`
first = false
const table = {}

for await (const { name, code } of parse()) {
table[name] = code
}
str += FOOTER
fs.writeFileSync(__dirname + '/../src/base-table.js', str)

fs.writeFileSync(path.join(__dirname, '../src/base-table.json'), JSON.stringify(table, null, 2))
}
run()

0 comments on commit 620c26c

Please sign in to comment.