Skip to content

Commit

Permalink
refactor(codec): make CodecPredicates optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Dec 2, 2024
1 parent bb25299 commit 21488db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ export interface CodecPredicates {
/**
* Used to check if bucket may end without boundary.
*/
isHead: boolean;
isHead?: boolean;

/**
* Used to check if bucket may be empty.
*/
isRoot: boolean;
isRoot?: boolean;

/**
* Used to check if entries fall inside of range.
Expand Down Expand Up @@ -221,17 +221,17 @@ export function encodeBucket(
average: number,
level: number,
entries: Entry[],
{ isHead, isRoot, range }: CodecPredicates,
{ isHead, isRoot, range }: CodecPredicates = {},
): Uint8Array {
if (!isRoot && entries.length === 0) {
if (isRoot != null && !isRoot && entries.length === 0) {
throw new TypeError("empty non-root bucket.");
}

const isBoundary = createIsBoundary(average, level);

const [encodedEntries, base] = encodeEntries(
entries,
isHead,
isHead ?? true,
isBoundary,
range ?? [
minTuple,
Expand All @@ -256,15 +256,15 @@ export function encodeBucket(
*/
export function decodeBucket(
bytes: Uint8Array,
{ isHead, isRoot, range, expectedPrefix }: CodecPredicates,
{ isHead, isRoot, range, expectedPrefix }: CodecPredicates = {},
): Bucket {
const decoded = decode(bytes);

if (!isValidEncodedBucket(decoded)) {
throw new TypeError("invalid bucket.");
}

if (!isRoot && decoded.entries.length === 0) {
if (isRoot != null && !isRoot && decoded.entries.length === 0) {
throw new TypeError("empty non-root bucket.");
}

Expand All @@ -282,7 +282,7 @@ export function decodeBucket(
const entries = decodeEntries(
decoded.entries,
decoded.base,
isHead,
isHead ?? true,
isBoundary,
range,
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const createBucket = (
average: number,
level: number,
entries: Entry[],
predicates: CodecPredicates,
predicates?: CodecPredicates,
): Bucket => {
const bytes = encodeBucket(average, level, entries, predicates);
return new DefaultBucket(average, level, entries, bytes, sha256(bytes));
Expand Down

0 comments on commit 21488db

Please sign in to comment.