Skip to content

Commit 57dd9f3

Browse files
committed
Add toPath() method
1 parent 038b5d0 commit 57dd9f3

File tree

4 files changed

+95
-21
lines changed

4 files changed

+95
-21
lines changed

src/index.test.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/to-path.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import toPath from './to-path';
2+
3+
const testCases: {
4+
parameters: Parameters<typeof toPath>;
5+
result: ReturnType<typeof toPath>;
6+
}[] = [
7+
{
8+
parameters: [null],
9+
result: []
10+
},
11+
{
12+
parameters: [],
13+
result: []
14+
},
15+
{
16+
parameters: [undefined],
17+
result: []
18+
},
19+
{
20+
parameters: ['plain.object.helpers'],
21+
result: ['plain', 'object', 'helpers']
22+
},
23+
{
24+
parameters: ['plain[object][helpers]'],
25+
result: ['plain[object][helpers]']
26+
},
27+
{
28+
parameters: ['plain[1][2][0]object[10][helpers]'],
29+
result: ['plain', '1', '2', '0', 'object', '10', '[helpers]']
30+
},
31+
{
32+
parameters: ['plain10]object10]helpers'],
33+
result: ['plain10]object10]helpers']
34+
},
35+
{
36+
parameters: ['plain[0]object][helpers]'],
37+
result: ['plain', '0', 'object][helpers]']
38+
},
39+
{
40+
parameters: ['.plain..object...helpers.]'],
41+
result: ['plain', 'object', 'helpers']
42+
},
43+
{
44+
parameters: ['plain[0]..object[1].helpers]'],
45+
result: ['plain', '0', 'object', '1', 'helpers]']
46+
},
47+
{
48+
parameters: ['plain[][][]object[1].helpers'],
49+
result: ['plain[][][]object', '1', 'helpers']
50+
},
51+
{
52+
parameters: ['hasOwnProperty'],
53+
result: []
54+
},
55+
{
56+
parameters: ['toString'],
57+
result: ['toString']
58+
}
59+
];
60+
61+
describe('toPath tool', () => {
62+
testCases.forEach(({ parameters, result }, i) => {
63+
test(`test #${i + 1}: ${parameters.join(', ')}`, () => {
64+
const path = toPath(...parameters);
65+
expect(path).toStrictEqual(result);
66+
});
67+
});
68+
});

src/to-path.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const cache: Record<string, string[]> = {};
2+
const regex = /(\[(?=\d+])|(?<=\[\d+)])|\.+/;
3+
4+
/**
5+
* Convert string sample in to the path (array)
6+
* Inspired by final-form's `toPath` helper
7+
* {@link https://github.com/final-form/final-form/blob/master/src/structure/toPath.js}
8+
* @param {string|null} [sample]
9+
* @returns {string[]}
10+
*/
11+
export default (sample?: string | null): string[] => {
12+
if (typeof sample === 'string' && sample.length > 0 && sample !== 'hasOwnProperty') {
13+
if (!cache.hasOwnProperty(sample)) {
14+
cache[sample] = sample
15+
.split(regex)
16+
.filter(
17+
(part: string | undefined) =>
18+
typeof part === 'string' &&
19+
part.length > 0 &&
20+
part !== '[' &&
21+
part !== ']'
22+
);
23+
}
24+
return cache[sample];
25+
}
26+
return [];
27+
};

0 commit comments

Comments
 (0)