urbit-ob

JavaScript utilities for phonemic base wrangling.
Log | Files | Refs | README

commit 796312ec3ec3a5098ea25a0f291711d31c0f97cf
parent a23d08314790f6edbe7f0df9bc64c00a23d4cfd0
Author: Jared Tobin <jared@jtobin.io>
Date:   Mon,  8 Oct 2018 10:01:32 +1300

Add hex2patq and patq2hex.

Plus a sanity check.

Diffstat:
Msrc/index.js | 25++++++++++++++++++++++++-
Mtest/core.test.js | 6++++++
2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/index.js b/src/index.js @@ -4,7 +4,7 @@ const bnjs = require('bn.js') const { reduce, concat, chunk, isUndefined, - isString, every, map } = require('lodash') + isString, every, map, split } = require('lodash') const raku = [ 3077398253, @@ -51,6 +51,12 @@ remlysfynwerrycsugnysnyllyndyndemluxfedsedbecmun\ lyrtesmudnytbyrsenwegfyrmurtelreptegpecnelnevfes\ ` +// split string at the indicated index +const splitAt = (index, str) => [str.slice(0, index), str.slice(index)]; + +// convert a decimal number to a hex string +const dec2hex = dec => dec.toString(16).padStart(2, '0') + // groups suffixes into array of syllables const suffixes = suf.match(/.{1,3}/g) @@ -398,6 +404,21 @@ const patq = (n) => { acc + (acc === '~' ? '' : '-') + name(elem), '~') } +hex2patq = hex => patq(new bnjs(hex, 'hex')) + +patq2hex = str => { + const chunks = split(str.slice(1), '-') + const splat = map(chunks, chunk => { + let syls = splitAt(3, chunk) + let hex = + syls[1] === '' + ? dec2hex(getSuffixIndex(syls[0])) + : dec2hex(getPrefixIndex(syls[0])) + + dec2hex(getSuffixIndex(syls[1])) + return hex + }) + return splat.join('') +} // returns the class of a ship from it's name const tierOfpatp = name => { @@ -544,6 +565,8 @@ module.exports = { patp: patp, patq: patq, + hex2patq: hex2patq, + patq2hex: patq2hex, sein: sein, _clan: clan, diff --git a/test/core.test.js b/test/core.test.js @@ -155,6 +155,12 @@ test('patq correctly encodes 0x1010101010101010102 as @q', () => { expect(ob.patq(input)).toBe(expected); }); +test('hex2patq works', () => { + let hex = '6d7920617765736f6d65207572626974207469636b65742c206920616d20736f206c75636b79'; + let expected = '~tastud-holruc-sidwet-salpel-taswet-holdeg-paddec-davdut-holdut-davwex-balwet-divwen-holdet-holruc-taslun-salpel-holtux-dacwex-baltud'; + expect(ob.hex2patq(hex)).toBe(expected); +}) + test('clan works as expected', () => { expect(ob._clan(new bnjs(0))).toBe('czar'); expect(ob._clan(new bnjs(255))).toBe('czar');