sub-encoder
Advanced tools
Comparing version 1.0.6 to 2.0.0
23
index.js
@@ -7,4 +7,2 @@ const isOptions = require('is-options') | ||
const SEP_BUMPED = b.from([0x1]) | ||
const GTE_DEFAULT = b.alloc(0) | ||
const LT_DEFAULT = b.alloc(0) | ||
@@ -38,4 +36,2 @@ module.exports = class SubEncoder { | ||
encode (key) { | ||
if (key === LT_DEFAULT) return b.concat([this.prefix.subarray(0, this.prefix.byteLength - 1), SEP_BUMPED]) | ||
if (key === GTE_DEFAULT) return this.prefix | ||
if (this.userEncoding) key = this.userEncoding.encode(key) | ||
@@ -46,2 +42,14 @@ if (this.prefix) key = b.concat([this.prefix, key]) | ||
encodeRange ({ gt, gte, lte, lt }) { | ||
if (gt) gt = this.encode(gt) | ||
else if (gte) gte = this.encode(gte) | ||
else if (this.prefix) gte = this.prefix | ||
if (lt) lt = this.encode(lt) | ||
else if (lte) lte = this.encode(lte) | ||
else if (this.prefix) lt = b.concat([this.prefix.subarray(0, this.prefix.byteLength - 1), SEP_BUMPED]) | ||
return { gt, gte, lte, lt } | ||
} | ||
decode (key) { | ||
@@ -53,9 +61,2 @@ if (this.prefix) key = key.subarray(this.prefix.byteLength) | ||
range (opts = {}) { | ||
opts = { ...opts, keyEncoding: this } | ||
if (this.prefix && !opts.gt && !opts.gte) opts.gte = GTE_DEFAULT | ||
if (this.prefix && !opts.lt && !opts.lte) opts.lt = LT_DEFAULT | ||
return opts | ||
} | ||
sub (prefix, opts) { | ||
@@ -62,0 +63,0 @@ return new SubEncoder(prefix, { |
{ | ||
"name": "sub-encoder", | ||
"version": "1.0.6", | ||
"version": "2.0.0", | ||
"description": "Generate sub encodings for key/value stores", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"brittle": "^3.1.1", | ||
"hyperbee": "^2.8.0", | ||
"hyperbee": "^2.11.0", | ||
"hypercore": "^10.3.2", | ||
@@ -29,0 +29,0 @@ "random-access-memory": "^6.0.0", |
@@ -66,4 +66,4 @@ const test = require('brittle') | ||
{ | ||
const range = enc.range({ lt: 'sub' }) | ||
const nodes = await collect(bee.createReadStream(range)) | ||
const range = { lt: 'sub' } | ||
const nodes = await collect(bee.createReadStream(range, { keyEncoding: enc })) | ||
t.is(nodes.length, 1) | ||
@@ -74,4 +74,4 @@ t.is(nodes[0].key, 'd1') | ||
{ | ||
const range = subA.range() | ||
const nodes = await collect(bee.createReadStream(range)) | ||
const range = {} | ||
const nodes = await collect(bee.createReadStream(range, { keyEncoding: subA })) | ||
t.is(nodes.length, 1) | ||
@@ -82,4 +82,4 @@ t.is(nodes[0].key, 'a1') | ||
{ | ||
const range = subB.range({ gt: 'b1', lt: 'b3' }) | ||
const nodes = await collect(bee.createReadStream(range)) | ||
const range = { gt: 'b1', lt: 'b3' } | ||
const nodes = await collect(bee.createReadStream(range, { keyEncoding: subB })) | ||
t.is(nodes.length, 1) | ||
@@ -106,4 +106,4 @@ t.is(nodes[0].key, 'b2') | ||
{ | ||
const range = enc.range({ lt: 'sub' }) | ||
const nodes = await collect(bee.createDiffStream(0, range)) | ||
const range = { lt: 'sub' } | ||
const nodes = await collect(bee.createDiffStream(0, range, { keyEncoding: enc })) | ||
t.is(nodes.length, 1) | ||
@@ -114,4 +114,4 @@ t.is(nodes[0].left.key, 'd1') | ||
{ | ||
const range = subA.range() | ||
const nodes = await collect(bee.createDiffStream(0, range)) | ||
const range = {} | ||
const nodes = await collect(bee.createDiffStream(0, range, { keyEncoding: subA })) | ||
t.is(nodes.length, 1) | ||
@@ -122,4 +122,4 @@ t.is(nodes[0].left.key, 'a1') | ||
{ | ||
const range = subB.range({ gt: 'b1', lt: 'b3' }) | ||
const nodes = await collect(bee.createDiffStream(0, range)) | ||
const range = { gt: 'b1', lt: 'b3' } | ||
const nodes = await collect(bee.createDiffStream(0, range, { keyEncoding: subB })) | ||
t.is(nodes.length, 1) | ||
@@ -142,3 +142,3 @@ t.is(nodes[0].left.key, 'b2') | ||
const n3 = await collect(bee.createReadStream(sub3.range())) | ||
const n3 = await collect(bee.createReadStream({ keyEncoding: sub3 })) | ||
@@ -161,5 +161,5 @@ t.is(n3.length, 1) | ||
const n1 = await collect(bee.createReadStream(sub1.range())) | ||
const n2 = await collect(bee.createReadStream(sub2.range())) | ||
const n3 = await collect(bee.createReadStream(sub3.range())) | ||
const n1 = await collect(bee.createReadStream({ keyEncoding: sub1 })) | ||
const n2 = await collect(bee.createReadStream({ keyEncoding: sub2 })) | ||
const n3 = await collect(bee.createReadStream({ keyEncoding: sub3 })) | ||
@@ -166,0 +166,0 @@ t.is(n1[0].key, '') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8856
190