urbit-ob

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

commit a4a8dacfc0cd92883f23e29064b82425df2d1102
parent 10abf765b782549d01822dddd7f6e22a82489115
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri,  5 Oct 2018 15:29:20 +1300

Merge pull request #5 from urbit/jt-add-patq

Add @q encoding
Diffstat:
Mpackage.json | 2+-
Msrc/index.js | 23++++++++++++++++++++++-
Mtest/core.test.js | 24++++++++++++++++++++++++
3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/package.json b/package.json @@ -1,6 +1,6 @@ { "name": "ob-js", - "version": "1.2.0", + "version": "1.3.0", "description": "utilities to convert urbit ship names back and forth between @p and @ud", "main": "dist/index.js", "scripts": { diff --git a/src/index.js b/src/index.js @@ -3,7 +3,8 @@ */ const bnjs = require('bn.js') -const { reduce, isUndefined, isString, every, map } = require('lodash') +const { reduce, concat, chunk, isUndefined, + isString, every, map } = require('lodash') const raku = [ 3077398253, @@ -379,6 +380,25 @@ const patp = (n) => { : loop(sxz, zero, '')) } +// bignum patq +const patq = (n) => { + const buff = n.toArrayLike(Buffer) + + const chunked = + buff.length % 2 === 1 && buff.length !== 1 + ? concat([[buff[0]]], chunk(buff.slice(1), 2)) + : chunk(buff, 2) + + const name = byts => + isUndefined(byts[1]) + ? getSuffix(byts[0]) + : getPrefix(byts[0]) + getSuffix(byts[1]) + + return chunked.reduce((acc, elem) => + acc + (acc === '~' ? '' : '-') + name(elem), '~') +} + + // returns the class of a ship from it's name const tierOfpatp = name => { const l = len(patp2arr(name)) @@ -523,6 +543,7 @@ module.exports = { isValidName: isValidName, patp: patp, + patq: patq, sein: sein, _clan: clan, diff --git a/test/core.test.js b/test/core.test.js @@ -131,6 +131,30 @@ test('patp correctly encodes a big hex string as @p', () => { expect(ob.patp(input)).toBe(expected); }); +test('patq correctly encodes 0 as @q', () => { + let input = new bnjs('0'); + let expected = '~zod'; + expect(ob.patq(input)).toBe(expected); +}); + +test('patq correctly encodes 0x102 as @q', () => { + let input = new bnjs('0102', 'hex'); + let expected = '~marbud'; + expect(ob.patq(input)).toBe(expected); +}); + +test('patq correctly encodes 0x10102 as @q', () => { + let input = new bnjs('010102', 'hex'); + let expected = '~nec-marbud'; + expect(ob.patq(input)).toBe(expected); +}); + +test('patq correctly encodes 0x1010101010101010102 as @q', () => { + let input = new bnjs('01010101010101010102', 'hex'); + let expected = '~marnec-marnec-marnec-marnec-marbud'; + expect(ob.patq(input)).toBe(expected); +}); + test('clan works as expected', () => { expect(ob._clan(new bnjs(0))).toBe('czar'); expect(ob._clan(new bnjs(255))).toBe('czar');