import { describe, expect, test } from "vitest" import { sanitize } from "." describe("sanitize", () => { test("should handle valid primitive attributes", () => { const input = { key1: "value1", key2: 10, key3: true, } const expected = { key1: "value1", key2: 10, key3: true, } expect(sanitize(input)).toEqual(expected) }) test("should handle valid array attributes", () => { const input = { key1: ["value1", "value2"], key2: [1, 2, 3], key3: [true, false, true], key4: [null, undefined, "a", 1, true], } const expected = { "key1.0": "value1", "key1.1": "value2", "key2.0": 1, "key2.1": 2, "key2.2": 3, "key3.0": true, "key3.1": false, "key3.2": true, "key4.0": null, "key4.1": undefined, "key4.2": "a", "key4.3": 1, "key4.4": true, } expect(sanitize(input)).toEqual(expected) }) test("should handle nested valid attributes", () => { const input = { key1: "Example", key2: 10, nested: { nestedKey1: "Value", nestedKey2: { nestedKey2Key1: true, }, }, } const expected = { key1: "Example", key2: 10, "nested.nestedKey1": "Value", "nested.nestedKey2.nestedKey2Key1": true, } expect(sanitize(input)).toEqual(expected) }) test("should handle a mix of valid and non-valid nested attributes", () => { const input = { key1: "Example", key2: 10, nested: { nestedKey1: "Value", nestedKey2: { nestedKey2Key1: true, }, nestedKey3: { reallyNested: "hello", }, }, } const expected = { key1: "Example", key2: 10, "nested.nestedKey1": "Value", "nested.nestedKey2.nestedKey2Key1": true, "nested.nestedKey3.reallyNested": "hello", } expect(sanitize(input)).toEqual(expected) }) test("should throw an error when a function is passed", () => { const key1 = () => {} const input = { key1, } const expected = { key1, } expect(sanitize(input)).toEqual(expected) }) test("should handle when input is not an object", () => { expect(sanitize(null)).toEqual({}) expect(sanitize(undefined)).toEqual({}) expect(sanitize("")).toEqual({}) expect(sanitize([1, 2, 3])).toEqual({ "0": 1, "1": 2, "2": 3 }) expect(sanitize("Test string")).toEqual({ value: "Test string" }) }) })