-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
24d442d
commit 6e83fa2
Showing
7 changed files
with
132 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,8 @@ | |
"noop", | ||
"unique id", | ||
"replace", | ||
"remove" | ||
"remove", | ||
"deepest" | ||
], | ||
"repository": { | ||
"type": "git", | ||
|
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,13 @@ | ||
import 'jest'; | ||
|
||
import { deepest } from './deepest'; | ||
|
||
describe('Deepest Function', () => { | ||
it('should get deepest object', () => { | ||
const a = {x: null}; | ||
const b = {x: a}; | ||
const c = {x: b}; | ||
|
||
expect(deepest(c, 'x')).toBe(a); | ||
}); | ||
}); |
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,54 @@ | ||
/** | ||
* #### Get deepest value in an object chain | ||
* | ||
* * * * * | ||
* Example usage: | ||
* ```typescript | ||
* import { deepest } from "@thalesrc/js-utils/object"; | ||
* | ||
* const a = { | ||
* x: null | ||
* }; | ||
* | ||
* const b = { | ||
* x: a | ||
* }; | ||
* | ||
* const c = { | ||
* x: b | ||
* }; | ||
* | ||
* deepest(c, 'x'); // {x: null} (a) | ||
* | ||
* ``` | ||
* Static usage example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/object/static/deepest"; | ||
* | ||
* const a = { | ||
* x: null | ||
* }; | ||
* | ||
* const b = { | ||
* x: a | ||
* }; | ||
* | ||
* const c = { | ||
* x: b | ||
* }; | ||
* | ||
* Object.deepest(c, 'x'); // a | ||
* ``` | ||
* * * * | ||
* @param object Object to deep dive | ||
* @param key key of the object which contains same type instance | ||
*/ | ||
export function deepest<T, K extends {[P in keyof T]: T[P] extends T ? P : never}[keyof T]>(object: T, key: K): T { | ||
let obj: any = object; | ||
|
||
while (obj[key]) { | ||
obj = obj[key]; | ||
} | ||
|
||
return obj; | ||
} |
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 +1,2 @@ | ||
export * from './clone'; | ||
export * from './deepest'; |
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,14 @@ | ||
import 'jest'; | ||
|
||
import './deepest'; | ||
import { deepest } from '../deepest'; | ||
|
||
describe('Deepest Static Function', () => { | ||
it('should act as same', () => { | ||
const a = {x: null}; | ||
const b = {x: a}; | ||
const c = {x: b}; | ||
|
||
expect(deepest(c, 'x')).toBe(Object.deepest(c, 'x')); | ||
}); | ||
}); |
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,35 @@ | ||
import { deepest } from '../deepest'; | ||
|
||
declare global { | ||
export interface ObjectConstructor { | ||
/** | ||
* #### Get deepest value in an object chain | ||
* | ||
* * * * * | ||
* Example usage: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/object/static/deepest"; | ||
* | ||
* const a = { | ||
* x: null | ||
* }; | ||
* | ||
* const b = { | ||
* x: a | ||
* }; | ||
* | ||
* const c = { | ||
* x: b | ||
* }; | ||
* | ||
* Object.deepest(c, 'x'); // a | ||
* ``` | ||
* * * * | ||
* @param object Object to deep dive | ||
* @param key key of the object which contains same type instance | ||
*/ | ||
deepest: typeof deepest; | ||
} | ||
} | ||
|
||
Object.deepest = deepest; |