-
-
Notifications
You must be signed in to change notification settings - Fork 723
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
7 changed files
with
175 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { reorderObject } from './reorderObject'; | ||
|
||
describe('reorderObject', () => { | ||
it('correctly reorders the object based on provided keys', () => { | ||
const myObj = { a: 1, b: 2, c: 3, d: 4 }; | ||
const order = ['b', 'a']; | ||
const result = reorderObject(myObj, order); | ||
const expected = { b: 2, a: 1, c: 3, d: 4 }; | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('ignores non-existent keys in the order array', () => { | ||
const myObj = { a: 1, b: 2, c: 3 }; | ||
const order = ['c', 'z', 'a']; // 'z' does not exist in myObj | ||
const result = reorderObject(myObj, order); | ||
const expected = { c: 3, a: 1, b: 2 }; | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('returns the original object when order array is empty', () => { | ||
const myObj = { a: 1, b: 2, c: 3 }; | ||
const order: string[] = []; | ||
const result = reorderObject(myObj, order); | ||
expect(result).toEqual(myObj); | ||
}); | ||
|
||
it('returns the object with the same order when order array contains all object keys', () => { | ||
const myObj = { a: 1, b: 2, c: 3 }; | ||
const order = ['a', 'b', 'c']; | ||
const result = reorderObject(myObj, order); | ||
expect(result).toEqual(myObj); | ||
}); | ||
|
||
it('does not modify the original object', () => { | ||
const myObj = { a: 1, b: 2, c: 3 }; | ||
const order = ['b', 'a']; | ||
const result = reorderObject(myObj, order); | ||
expect(myObj).toEqual({ a: 1, b: 2, c: 3 }); // myObj should remain unchanged | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const reorderObject = <T extends object>(obj: T, order: string[]): T => { | ||
// Create a set for quick lookup of the ordered keys | ||
const orderSet = new Set(order); | ||
|
||
const orderedObj: Partial<T> = {}; | ||
|
||
// Add explicitly ordered keys to the ordered object | ||
order.forEach((key) => { | ||
if (key in obj) { | ||
orderedObj[key as keyof T] = obj[key as keyof T]; | ||
} | ||
}); | ||
|
||
// Add remaining keys that were not explicitly ordered | ||
Object.keys(obj).forEach((key) => { | ||
if (!orderSet.has(key)) { | ||
orderedObj[key as keyof T] = obj[key as keyof T]; | ||
} | ||
}); | ||
|
||
return orderedObj as T; | ||
}; |