Skip to content

Commit

Permalink
feat: added deepest function
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Jul 8, 2020
1 parent 24d442d commit 6e83fa2
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"noop",
"unique id",
"replace",
"remove"
"remove",
"deepest"
],
"repository": {
"type": "git",
Expand Down
13 changes: 13 additions & 0 deletions src/object/deepest.spec.ts
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);
});
});
54 changes: 54 additions & 0 deletions src/object/deepest.ts
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;
}
1 change: 1 addition & 0 deletions src/object/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './clone';
export * from './deepest';
14 changes: 14 additions & 0 deletions src/object/static/deepest.spec.ts
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'));
});
});
35 changes: 35 additions & 0 deletions src/object/static/deepest.ts
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;

0 comments on commit 6e83fa2

Please sign in to comment.