Skip to content

Commit 03c176e

Browse files
committed
Stricter types
1 parent 9335f39 commit 03c176e

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/nested.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const NestedApi = ({ database }: { database: InternalDatabase }) => {
101101
return addOperation({ op: "DEL", key: joinedKey, value: null });
102102
};
103103

104-
const get = async (key: NestedKey): Promise<PossiblyNestedValue> => {
104+
const get = async (key: NestedKey): Promise<PossiblyNestedValue | undefined> => {
105105
const joinedKey = typeof key === "string" ? key : joinKey(key);
106106
const relevantKeyValues: { key: string; value: DagCborEncodable }[] = [];
107107

@@ -131,7 +131,7 @@ export const NestedApi = ({ database }: { database: InternalDatabase }) => {
131131
): Promise<string[]> => {
132132
let flattenedEntries: { key: string; value: DagCborEncodable }[];
133133
if (typeof keyOrObject === "string") {
134-
flattenedEntries = flatten(object).map((entry) => ({
134+
flattenedEntries = flatten(object!).map((entry) => ({
135135
key: `${keyOrObject}/${entry.key}`,
136136
value: entry.value,
137137
}));
@@ -159,15 +159,18 @@ export const NestedApi = ({ database }: { database: InternalDatabase }) => {
159159
};
160160
for await (const entry of log.traverse()) {
161161
const { op, key, value } = entry.payload;
162+
if (typeof key !== "string") continue;
163+
162164
if (op === "PUT" && !keyExists(key)) {
165+
if (value === undefined) continue;
163166
keys[key] = true;
164167
count++;
165168
const hash = entry.hash;
166169
yield { key, value, hash };
167170
} else if (op === "DEL" && !keyExists(key)) {
168171
keys[key] = true;
169172
}
170-
if (count >= amount) {
173+
if (amount !== undefined && count >= amount) {
171174
break;
172175
}
173176
}

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const flatten = (
3131
x: PossiblyNestedValue,
3232
rootKey: string[],
3333
): void => {
34-
if (typeof x === "object" && !Array.isArray(x)) {
34+
if (typeof x === "object" && !Array.isArray(x) && x !== null) {
3535
for (const [key, value] of Object.entries(x)) {
3636
recursiveFlatten(value, [...rootKey, key]);
3737
}
@@ -55,7 +55,8 @@ export const toNested = (
5555
if (typeof root[c] !== "object" || Array.isArray(root[c])) root[c] = {};
5656
root = root[c] as NestedValue;
5757
}
58-
root[keyComponents.pop()] = value;
58+
const finalKeyComponent = keyComponents.pop()
59+
if (finalKeyComponent) root[finalKeyComponent] = value;
5960
}
6061
return nested;
6162
};

tsconfig.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
"moduleResolution": "node",
1313
"noFallthroughCasesInSwitch": true,
1414
"noImplicitReturns": true,
15-
"noUnusedLocals": false,
16-
"noUnusedParameters": false,
15+
"noUnusedLocals": true,
16+
"noUnusedParameters": true,
1717
"paths": {
1818
"@/*": ["src/*"]
1919
},
2020
"outDir": "./dist",
2121
"sourceMap": true,
2222
"strict": true,
23-
"strictPropertyInitialization": false,
24-
"strictNullChecks": false,
23+
"strictPropertyInitialization": true,
24+
"strictNullChecks": true,
2525
"target": "esnext",
2626
"useUnknownInCatchVariables": false,
2727

0 commit comments

Comments
 (0)