Skip to content

Commit

Permalink
Json tests (#19)
Browse files Browse the repository at this point in the history
* json: skipNull option

* json: tests

* vitest: exclude index, types from coverage

* utils: test intersection
  • Loading branch information
hbbio committed Feb 5, 2024
1 parent fcab977 commit 56c4185
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, test } from "vitest";

import { jsonStringify } from "./json";

const values: any[] = [
1,
"hello, world",
'"and"',
['"and"'],
{},
[true, false],
{ foo: 1, bar: "charlie" },
[1, 2, 3],
{ foo: [1, 2, 3], bar: { foo: 1, bar: "charlie" } }
// 100n, // @todo returns "100" which is correct but we need to adapt the test
];

test("jsonStringify re-parse", async () => {
for (let v of values) {
expect(JSON.parse(jsonStringify(v))).toEqual(v);
}
});

test("jsonStringify order", () => {
expect(jsonStringify({ a: 1, b: "bar" })).toBe(
jsonStringify({ b: "bar", a: 1 })
);
expect(jsonStringify({ a: { c: 100, z: 1 }, b: "bar" })).toBe(
jsonStringify({ b: "bar", a: { z: 1, c: 100 } })
);
});

test("jsonStringify undefined", () => {
expect(jsonStringify({ a: 1, b: undefined })).toBe(jsonStringify({ a: 1 }));
});
14 changes: 9 additions & 5 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const errorCell = new Error("cell");
* @param obj
* @returns
*/
export const jsonStringify = <T>(obj: T) => {
export const jsonStringify = <T>(
obj: T,
options: { skipNull?: boolean } = {}
) => {
if (obj instanceof Cell) throw errorCell;
let out = "";
const aux = <T>(v: T) => {
Expand All @@ -24,13 +27,17 @@ export const jsonStringify = <T>(obj: T) => {
}
switch (typeof v) {
case "object": {
if (v === null) {
out += "null";
break;
}
out += "{";
let first = true;
// sort objects alphabetically
for (const [k, elt] of Object.entries(v).sort(([k1, _v1], [k2, _v2]) =>
k1 < k2 ? -1 : k1 > k2 ? 1 : 0
)) {
if (elt === undefined || elt === null) continue;
if (elt === undefined || (options.skipNull && elt === null)) continue;
if (!first) out += ",";
out += JSON.stringify(k);
out += ":";
Expand All @@ -46,9 +53,6 @@ export const jsonStringify = <T>(obj: T) => {
case "bigint":
out += `"${v.toString()}"`;
break;
// case "boolean":
// out += v ? "true" : "false";
// break;
default:
out += JSON.stringify(v);
}
Expand Down
27 changes: 27 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it, expect } from "vitest";

import { intersection } from "./utils";

describe("intersection function", () => {
it("returns the correct intersection of two arrays with common elements", () => {
expect(intersection([1, 2, 3], [2, 3, 4])).toEqual([2, 3]);
});

it("returns an empty array when there are no common elements", () => {
expect(intersection([1, 2, 3], [4, 5, 6])).toEqual([]);
});

it("handles identical arrays correctly", () => {
expect(intersection([1, 2, 3], [1, 2, 3])).toEqual([1, 2, 3]);
});

it("returns an empty array when one or both input arrays are empty", () => {
expect(intersection([], [1, 2, 3])).toEqual([]);
expect(intersection([1, 2, 3], [])).toEqual([]);
expect(intersection([], [])).toEqual([]);
});

it("correctly handles duplicates in the input arrays", () => {
expect(intersection([1, 2, 2, 3], [2, 2, 3, 4])).toEqual([2, 2, 3]);
});
});
3 changes: 3 additions & 0 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import { defineConfig } from "vite";
export default defineConfig({
test: {
environment: "happy-dom",
coverage: {
exclude: ["src/index.ts", "src/types.ts"],
},
},
});

0 comments on commit 56c4185

Please sign in to comment.