Skip to content

Commit 1e607c8

Browse files
author
brizental
committed
Fix bug on isBoolean function, attend to review comments
1 parent 115f223 commit 1e607c8

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/metrics/payload.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
import { BooleanMetricPayload, isBooleanMetricPayload } from "metrics/boolean";
66
import { isString } from "utils";
77

8+
/**
9+
* Validates that a given value is the correct type of payload for a metric of a given type.
10+
*
11+
* @param type The type of the metric to validate
12+
* @param v The value to verify
13+
*
14+
* @returns Whether or not `v` is of the correct type.
15+
*/
816
export function isMetricPayload(type: string, v: unknown): v is MetricPayload {
917
switch (type) {
1018
case "boolean":

src/storage/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { isString, isUndefined, isObject, isBoolean, isNumber } from "utils";
5+
import { isString, isUndefined, isObject, isBoolean } from "utils";
66

77
/**
88
* The storage index in the ordered list of keys to navigate on the store
@@ -45,7 +45,7 @@ export function isStorageValue(v: unknown): v is StorageValue {
4545
if (isUndefined(v) || isString(v) || isBoolean(v)) {
4646
return true;
4747
}
48-
48+
4949
if (isObject(v)) {
5050
if (Object.keys(v).length === 0) {
5151
return true;

src/storage/persistent/webext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { Store, StorageIndex, StorageValue, StorageObject, isStorageValue } from "storage";
66
import { updateNestedObject, getValueFromNestedObject, deleteKeyFromNestedObject } from "storage/utils";
7-
import { isString, isUndefined } from "utils";
7+
import { isObject } from "utils";
88

99
type WebExtStoreQuery = { [x: string]: { [x: string]: null; } | null; };
1010

@@ -75,7 +75,7 @@ class WebExtStore implements Store {
7575
}
7676

7777
if (isStorageValue(response)) {
78-
if (!isUndefined(response) && !isString(response)) {
78+
if (isObject(response)) {
7979
return getValueFromNestedObject(response, [ this.rootKey, ...index ]);
8080
} else {
8181
return response;

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
export function isObject(v: unknown): v is Record<string | number | symbol, unknown> {
6-
return !!(v && typeof v === "object" && v.constructor === Object);
6+
return (typeof v === "object" && v !== null && v.constructor === Object);
77
}
88

99
export function isUndefined(v: unknown): v is undefined {
1010
return typeof v === "undefined";
1111
}
1212

1313
export function isString(v: unknown): v is string {
14-
return !!(v && (typeof v === "string" || (v === "object" && v.constructor === String)));
14+
return (typeof v === "string" || (typeof v === "object" && v !== null && v.constructor === String));
1515
}
1616

1717
export function isBoolean(v: unknown): v is string {
18-
return !!(v && (typeof v === "boolean" || (v === "object" && v.constructor === Boolean)));
18+
return (typeof v === "boolean" || (typeof v === "object" && v !== null && v.constructor === Boolean));
1919
}

tests/storage.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ for (const store in stores) {
7676
let store: Store;
7777
const expected = {
7878
bip: {
79+
bling: false,
7980
bop: {
8081
blip: "something, something!",
8182
blergh: "don't panic!",
@@ -93,6 +94,7 @@ for (const store in stores) {
9394
before(async function () {
9495
!isUndefined(currentStore.before) && await currentStore.before();
9596
store = currentStore.initializeStore();
97+
await store.update(["bip", "bling"], () => false);
9698
await store.update(["bip", "bop", "blip"], () => "something, something!");
9799
await store.update(["bip", "bop", "blergh"], () => "don't panic!");
98100
await store.update(["bip", "bop", "burp"], () => "you are doing great!");
@@ -116,7 +118,7 @@ for (const store in stores) {
116118
const value = await store.get(["bip", "bop", "inexistent"]);
117119
assert.strictEqual(value, undefined);
118120
});
119-
121+
120122
it("Attempting to get an index that contains an object works", async function () {
121123
const value = await store.get(["bip", "bok"]);
122124
assert.deepStrictEqual(value, expected["bip"]["bok"]);
@@ -126,6 +128,11 @@ for (const store in stores) {
126128
const value = await store.get(["bump"]);
127129
assert.deepStrictEqual(value, expected["bump"]);
128130
});
131+
132+
it("Attempting to get an index that contains a boolean works", async function () {
133+
const value = await store.get(["bip", "bling"]);
134+
assert.strictEqual(value, expected["bip"]["bling"]);
135+
});
129136
});
130137

131138
describe("update", function () {

0 commit comments

Comments
 (0)