Skip to content

Commit

Permalink
fix: object keys which starts with a number
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaple committed Oct 23, 2023
1 parent 2f184a0 commit e5252cc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/flatten.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('flattenObject', () => {
a: {
b: {
c: 123,
'2c': 456,
},
d: [
{
Expand All @@ -29,6 +30,7 @@ describe('flattenObject', () => {
},
{
'a.b.c': 123,
'a.b["2c"]': 456,
'a.d[0].e': 456,
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const flatten = <T>(
const getKey = (key: string, prefix: string, isNumber: boolean) => {
let k;
if (
/[.'"\s\\\b\f\n\r\t\v{}()[\];,<>=!+\-*%&|^~?:]/.test(key) ||
/[.'"\s\\\b\f\n\r\t\v{}()[\];,<>=!+\-*%&|^~?:]|^\d+\D/.test(key) ||
key === ''
) {
// use brackets if key contains special characters
Expand Down
5 changes: 5 additions & 0 deletions src/internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ describe('parsePath', () => {
expect(result).toEqual(['foo', 'bar', 1, 'baz', 'qux']);
});

it('should parse string starts with number', () => {
const result = parsePath('foo.1bc');
expect(result).toEqual(['foo', '1bc']);
});

it('should parse escaped quotes correctly', () => {
const result = parsePath('foo["bar\\"baz"]');
expect(result).toEqual(['foo', 'bar"baz']);
Expand Down

0 comments on commit e5252cc

Please sign in to comment.