1import { DeepPartial } from 'utility-types';
2import merge from 'ts-deepmerge';
3
4interface Toggleable<T> {
5 inactive: T
6 active: T,
7}
8
9/**
10 * Helper function for creating Toggleable objects.
11 * @template T The type of the object being toggled.
12 * @param inactive The initial state of the toggleable object.
13 * @param modifications The modifications to be applied to the initial state to create the active state.
14 * @returns A Toggleable object containing both the inactive and active states.
15 * @example
16 * ```
17 * toggleable({day: 1, month: "January"}, {day: 3})
18 * ```
19 * This returns the following object:
20 * ```
21 * Toggleable<_>{
22 * inactive: { day: 1, month: "January" },
23 * active: { day: 3, month: "January" }
24 * }
25 * ```
26 * The function also works for nested structures:
27 * ```
28 * toggleable({first_level: "foo", second_level: {nested_member: "nested"}}, {second_level: {nested_member: "another nested thing"}})
29 * ```
30 * Which returns:
31 * ```
32 * Toggleable<_> {
33 * inactive: {first_level: "foo", second_level: {nested_member: "nested"}},
34 * active: { first_level: "foo", second_level: {nested_member: "another nested thing"}}
35 * }
36 * ```
37 */
38export function toggleable<T extends Object>(inactive: T, modifications: DeepPartial<T>): Toggleable<T> {
39 let active: T = merge(inactive, modifications) as T;
40 return { active: active, inactive: inactive };
41}