@typeberry/trie
Advanced tools
Comparing version 0.0.1-2cf3b53 to 0.0.1-386317e
68
index.js
@@ -35,11 +35,40 @@ 'use strict'; | ||
} | ||
/** Decode contained bytes as string. */ | ||
asText() { | ||
const decoder = new TextDecoder(); | ||
return decoder.decode(this.buffer); | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other) { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.buffer[i] !== other.buffer[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v) { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.fromBlob(encoder.encode(v)); | ||
return BytesBlob.from(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
static fromBlob(v) { | ||
static from(v) { | ||
return new BytesBlob(v); | ||
} | ||
/** Create a new [`BytesBlob`] by concatenating data from multiple `Uint8Array`s. */ | ||
static copyFromBlobs(v, ...rest) { | ||
const totalLength = v.length + rest.reduce((a, v) => a + v.length, 0); | ||
const buffer = new Uint8Array(totalLength); | ||
buffer.set(v, 0); | ||
let offset = v.length; | ||
for (const r of rest) { | ||
buffer.set(r, offset); | ||
offset += r.length; | ||
} | ||
return new BytesBlob(buffer); | ||
} | ||
/** Create a new [`BytesBlob`] from an array of bytes. */ | ||
@@ -76,24 +105,12 @@ static fromNumbers(v) { | ||
*/ | ||
class Bytes { | ||
class Bytes extends BytesBlob { | ||
constructor(raw, len) { | ||
super(raw); | ||
check(raw.byteLength === len, `Given buffer has incorrect size ${raw.byteLength} vs expected ${len}`); | ||
this.raw = raw; | ||
this.length = len; | ||
} | ||
/** Return hex encoding of the sequence. */ | ||
toString() { | ||
return bytesToHexString(this.raw); | ||
/** Raw bytes array. */ | ||
get raw() { | ||
return this.buffer; | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other) { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.raw[i] !== other.raw[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create new [`Bytes<X>`] given a backing buffer and it's length. */ | ||
@@ -268,5 +285,6 @@ static fromBlob(v, len) { | ||
class TrieNode { | ||
constructor() { | ||
/** Exactly 512 bits / 64 bytes */ | ||
this.data = new Uint8Array(64); | ||
constructor( | ||
/** Exactly 512 bits / 64 bytes */ | ||
data = new Uint8Array(64)) { | ||
this.data = data; | ||
} | ||
@@ -392,3 +410,3 @@ /** Returns the type of the node */ | ||
const len = this.getValueLength(); | ||
return BytesBlob.fromBlob(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.from(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
} | ||
@@ -414,2 +432,6 @@ /** | ||
} | ||
/** Return number of items in the dictionary. */ | ||
get size() { | ||
return this.map.size; | ||
} | ||
/** Return true if the key is present in the dictionary. */ | ||
@@ -416,0 +438,0 @@ has(key) { |
{ | ||
"name": "@typeberry/trie", | ||
"version": "0.0.1-2cf3b53", | ||
"version": "0.0.1-386317e", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Fluffy Labs", |
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
43944
1244
3