Skip to content

Commit 48d9bfe

Browse files
Merge pull request #2 from dutchenkoOleg/master
0.0.3-prealpha
2 parents 7492cda + 32ba085 commit 48d9bfe

File tree

7 files changed

+148
-7
lines changed

7 files changed

+148
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
## Coverage
99

10-
| Statements | Branches | Functions | Lines |
11-
| --- | --- | --- | --- |
12-
| ![Statements](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/Coverage-93.33%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) |
10+
| Statements | Branches | Functions | Lines |
11+
| --------------------------------------------------------------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
12+
| ![Statements](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/Coverage-95.24%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) |

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plain-object-helpers",
3-
"version": "0.0.1-prealpha",
3+
"version": "0.0.3-prealpha",
44
"description": "description",
55
"main": "dist/index.js",
66
"files": [

src/get-in.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// -----------------------------------------------------------------------------
2+
// Deps
3+
// -----------------------------------------------------------------------------
4+
5+
import { getIn } from './get-in';
6+
7+
// -----------------------------------------------------------------------------
8+
// Tests
9+
// -----------------------------------------------------------------------------
10+
11+
const testCases: {
12+
parameters: Parameters<typeof getIn>;
13+
result: ReturnType<typeof getIn>;
14+
}[] = [
15+
{
16+
parameters: [{ key: 'value' }, 'key'],
17+
result: 'value'
18+
},
19+
{
20+
parameters: [{ key: 'value' }, 'ZZZ'],
21+
result: undefined
22+
},
23+
{
24+
parameters: [{ key: 'value' }, 'ZZZ', 'fallbackValue'],
25+
result: 'fallbackValue'
26+
},
27+
{
28+
parameters: [
29+
{
30+
key1: {
31+
key2: {
32+
key3: 'value'
33+
}
34+
}
35+
},
36+
'key1.key2.key3'
37+
],
38+
result: 'value'
39+
},
40+
{
41+
parameters: [
42+
{
43+
key1: ['value0', 'value1', 'value2']
44+
},
45+
'key1[1]'
46+
],
47+
result: 'value1'
48+
},
49+
{
50+
parameters: [
51+
{
52+
key1: ['value0', 'value1', 'value2']
53+
},
54+
'key1[10]',
55+
'fallbackValue'
56+
],
57+
result: 'fallbackValue'
58+
},
59+
{
60+
parameters: [
61+
{
62+
key1: [
63+
{
64+
x: 777
65+
},
66+
{
67+
x: 888
68+
},
69+
{
70+
x: 999
71+
}
72+
]
73+
},
74+
'key1[2].x'
75+
],
76+
result: 999
77+
},
78+
{
79+
parameters: [['value0', 'value1', 'value2'], '[1]'],
80+
result: 'value1'
81+
}
82+
];
83+
84+
describe('getIn tool', () => {
85+
testCases.forEach(({ parameters, result }, i) => {
86+
test(`test #${i + 1}: ${parameters.join(', ')}`, () => {
87+
const value = getIn(...parameters);
88+
expect(value).toStrictEqual(result);
89+
});
90+
});
91+
});

src/get-in.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// -----------------------------------------------------------------------------
2+
// Deps
3+
// -----------------------------------------------------------------------------
4+
5+
import { toPath } from './to-path';
6+
import { isPlainObject } from './is-plain-object';
7+
8+
// -----------------------------------------------------------------------------
9+
// Helper
10+
// -----------------------------------------------------------------------------
11+
12+
/**
13+
* Convert string sample in to the path (array)
14+
* Inspired by final-form's `getIn` helper
15+
* {@link https://github.com/final-form/final-form/blob/master/src/structure/getIn.js}
16+
* @param {*} sample
17+
* @param {string} keyPath
18+
* @param {*} fallback
19+
*/
20+
export function getIn<GReturnValue = any>(
21+
sample: any | any[],
22+
keyPath: string,
23+
fallback?: GReturnValue
24+
): GReturnValue {
25+
const path = toPath(keyPath);
26+
const pathLength = path.length;
27+
28+
let current: any = sample;
29+
for (let i = 0; i < pathLength; i++) {
30+
const key: string = path[i];
31+
const index = parseInt(key, 10);
32+
if (isPlainObject(current) && current.hasOwnProperty(key)) {
33+
current = current[key];
34+
} else if (Array.isArray(current) && !Number.isNaN(index) && index > -1) {
35+
current = current[index];
36+
} else {
37+
current = undefined;
38+
break;
39+
}
40+
}
41+
return current === undefined ? fallback : current;
42+
}

src/to-path.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Deps
33
// -----------------------------------------------------------------------------
44

5-
import toPath from './to-path';
5+
import { toPath } from './to-path';
66

77
// -----------------------------------------------------------------------------
88
// Tests

src/to-path.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
// -----------------------------------------------------------------------------
2+
// Private
3+
// -----------------------------------------------------------------------------
4+
15
const cache: Record<string, string[]> = {};
26
const regex = /(\[(?=\d+])|(?<=\[\d+)])|\.+/;
37

8+
// -----------------------------------------------------------------------------
9+
// Helper
10+
// -----------------------------------------------------------------------------
11+
412
/**
513
* Convert string sample in to the path (array)
614
* Inspired by final-form's `toPath` helper
715
* {@link https://github.com/final-form/final-form/blob/master/src/structure/toPath.js}
816
* @param {string|null} [sample]
917
* @returns {string[]}
1018
*/
11-
export default (sample?: string | null): string[] => {
19+
export const toPath = (sample?: string | null): string[] => {
1220
if (typeof sample === 'string' && sample.length > 0 && sample !== 'hasOwnProperty') {
1321
if (!cache.hasOwnProperty(sample)) {
1422
cache[sample] = sample

0 commit comments

Comments
 (0)