commit b1fd6f53459701fe310c3be0d738d3ee417e6d01
parent 86d0d8e4dc7fe04d321ca18d42c68c6f60005dbf
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 10 Nov 2018 19:07:28 +1300
Move eqPatq, etc. to internal/co.
Also add tests for it.
Diffstat:
3 files changed, 44 insertions(+), 35 deletions(-)
diff --git a/src/index.js b/src/index.js
@@ -2,40 +2,8 @@
const co = require('./internal/co')
const ob = require('./internal/ob')
-/**
- * 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) => {
- const phex = co.patq2hex(p)
- const qhex = co.patq2hex(q)
- return eqModLeadingZeroBytes(phex, qhex)
-}
-
module.exports = Object.assign(
co,
- ob,
- { eqPatq }
+ ob
)
diff --git a/src/internal/co.js b/src/internal/co.js
@@ -328,6 +328,37 @@ const isValidPat = name => {
return leadingTilde && !wrongLength && sylsExist
}
+/**
+ * Remove all leading zero bytes from a sliceable value.
+ * @param {String, Buffer, Array}
+ * @return {String}
+ */
+const removeLeadingZeroBytes = str =>
+ str.slice(0, 2) === '00'
+ ? removeLeadingZeroBytes(str.slice(2))
+ : str
+
+/**
+ * Equality comparison, modulo leading zero bytes.
+ * @param {String, Buffer, Array}
+ * @param {String, Buffer, Array}
+ * @return {Bool}
+ */
+const eqModLeadingZeroBytes = (s, t) =>
+ lodash.isEqual(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) => {
+ const phex = patq2hex(p)
+ const qhex = patq2hex(q)
+ return eqModLeadingZeroBytes(phex, qhex)
+}
+
module.exports = {
patp,
patp2hex,
@@ -338,5 +369,6 @@ module.exports = {
patq2dec,
hex2patq,
clan,
- sein
+ sein,
+ eqPatq
}
diff --git a/test/co.test.js b/test/co.test.js
@@ -11,7 +11,8 @@ const {
patq2dec,
hex2patq,
clan,
- sein
+ sein,
+ eqPatq
} = require('../src/internal/co')
const patps = jsc.uint32.smap(
@@ -190,3 +191,11 @@ describe('clan/sein', () => {
})
+describe('eqPatq', () => {
+ it('works as expected', () => {
+ expect(eqPatq('~dozzod-dozzod', '~zod')).to.equal(true)
+ expect(eqPatq('~dozzod-mardun', '~mardun')).to.equal(true)
+ expect(eqPatq('~dozzod-mardun', '~mardun-dozzod')).to.equal(false)
+ })
+})
+