diff --git a/README.md b/README.md index 3ae2311..143394d 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,19 @@ const clonedObject = clone(object); // object.b.d === clonedObject.b.d // false ``` +#### [Deepest](https://thalesrc.github.io/js-utils/modules/_object_deepest_.html) +Get deepest value in an object chain + +```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) +``` + ### Promise #### [Revert](https://thalesrc.github.io/js-utils/modules/_promise_revert_.html) diff --git a/package.json b/package.json index b5cb525..43a4370 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "noop", "unique id", "replace", - "remove" + "remove", + "deepest" ], "repository": { "type": "git", diff --git a/src/object/deepest.spec.ts b/src/object/deepest.spec.ts new file mode 100644 index 0000000..f0518a8 --- /dev/null +++ b/src/object/deepest.spec.ts @@ -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); + }); +}); diff --git a/src/object/deepest.ts b/src/object/deepest.ts new file mode 100644 index 0000000..0e2c851 --- /dev/null +++ b/src/object/deepest.ts @@ -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(object: T, key: K): T { + let obj: any = object; + + while (obj[key]) { + obj = obj[key]; + } + + return obj; +} diff --git a/src/object/index.ts b/src/object/index.ts index f86973e..3c9c467 100644 --- a/src/object/index.ts +++ b/src/object/index.ts @@ -1 +1,2 @@ export * from './clone'; +export * from './deepest'; diff --git a/src/object/static/deepest.spec.ts b/src/object/static/deepest.spec.ts new file mode 100644 index 0000000..87572fd --- /dev/null +++ b/src/object/static/deepest.spec.ts @@ -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')); + }); +}); diff --git a/src/object/static/deepest.ts b/src/object/static/deepest.ts new file mode 100644 index 0000000..f1eb8b9 --- /dev/null +++ b/src/object/static/deepest.ts @@ -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;