|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { flatten, unflatten } from "flat"; |
| 3 | +import { |
| 4 | + buildDenormalizedKeysMap, |
| 5 | + denormalizeObjectKeys, |
| 6 | + mapDeormalizedKeys, |
| 7 | + normalizeObjectKeys, |
| 8 | + OBJECT_NUMERIC_KEY_PREFIX, |
| 9 | +} from "./flat"; |
| 10 | + |
| 11 | +describe("flat loader helper functions", () => { |
| 12 | + const inputObj = { |
| 13 | + messages: { |
| 14 | + "1": "a", |
| 15 | + "2": "b", |
| 16 | + }, |
| 17 | + }; |
| 18 | + const inputArray = { |
| 19 | + messages: ["a", "b", "c"], |
| 20 | + }; |
| 21 | + |
| 22 | + describe("denormalizeObjectKeys", () => { |
| 23 | + it("should denormalize object keys", () => { |
| 24 | + const output = denormalizeObjectKeys(inputObj); |
| 25 | + expect(output).toEqual({ |
| 26 | + messages: { |
| 27 | + [`${OBJECT_NUMERIC_KEY_PREFIX}1`]: "a", |
| 28 | + [`${OBJECT_NUMERIC_KEY_PREFIX}2`]: "b", |
| 29 | + }, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should preserve array", () => { |
| 34 | + const output = denormalizeObjectKeys(inputArray); |
| 35 | + expect(output).toEqual({ |
| 36 | + messages: ["a", "b", "c"], |
| 37 | + }); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("buildDenormalizedKeysMap", () => { |
| 42 | + it("should build normalized keys map", () => { |
| 43 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputObj), { delimiter: "/" }); |
| 44 | + const output = buildDenormalizedKeysMap(denormalized); |
| 45 | + expect(output).toEqual({ |
| 46 | + "messages/1": `messages/${OBJECT_NUMERIC_KEY_PREFIX}1`, |
| 47 | + "messages/2": `messages/${OBJECT_NUMERIC_KEY_PREFIX}2`, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should build keys map array", () => { |
| 52 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputArray), { delimiter: "/" }); |
| 53 | + const output = buildDenormalizedKeysMap(denormalized); |
| 54 | + expect(output).toEqual({ |
| 55 | + "messages/0": "messages/0", |
| 56 | + "messages/1": "messages/1", |
| 57 | + "messages/2": "messages/2", |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("normalizeObjectKeys", () => { |
| 63 | + it("should normalize denormalized object keys", () => { |
| 64 | + const output = normalizeObjectKeys(denormalizeObjectKeys(inputObj)); |
| 65 | + expect(output).toEqual(inputObj); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should process array keys", () => { |
| 69 | + const output = normalizeObjectKeys(denormalizeObjectKeys(inputArray)); |
| 70 | + expect(output).toEqual(inputArray); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe("mapDeormalizedKeys", () => { |
| 75 | + it("should map normalized keys", () => { |
| 76 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputObj), { delimiter: "/" }); |
| 77 | + const keyMap = buildDenormalizedKeysMap(denormalized); |
| 78 | + const flattened: Record<string, string> = flatten(inputObj, { delimiter: "/" }); |
| 79 | + const mapped = mapDeormalizedKeys(flattened, keyMap); |
| 80 | + expect(mapped).toEqual(denormalized); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should map array", () => { |
| 84 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputArray), { delimiter: "/" }); |
| 85 | + const keyMap = buildDenormalizedKeysMap(denormalized); |
| 86 | + const flattened: Record<string, string> = flatten(inputArray, { delimiter: "/" }); |
| 87 | + const mapped = mapDeormalizedKeys(flattened, keyMap); |
| 88 | + expect(mapped).toEqual(denormalized); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments