urbit-ob

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

commit cfaa11c61336d91f6681e2733c707a79a5a0308b
parent a3af0e584827ce2e2ca1e9d7d9d42496d51426ae
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri,  5 Oct 2018 15:29:04 +1300

Merge branch 'master' into jt-add-patq
Diffstat:
Msrc/index.js | 56++++++++++++++++++++++++++++++++++++++++++--------------
Mtest/core.test.js | 38++++++++++++++++++++++++++++++--------
2 files changed, 72 insertions(+), 22 deletions(-)

diff --git a/src/index.js b/src/index.js @@ -294,27 +294,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) @@ -323,11 +351,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) @@ -522,6 +545,9 @@ module.exports = { patp: patp, patq: patq, + sein: sein, + _clan: clan, + _add2patp: _add2patp, _getsuffix: getSuffix, _muk: muk, @@ -546,5 +572,7 @@ module.exports = { _dec2bin: dec2bin, _syl2bin: syl2bin, + _met: met, + _arr2patp: arr2patp } diff --git a/test/core.test.js b/test/core.test.js @@ -110,47 +110,69 @@ 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('correctly encode 0 as @q', () => { +test('patq correctly encodes 0 as @q', () => { let input = new bnjs('0'); let expected = '~zod'; expect(ob.patq(input)).toBe(expected); }); -test('correctly encode 0x102 as @q', () => { +test('patq correctly encodes 0x102 as @q', () => { let input = new bnjs('0102', 'hex'); let expected = '~marbud'; expect(ob.patq(input)).toBe(expected); }); -test('correctly encode 0x10102 as @q', () => { +test('patq correctly encodes 0x10102 as @q', () => { let input = new bnjs('010102', 'hex'); let expected = '~nec-marbud'; expect(ob.patq(input)).toBe(expected); }); -test('correctly encode 0x1010101010101010102 as @q', () => { +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'); + 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'); +});