urbit-ob

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

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

Merge pull request #6 from urbit/jt-add-sein

Add's Hoon's "sein" function
Diffstat:
Msrc/index.js | 56++++++++++++++++++++++++++++++++++++++++++--------------
Mtest/core.test.js | 30++++++++++++++++++++++++++----
2 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/src/index.js b/src/index.js @@ -293,27 +293,55 @@ const murmur3 = (data, seed) => { * */ -const bex = (n) => { - const two = new bnjs(2) - return two.pow(n) +const zero = new bnjs(0) +const one = new bnjs(1) +const two = new bnjs(2) +const three = new bnjs(3) +const four = new bnjs(4) +const five = new bnjs(5) + +const clan = (who) => { + const wid = met(three, who) + return wid.lte(one) + ? 'czar' + : wid.eq(two) + ? 'king' + : wid.lte(four) + ? 'duke' + : wid.lte(new bnjs(8)) + ? 'earl' + : 'pawn' } +const sein = (who) => { + const mir = clan(who) + const res = + mir === 'czar' + ? who + : mir === 'king' + ? end(three, one, who) + : mir === 'duke' + ? end(four, one, who) + : mir === 'earl' + ? end(five, one, who) + : zero + return add2patp(res) +} + +const bex = (n) => two.pow(n) + const rsh = (a, b, c) => { const sub = bex(a).mul(b) return c.div(bex(sub)) } -const met = (a, b, c = new bnjs(0)) => { - const zero = new bnjs(0) - const one = new bnjs(1) +const met = (a, b, c = zero) => { return b.eq(zero) ? c : met(a, rsh(a, one, b), c.add(one)) } -const end = (a, b, c) => { - return c.mod(bex(bex(a).mul(b))) -} +const end = (a, b, c) => c.mod(bex(bex(a).mul(b))) const lsh = (a, b, c) => { const sub = bex(a).mul(b) @@ -322,11 +350,6 @@ const lsh = (a, b, c) => { // bignum patp const patp = (n) => { - const zero = new bnjs(0) - const one = new bnjs(1) - const three = new bnjs(3) - const four = new bnjs(4) - let sxz = new bnjs(feen(n)) let dyy = met(four, sxz) @@ -501,6 +524,9 @@ module.exports = { patp: patp, + sein: sein, + _clan: clan, + _add2patp: _add2patp, _getsuffix: getSuffix, _muk: muk, @@ -525,5 +551,7 @@ module.exports = { _dec2bin: dec2bin, _syl2bin: syl2bin, + _met: met, + _arr2patp: arr2patp } diff --git a/test/core.test.js b/test/core.test.js @@ -110,23 +110,45 @@ test('identifies poldec-tonteg as a planet from address', () => { expect(ob.tierOfadd(9896704)).toBe('planet'); }); -test('correctly encodes 0 as @p', () => { +test('patp correctly encodes 0 as @p', () => { let expected = '~zod'; expect(ob.patp('0')).toBe(expected); }); -test('correctly encodes 4294967295 as @p', () => { +test('patp correctly encodes 4294967295 as @p', () => { let expected = '~dostec-risfen'; expect(ob.patp('4294967295')).toBe(expected); }); -test('correctly encodes 328256967394537077627 as @p', () => { +test('patp correctly encodes 328256967394537077627 as @p', () => { let expected = '~dozsyx--halrux-samlep-posmus-ranrux'; expect(ob.patp('328256967394537077627')).toBe(expected); }); -test('correctly encodes a big hex string as @p', () => { +test('patp correctly encodes a big hex string as @p', () => { let input = new bnjs('7468697320697320736f6d6520766572792068696768207175616c69747920656e74726f7079', 16); let expected = '~divmes-davset-holdet--sallun-salpel-taswet-holtex--watmeb-tarlun-picdet-magmes--holter-dacruc-timdet-divtud--holwet-maldut-padpel-sivtud'; expect(ob.patp(input)).toBe(expected); }); + +test('clan works as expected', () => { + expect(ob._clan(new bnjs(0))).toBe('czar'); + expect(ob._clan(new bnjs(255))).toBe('czar'); + expect(ob._clan(new bnjs(256))).toBe('king'); + expect(ob._clan(new bnjs(50000))).toBe('king'); + expect(ob._clan(new bnjs(70000))).toBe('duke'); + expect(ob._clan(new bnjs(2170000))).toBe('duke'); + expect(ob._clan(new bnjs('5232170000'))).toBe('earl'); + expect(ob._clan(new bnjs('525525525125232170000'))).toBe('pawn'); +}) + +test('sein identifies parents correctly', () => { + expect(ob.sein(new bnjs(0))).toBe('zod'); + expect(ob.sein(new bnjs(1))).toBe('nec'); + expect(ob.sein(new bnjs(250))).toBe('rep'); + expect(ob.sein(new bnjs(256))).toBe('zod'); + expect(ob.sein(new bnjs(257))).toBe('nec'); + expect(ob.sein(new bnjs(50000))).toBe('sec'); + expect(ob.sein(new bnjs(15663360))).toBe('marzod'); + expect(ob.sein(new bnjs(15663361))).toBe('marnec'); +});