-
-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support array indexes #82
Conversation
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
I think we should use |
What should |
IMHO, nothing. It would be the same as |
I think that a.0.b make more sense. To do this without breaking changes you could just not let the user create an new array with the dot notation but let them edit them, add items or get them with classic let data = {a:[]}
dotProp.set(data , 'a.0', 1);
console.log(data) // {a:[1]}
dotProp.set(data , 'a.1.b', 2);
console.log(data) // {a:[1, {b:2}]}
dotProp.set(data , 'a..b', 3);
console.log(data) // {a:[1, {b:2}, {b:3}]} let data = {}
dotProp.set(data , 'a.0', 1); // Throw exception Or with the let data = {}
dotProp.set(data , 'a..0', 1);
console.log(data) // {a:[1]}
dotProp.set(data , 'a..1.b', 2);
console.log(data) // {a:[1, {b:2}]}
data = {}
dotProp.set(data , 'a..1.b', 2);
console.log(data) // {a:[undefined, {b:2}]} Currently the let data = {}
dotProp.set(data , 'a..b', 2);
{ a: { '': { b: 2 } } } Edit: throw exception is not a good idea because this is a breaking change. Maybe you could just add or edit an array but not create it. let data = {}
dotProp.set(data , 'a', []); // {a:[]}
dotProp.set(data , 'a.0', 1); // {a:[1]}
dotProp.set(data , 'a.2', 2); // {a:[1, undefined, 2]}
dotProp.set(data , 'a..', 3); // {a:[1, undefined, 2, 3]}
dotProp.set(data , 'a..b', 4); // {a:[1, undefined, 2, 3, {b:4}]} |
@sindresorhus I've managed to turn the segmenting logic into a simple regex. Should I add it directly to the |
@Richienb Are you sure a regex is a good idea for this? I'm generally trying to avoid regexes unless absolutely necessary as it's so easy to accidentally make a ReDoS. |
Good point. Regexes make it much easier to implement this extra feature but otherwise, we could just revert to scanning the string from left to right to segment it. |
Bump, in case you forgot about this. If you're just busy, feel free to ignore. |
@sindresorhus Are you ok with the parsing strategy I mentioned above? |
👍 |
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
I have chosen to segment |
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
The tests are failing because of a bug: #86 |
CI is now passing. |
I would like more tests with lots of different variations of invalid patterns (including different kind of escaping), just to ensure the parsing handles all edge-cases. |
Why? I think that should be an invalid pattern and not return anything. It's beneficial to keep the syntax as simple as possible to reduce the chances of bugs and vulnerabilities. |
I'm trying to be as lenient as possible on the errors so instead, I'll have this literally parsed as |
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Bump :) |
@Richienb @sindresorhus Is this PR working now so I can try it locally? |
@mikeerickson It's mostly done but not completely. |
ok, no problem. i will check back again in a week or so. |
Bump => #82 (comment) Happy new year :) |
I realised that an extremely lenient parser would probably lead to user confusion so I made it much stricter. |
Great work, @Richienb 👍 |
Breaking change
Before:
After:
Fixes #71
Fixes #87