urbit-ob

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

commit df40a774b8aafcd94d3705f0fb627cee9d92f38c
parent 2d1518224fcbdd6180c2ae23fcaf65ae13cfde63
Author: Jared Tobin <jared@jtobin.io>
Date:   Thu, 27 Sep 2018 19:48:48 +1200

Add arbitrary-sized integer @p support.

Diffstat:
Msrc/index.js | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mtest/core.test.js | 22++++++++++++++++++++++
2 files changed, 91 insertions(+), 6 deletions(-)

diff --git a/src/index.js b/src/index.js @@ -2,7 +2,6 @@ * Utility methods */ -// var bn = require('bignum') const bnjs = require('bn.js') const { reduce, isUndefined, isString, every, map } = require('lodash') @@ -123,16 +122,17 @@ const arr2patp = a => reduce(a, (acc, syl, i) => isEven(i) const feen = pyn => { - const f = 4294967295 if (pyn >= 0x10000 && pyn <= 0xFFFFFFFF) { const tmp = fice(pyn - 0x10000) + 0x10000 return tmp } if (pyn >= 0x100000000 && pyn <= 0xffffffffffffffff) { - const pynBn = new bnjs(pyn) - const lo = pynBn.and(f) - const hi = pynBn.and('18446744069414584000') - return hi.or(feen(lo)).toNumber() + const f = new bnjs('4294967295') + const g = new bnjs('18446744069414584000') + const lo = pyn.and(f) + const hi = pyn.and(g) + let next = new bnjs(feen(lo)) + return hi.or(next) } return pyn } @@ -293,6 +293,67 @@ const murmur3 = (data, seed) => { * */ +const bex = (n) => { + const two = new bnjs(2) + return 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) + 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 lsh = (a, b, c) => { + const sub = bex(a).mul(b) + return bex(sub).mul(c) +} + +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) + + let loop = (tsxz, timp, trep) => { + let log = end(four, one, tsxz) + let pre = getPrefix(rsh(three, one, log)) + let suf = getSuffix(end(three, one, log)) + let etc = + (timp.mod(four)).eq(zero) + ? timp.eq(zero) + ? '' + : '--' + : '-' + + let res = pre + suf + etc + trep + + return timp.eq(dyy) + ? trep + : loop(rsh(four, one, tsxz), timp.add(one), res) + } + + let dyx = met(three, sxz) + + return '~' + + (dyx.lte(one) + ? getSuffix(sxz) + : loop(sxz, zero, '')) +} // returns the class of a ship from it's name @@ -438,6 +499,8 @@ module.exports = { toPlanetName: toPlanetName, isValidName: isValidName, + patp: patp, + _add2patp: _add2patp, _getsuffix: getSuffix, _muk: muk, diff --git a/test/core.test.js b/test/core.test.js @@ -1,3 +1,4 @@ +const bnjs = require('bn.js'); const ob = require('../src/index.js'); @@ -108,3 +109,24 @@ test('identifies binzod as a star from address', () => { test('identifies poldec-tonteg as a planet from address', () => { expect(ob.tierOfadd(9896704)).toBe('planet'); }); + +test('correctly encodes 0 as @p', () => { + let expected = '~zod'; + expect(ob.patp('0')).toBe(expected); +}); + +test('correctly encodes 4294967295 as @p', () => { + let expected = '~dostec-risfen'; + expect(ob.patp('4294967295')).toBe(expected); +}); + +test('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', () => { + 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); +});