sub-encoder
Advanced tools
Comparing version 2.1.0 to 2.1.1
10
index.js
@@ -6,7 +6,8 @@ const codecs = require('codecs') | ||
const SEP_BUMPED = b.from([0x1]) | ||
const EMPTY = b.alloc(0) | ||
module.exports = class SubEncoder { | ||
constructor (prefix, encoding) { | ||
constructor (prefix, encoding, parent = null) { | ||
this.userEncoding = codecs(encoding) | ||
this.prefix = typeof prefix === 'string' ? b.from(prefix) : (prefix || null) | ||
this.prefix = prefix != null ? createPrefix(prefix, parent) : null | ||
this.lt = this.prefix && b.concat([this.prefix.subarray(0, this.prefix.byteLength - 1), SEP_BUMPED]) | ||
@@ -53,4 +54,3 @@ } | ||
sub (prefix, encoding) { | ||
const prefixBuf = typeof prefix === 'string' ? b.from(prefix) : prefix | ||
return new SubEncoder(createPrefix(prefixBuf, this.prefix), compat(encoding)) | ||
return new SubEncoder(prefix || EMPTY, compat(encoding), this.prefix) | ||
} | ||
@@ -60,2 +60,4 @@ } | ||
function createPrefix (prefix, parent) { | ||
prefix = typeof prefix === 'string' ? b.from(prefix) : prefix | ||
if (prefix && parent) return b.concat([parent, prefix, SEP]) | ||
@@ -62,0 +64,0 @@ if (prefix) return b.concat([prefix, SEP]) |
{ | ||
"name": "sub-encoder", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Generate sub encodings for key/value stores", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/andrewosh/sub-encoder.git" | ||
"url": "git+https://github.com/holepunchto/sub-encoder.git" | ||
}, | ||
@@ -20,7 +20,7 @@ "keywords": [ | ||
"author": "Andrew Osheroff <andrewosh@gmail.com>", | ||
"license": "MIT", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/andrewosh/sub-encoder/issues" | ||
"url": "https://github.com/holepunchto/sub-encoder/issues" | ||
}, | ||
"homepage": "https://github.com/andrewosh/sub-encoder#readme", | ||
"homepage": "https://github.com/holepunchto/sub-encoder#readme", | ||
"devDependencies": { | ||
@@ -27,0 +27,0 @@ "brittle": "^3.1.1", |
@@ -35,6 +35,10 @@ # sub-encoder | ||
`prefix` can be string or Buffer. If set, acts as an initial sub instead of starting at the Hyperbee's base level. | ||
#### `subEnc = enc.sub(prefix, [encoding])` | ||
Make a new sub. Returns a SubEncoder. | ||
Make a new sub. Returns a SubEncoder, so subs can easily be nested. | ||
`prefix` can be string or Buffer. | ||
#### `buf = enc.encode(key)` | ||
@@ -53,2 +57,2 @@ | ||
### License | ||
MIT | ||
Apache-2.0 |
@@ -143,2 +143,34 @@ const test = require('brittle') | ||
test('supports the empty sub on top of another sub', async t => { | ||
const bee = new Hyperbee(new Hypercore(ram)) | ||
const enc = new SubEncoder() | ||
const sub1 = enc.sub('1', 'utf-8') | ||
const subSubEmpty = sub1.sub() | ||
const subSub1 = sub1.sub('not empty', 'utf-8') | ||
await bee.put('', 'sub1 Entry', { keyEncoding: sub1 }) | ||
await bee.put('', 'subSub1 Entry', { keyEncoding: subSub1 }) | ||
await bee.put(b.alloc(1), 'EmptySubsub entry', { keyEncoding: subSubEmpty }) | ||
const n3 = await collect(bee.createReadStream({ keyEncoding: subSubEmpty })) | ||
t.is(n3.length, 1) | ||
t.alike(n3[0].key, b.alloc(1)) | ||
}) | ||
test('empty str is a valid prefix, causing no overlap', async t => { | ||
const bee = new Hyperbee(new Hypercore(ram)) | ||
const enc = new SubEncoder() | ||
const sub1 = enc.sub('', 'utf-8') | ||
const sub2 = enc.sub('other', 'utf-8') | ||
await bee.put('hey', 'ho', { keyEncoding: sub1 }) | ||
await bee.put('not', 'a part', { keyEncoding: sub2 }) | ||
const res = await collect(bee.createReadStream({ keyEncoding: sub1 })) | ||
t.is(res.length, 1) | ||
t.alike(res[0].key, 'hey') | ||
}) | ||
test('can read out the empty key in subs', async t => { | ||
@@ -192,2 +224,8 @@ const bee = new Hyperbee(new Hypercore(ram)) | ||
test('constructor-specified sub equivalent to calling .sub()', async t => { | ||
const directSub = new SubEncoder('mysub', 'utf-8') | ||
const roundaboutSub = (new SubEncoder()).sub('mysub', 'utf-8') | ||
t.alike(directSub, roundaboutSub) | ||
}) | ||
async function collect (ite) { | ||
@@ -194,0 +232,0 @@ const res = [] |
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
23453
7
242
57