File size: 1,890 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, expect, it } from "vitest";

import { exclude } from "../object";

describe("exclude", () => {
  type TestObject = {
    id: number;
    name: string;
    age: number;
    email: string;
  };

  it("excludes specified keys from the object", () => {
    const object: TestObject = {
      id: 1,
      name: "Alice",
      age: 30,
      email: "alice@example.com",
    };
    const result = exclude(object, ["age", "email"]);

    expect(result).toEqual({ id: 1, name: "Alice" });
    expect(result).not.toHaveProperty("age");
    expect(result).not.toHaveProperty("email");
  });

  it("returns the same object if no keys are specified", () => {
    const object: TestObject = {
      id: 1,
      name: "Alice",
      age: 30,
      email: "alice@example.com",
    };
    const keysToExclude: (keyof TestObject)[] = [];
    const result = exclude(object, keysToExclude);

    expect(result).toEqual(object);
  });

  it("does not modify the original object", () => {
    const object: TestObject = {
      id: 1,
      name: "Alice",
      age: 30,
      email: "alice@example.com",
    };
    exclude(object, ["age", "email"]);

    expect(object).toHaveProperty("age");
    expect(object).toHaveProperty("email");
  });

  it("handles cases where keys to exclude are not present in the object", () => {
    const object: TestObject = {
      id: 1,
      name: "Alice",
      age: 30,
      email: "alice@example.com",
    };
    const keysToExclude = ["nonExistentKey" as keyof TestObject];
    const result = exclude(object, keysToExclude);

    expect(result).toEqual(object);
  });

  it("returns the input if it is not an object", () => {
    const object: unknown = null;
    const keysToExclude = ["id"];

    // @ts-expect-error passing invalid input type for tests
    const result = exclude(object, keysToExclude);

    expect(result).toBeNull();
  });
});