commit 144d4c8cf6d606357a13e43822f0cf1028435b91
parent c04222df4939d4fc8ae9a36420fc52a54e068423
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 10 Oct 2018 16:48:44 +1300
Merge pull request #10 from urbit/jt-add-patq-eq
add eqPatq function.
Diffstat:
5 files changed, 210 insertions(+), 18 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-lock.json b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "urbit-ob",
- "version": "1.0.0",
+ "version": "1.4.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -1044,6 +1044,12 @@
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
"dev": true
},
+ "assertion-error": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
+ "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==",
+ "dev": true
+ },
"assign-symbols": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
@@ -1640,6 +1646,12 @@
}
}
},
+ "browser-stdout": {
+ "version": "1.3.1",
+ "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
+ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
+ "dev": true
+ },
"browserify-aes": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
@@ -1832,6 +1844,20 @@
"lazy-cache": "^1.0.3"
}
},
+ "chai": {
+ "version": "4.2.0",
+ "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz",
+ "integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==",
+ "dev": true,
+ "requires": {
+ "assertion-error": "^1.1.0",
+ "check-error": "^1.0.2",
+ "deep-eql": "^3.0.1",
+ "get-func-name": "^2.0.0",
+ "pathval": "^1.1.0",
+ "type-detect": "^4.0.5"
+ }
+ },
"chalk": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
@@ -1843,6 +1869,12 @@
"supports-color": "^5.3.0"
}
},
+ "check-error": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
+ "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=",
+ "dev": true
+ },
"chokidar": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-1.7.0.tgz",
@@ -2243,6 +2275,15 @@
"integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=",
"dev": true
},
+ "deep-eql": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
+ "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
+ "dev": true,
+ "requires": {
+ "type-detect": "^4.0.0"
+ }
+ },
"deep-is": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz",
@@ -3799,6 +3840,12 @@
"integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==",
"dev": true
},
+ "get-func-name": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
+ "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=",
+ "dev": true
+ },
"get-stream": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",
@@ -3959,6 +4006,12 @@
"resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz",
"integrity": "sha1-TK+tdrxi8C+gObL5Tpo906ORpyU="
},
+ "growl": {
+ "version": "1.10.5",
+ "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz",
+ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==",
+ "dev": true
+ },
"growly": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz",
@@ -4174,6 +4227,12 @@
"minimalistic-assert": "^1.0.1"
}
},
+ "he": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
+ "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
+ "dev": true
+ },
"hmac-drbg": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz",
@@ -5592,6 +5651,18 @@
"verror": "1.10.0"
}
},
+ "jsverify": {
+ "version": "0.8.3",
+ "resolved": "https://registry.npmjs.org/jsverify/-/jsverify-0.8.3.tgz",
+ "integrity": "sha1-AukXjhkktar5WcAjmvhO1cbQc4w=",
+ "dev": true,
+ "requires": {
+ "lazy-seq": "^1.0.0",
+ "rc4": "~0.1.5",
+ "trampa": "^1.0.0",
+ "typify-parser": "^1.1.0"
+ }
+ },
"just-debounce": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/just-debounce/-/just-debounce-1.0.0.tgz",
@@ -5630,6 +5701,12 @@
"dev": true,
"optional": true
},
+ "lazy-seq": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/lazy-seq/-/lazy-seq-1.0.0.tgz",
+ "integrity": "sha1-iAy4qrJWAmOC4C9T7AiWgqdMW2o=",
+ "dev": true
+ },
"lazystream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/lazystream/-/lazystream-1.0.0.tgz",
@@ -6486,6 +6563,56 @@
"minimist": "0.0.8"
}
},
+ "mocha": {
+ "version": "5.2.0",
+ "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz",
+ "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==",
+ "dev": true,
+ "requires": {
+ "browser-stdout": "1.3.1",
+ "commander": "2.15.1",
+ "debug": "3.1.0",
+ "diff": "3.5.0",
+ "escape-string-regexp": "1.0.5",
+ "glob": "7.1.2",
+ "growl": "1.10.5",
+ "he": "1.1.1",
+ "minimatch": "3.0.4",
+ "mkdirp": "0.5.1",
+ "supports-color": "5.4.0"
+ },
+ "dependencies": {
+ "commander": {
+ "version": "2.15.1",
+ "resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
+ "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
+ "dev": true
+ },
+ "glob": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
+ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
+ "dev": true,
+ "requires": {
+ "fs.realpath": "^1.0.0",
+ "inflight": "^1.0.4",
+ "inherits": "2",
+ "minimatch": "^3.0.4",
+ "once": "^1.3.0",
+ "path-is-absolute": "^1.0.0"
+ }
+ },
+ "supports-color": {
+ "version": "5.4.0",
+ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
+ "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
+ "dev": true,
+ "requires": {
+ "has-flag": "^3.0.0"
+ }
+ }
+ }
+ },
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@@ -7043,6 +7170,12 @@
"pify": "^3.0.0"
}
},
+ "pathval": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
+ "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=",
+ "dev": true
+ },
"pbkdf2": {
"version": "3.0.16",
"resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.0.16.tgz",
@@ -7276,6 +7409,12 @@
"safe-buffer": "^5.1.0"
}
},
+ "rc4": {
+ "version": "0.1.5",
+ "resolved": "https://registry.npmjs.org/rc4/-/rc4-0.1.5.tgz",
+ "integrity": "sha1-CMbgSgFo9utiHCKrbLEVG9n0pk0=",
+ "dev": true
+ },
"react-native-securerandom": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/react-native-securerandom/-/react-native-securerandom-0.1.1.tgz",
@@ -8773,6 +8912,12 @@
"punycode": "^2.1.0"
}
},
+ "trampa": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/trampa/-/trampa-1.0.0.tgz",
+ "integrity": "sha1-Ukc0esM0gH+mwAAERMuRtjmECtU=",
+ "dev": true
+ },
"trim-right": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz",
@@ -8804,6 +8949,12 @@
"prelude-ls": "~1.1.2"
}
},
+ "type-detect": {
+ "version": "4.0.8",
+ "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
+ "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
+ "dev": true
+ },
"typedarray": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
@@ -8816,6 +8967,12 @@
"integrity": "sha1-m7i6DoQfs/TPH+fCRenz+opf6Zw=",
"dev": true
},
+ "typify-parser": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/typify-parser/-/typify-parser-1.1.0.tgz",
+ "integrity": "sha1-rHO/pfJTQ0aOLQ8zRsYRe8A9PJk=",
+ "dev": true
+ },
"uglify-js": {
"version": "2.8.29",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-2.8.29.tgz",
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 })