-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,87 +3,79 @@ import { describe, it, expect } from "bun:test"; | |
import { convertKeys } from "./transform"; | ||
|
||
describe("convertKeys - Edge Cases", () => { | ||
it("should handle empty objects and arrays", () => { | ||
expect(convertKeys({})).toEqual({}); | ||
expect(convertKeys([])).toEqual([]); | ||
}); | ||
it("should handle empty objects and arrays", () => { | ||
expect(convertKeys({})).toEqual({}); | ||
expect(convertKeys([])).toEqual([]); | ||
}); | ||
|
||
it("should handle mixed nested structures", () => { | ||
const input = { | ||
Complex_Structure: { | ||
Array_Of_Objects: [ | ||
{ First_Item: 1 }, | ||
{ Second_Item: 2 } | ||
], | ||
Nested_Array: [[{ Deep_Nested: true }]], | ||
Mixed_Values: { | ||
Some_String: "test", | ||
Some_Number: 123, | ||
Some_Boolean: true, | ||
Some_Null: null, | ||
Some_Undefined: undefined | ||
} | ||
} | ||
}; | ||
it("should handle mixed nested structures", () => { | ||
const input = { | ||
Complex_Structure: { | ||
Array_Of_Objects: [{ First_Item: 1 }, { Second_Item: 2 }], | ||
Nested_Array: [[{ Deep_Nested: true }]], | ||
Mixed_Values: { | ||
Some_String: "test", | ||
Some_Number: 123, | ||
Some_Boolean: true, | ||
Some_Null: null, | ||
Some_Undefined: undefined, | ||
}, | ||
}, | ||
}; | ||
|
||
const expected = { | ||
complexstructure: { | ||
arrayofobjects: [ | ||
{ firstitem: 1 }, | ||
{ seconditem: 2 } | ||
], | ||
nestedarray: [[{ deepnested: true }]], | ||
mixedvalues: { | ||
somestring: "test", | ||
somenumber: 123, | ||
someboolean: true, | ||
somenull: null, | ||
someundefined: undefined | ||
} | ||
} | ||
}; | ||
|
||
expect(convertKeys(input)).toEqual(expected); | ||
}); | ||
}) | ||
const expected = { | ||
complexstructure: { | ||
arrayofobjects: [{ firstitem: 1 }, { seconditem: 2 }], | ||
nestedarray: [[{ deepnested: true }]], | ||
mixedvalues: { | ||
somestring: "test", | ||
somenumber: 123, | ||
someboolean: true, | ||
somenull: null, | ||
someundefined: undefined, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(convertKeys(input)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("convertKeys - Microsoft Graph User Data", () => { | ||
it("should correctly transform Microsoft Graph user data structure", () => { | ||
const input = { | ||
sub: "b-PncAH_Eo20KlWh1NeP0y8T0gmrcexwXB5RC_5WxAI", | ||
name: "Tanita Hjort Sode", | ||
family_name: "Sode", | ||
given_name: "Tanita Hjort", | ||
picture: "https://graph.microsoft.com/v1.0/me/photo/$value", | ||
email: "[email protected]" | ||
}; | ||
it("should correctly transform Microsoft Graph user data structure", () => { | ||
const input = { | ||
sub: "b-PncAH_Eo20KlWh1NeP0y8T0gmrcexwXB5RC_5WxAI", | ||
name: "Tanita Hjort Sode", | ||
family_name: "Sode", | ||
given_name: "Tanita Hjort", | ||
picture: "https://graph.microsoft.com/v1.0/me/photo/$value", | ||
email: "[email protected]", | ||
}; | ||
|
||
const expected = { | ||
sub: "b-PncAH_Eo20KlWh1NeP0y8T0gmrcexwXB5RC_5WxAI", | ||
name: "Tanita Hjort Sode", | ||
familyname: "Sode", | ||
givenname: "Tanita Hjort", | ||
picture: "https://graph.microsoft.com/v1.0/me/photo/$value", | ||
email: "[email protected]" | ||
}; | ||
const expected = { | ||
sub: "b-PncAH_Eo20KlWh1NeP0y8T0gmrcexwXB5RC_5WxAI", | ||
name: "Tanita Hjort Sode", | ||
familyname: "Sode", | ||
givenname: "Tanita Hjort", | ||
picture: "https://graph.microsoft.com/v1.0/me/photo/$value", | ||
email: "[email protected]", | ||
}; | ||
|
||
const result = convertKeys(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
const result = convertKeys(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
// Additional test to verify specific field transformations | ||
it("should specifically verify each field transformation", () => { | ||
const input = { | ||
family_name: "Sode", | ||
given_name: "Tanita Hjort" | ||
}; | ||
// Additional test to verify specific field transformations | ||
it("should specifically verify each field transformation", () => { | ||
const input = { | ||
family_name: "Sode", | ||
given_name: "Tanita Hjort", | ||
}; | ||
|
||
const result = convertKeys(input); | ||
const result = convertKeys(input); | ||
|
||
// Individual field checks | ||
expect(result).toHaveProperty('familyname'); | ||
expect(result).toHaveProperty('givenname'); | ||
}); | ||
// Individual field checks | ||
expect(result).toHaveProperty("familyname"); | ||
expect(result).toHaveProperty("givenname"); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
// Custom type to remove underscores and convert to lowercase | ||
type RemoveUnderscoreAndLowercase<S extends string> = | ||
S extends `${infer F}_${infer R}` | ||
? `${Lowercase<F>}${RemoveUnderscoreAndLowercase<R>}` | ||
: Lowercase<S>; | ||
type RemoveUnderscoreAndLowercase<S extends string> = | ||
S extends `${infer F}_${infer R}` | ||
? `${Lowercase<F>}${RemoveUnderscoreAndLowercase<R>}` | ||
: Lowercase<S>; | ||
|
||
// Modified TransformKeys type using the custom type | ||
type TransformKeys<T> = T extends Array<any> | ||
? Array<TransformKeys<T[number]>> | ||
: T extends object | ||
? { [K in keyof T as RemoveUnderscoreAndLowercase<string & K>]: TransformKeys<T[K]> } | ||
: T; | ||
? Array<TransformKeys<T[number]>> | ||
: T extends object | ||
? { | ||
[K in keyof T as RemoveUnderscoreAndLowercase< | ||
string & K | ||
>]: TransformKeys<T[K]>; | ||
} | ||
: T; | ||
|
||
export function convertKeys<T extends Record<string, any>>( | ||
obj: T | ||
obj: T, | ||
): TransformKeys<T> { | ||
if (obj === null || typeof obj !== 'object') { | ||
return obj as TransformKeys<T>; | ||
} | ||
if (obj === null || typeof obj !== "object") { | ||
return obj as TransformKeys<T>; | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
return obj.map(item => convertKeys(item)) as TransformKeys<T>; | ||
} | ||
if (Array.isArray(obj)) { | ||
return obj.map((item) => convertKeys(item)) as TransformKeys<T>; | ||
} | ||
|
||
return Object.entries(obj).reduce((acc: any, [key, value]) => { | ||
const newKey = key.toLowerCase().replace(/_/g, ''); | ||
acc[newKey] = typeof value === 'object' | ||
? convertKeys(value) | ||
: value; | ||
return acc; | ||
}, {}) as TransformKeys<T>; | ||
} | ||
return Object.entries(obj).reduce((acc: any, [key, value]) => { | ||
const newKey = key.toLowerCase().replace(/_/g, ""); | ||
acc[newKey] = typeof value === "object" ? convertKeys(value) : value; | ||
return acc; | ||
}, {}) as TransformKeys<T>; | ||
} |