Skip to content

add allowDotsForNumbers option to convert from brackets of indices to dots#542

Open
antareepsarkar wants to merge 3 commits intoljharb:mainfrom
antareepsarkar:main
Open

add allowDotsForNumbers option to convert from brackets of indices to dots#542
antareepsarkar wants to merge 3 commits intoljharb:mainfrom
antareepsarkar:main

Conversation

@antareepsarkar
Copy link
Copy Markdown

resolves #516

allowDots does not convert bracket notation for array indices to dots.
This PR adds an option allowDotsForIndices which does that.

@Pratikshit26

This comment was marked as off-topic.

Copy link
Copy Markdown
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about for parse?

@antareepsarkar
Copy link
Copy Markdown
Author

what about for parse?

Sorry, just forgot about parse. I will modify that as well.

@ljharb ljharb marked this pull request as draft February 5, 2026 00:32
@antareepsarkar antareepsarkar requested a review from ljharb February 7, 2026 06:23
@antareepsarkar antareepsarkar marked this pull request as ready for review February 9, 2026 09:21
Copy link
Copy Markdown
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the issue suggests "allowDotsForNumbers" - "for indices" and "for numbers" are distinctly different and imply different semantics.

Additionally, the intention was for allowDots to be required to be explicitly passed as true when the new option is passed.

Also, if this new option is omitted, behavior MUST not change, and in this PR, it does.

If allowDotsForIndices: true and encodeDotInKeys: true, the dots used for indices would get encoded as %2E, defeating the purpose. There's no guard for this.

lib/parse.js Outdated
var splitKeyIntoSegments = function splitKeyIntoSegments(givenKey, options) {
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
var key = options.allowDots
? options.allowDotsForIndices ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey.replace(/\.(?!\d)([^.[\]]+)/g, '[$1]') : givenKey;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this regex differs from the original in the capture group: it adds ] to the exclusion set ([^.[]] vs [^.[). This means keys containing ] that were previously captured now won't be. This would be a breaking change.

additionally, the negative lookahead (?!\d) is too broad. It prevents ANY dot-before-digit from being treated as property access, not just dots before valid array indices. For example, a.1foo.b=c - 1foo is not a valid array index, but the regex would still skip .1foo, producing { 'a.1foo': { b: 'c' } } instead of the current { a: { '1foo': { b: 'c' } } }, which is another breaking change.

@ljharb ljharb marked this pull request as draft February 11, 2026 22:39
@antareepsarkar antareepsarkar marked this pull request as ready for review March 2, 2026 14:14
@antareepsarkar antareepsarkar requested a review from ljharb March 2, 2026 14:14
@antareepsarkar antareepsarkar changed the title add allowDotsForIndices option to convert from brackets of indices to dots add allowDotsForNumbersoption to convert from brackets of indices to dots Mar 8, 2026
@antareepsarkar antareepsarkar changed the title add allowDotsForNumbersoption to convert from brackets of indices to dots add allowDotsForNumbers option to convert from brackets of indices to dots Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stringify, request supporting 'dot' as option for arrayFormat

3 participants