urbit-ob

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

commit 14a24542282dd75a37aa1b5a05c456a7b7a9754f
parent c04222df4939d4fc8ae9a36420fc52a54e068423
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed, 10 Oct 2018 16:37:22 +1300

Add eqPatq function.

* Add eqPatq for comparing @q values (i.e. modulo leading zero bytes).
* Minor version bump.
* Track CHANGELOG

Diffstat:
ACHANGELOG | 5+++++
Mpackage.json | 6+++---
Msrc/index.js | 41+++++++++++++++++++++++++++++++++++++++++
Mtest/property.js | 17+++--------------
4 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -0,0 +1,5 @@ +# Changelog + +- 1.4.0 (2018-10-10) + * Add 'eqPatq' function for comparing @q values for equality (i.e., modulo + leading zero bytes). diff --git a/package.json b/package.json @@ -1,10 +1,10 @@ { "name": "ob-js", - "version": "1.3.0", - "description": "utilities to convert urbit ship names back and forth between @p and @ud", + "version": "1.4.0", + "description": "Utilities for Hoon-style atom printing and conversion", "main": "dist/index.js", "scripts": { - "test": "jest test/*.test.js && mocha test/property.js", + "test": "jest --coverage test/*.test.js && mocha test/property.js", "build": "gulp" }, "keywords": [ diff --git a/src/index.js b/src/index.js @@ -431,6 +431,44 @@ patq2hex = str => { return splat.join('') } + +/** + * Remove all leading zero bytes from a hex-encoded string. + * @param {string} str a hex encoded string + * @return {string} + */ +const removeLeadingZeroBytes = str => + str.slice(0, 2) === '00' + ? removeLeadingZeroBytes(str.slice(2)) + : str + + + +/** + * Equality comparison, modulo leading zero bytes. + * @param {string} s a hex-encoded string + * @param {string} t a hex-encoded string + * @return {bool} + */ +const eqModLeadingZeroBytes = (s, t) => + removeLeadingZeroBytes(s) === removeLeadingZeroBytes(t) + + + +/** + * Equality comparison on @q values. + * @param {string} p a @q-encoded string + * @param {string} q a @q-encoded string + * @return {bool} + */ +const eqPatq = (p, q) => { + phex = patq2hex(p) + qhex = patq2hex(q) + return eqModLeadingZeroBytes(phex, qhex) +} + + + // returns the class of a ship from it's name const tierOfpatp = name => { const l = len(patp2arr(name)) @@ -578,6 +616,7 @@ module.exports = { patq: patq, hex2patq: hex2patq, patq2hex: patq2hex, + eqPatq: eqPatq, sein: sein, _clan: clan, @@ -606,6 +645,8 @@ module.exports = { _dec2bin: dec2bin, _syl2bin: syl2bin, + _eqModLeadingZeroBytes: eqModLeadingZeroBytes, + _met: met, _arr2patp: arr2patp diff --git a/test/property.js b/test/property.js @@ -4,14 +4,6 @@ const jsc = require('jsverify') const ob = require('../src') const bn = require('bn.js') -removeLeadingZeroBytes = str => - str.slice(0, 2) === '00' - ? removeLeadingZeroBytes(str.slice(2)) - : str - -eqModLeadingZeroBytes = (s, t) => - removeLeadingZeroBytes(s) === removeLeadingZeroBytes(t) - describe('@q encoding', () => { let hexString = jsc.string.smap( @@ -19,18 +11,15 @@ describe('@q encoding', () => { x => Buffer.from(x, 'hex').toString() ) - let patq = hexString.smap( - hex => ob.patq(new bn(hex, 'hex')), - pq => ob.patq2hex(pq) - ) + let patq = hexString.smap(ob.hex2patq, ob.patq2hex) it('patq2hex and hex2patq are inverses', () => { let iso0 = jsc.forall(hexString, hex => - eqModLeadingZeroBytes(ob.patq2hex(ob.hex2patq(hex)), hex) + ob._eqModLeadingZeroBytes(ob.patq2hex(ob.hex2patq(hex)), hex) ) let iso1 = jsc.forall(patq, str => - ob.hex2patq(ob.patq2hex(str)) === str + ob.eqPatq(ob.hex2patq(ob.patq2hex(str)), str) ) jsc.assert(iso0, { tests: 200 })