Skip to content

[1.2] allow insert to last index - #290

Closed
gkorland wants to merge 6 commits into
1.2from
arrinsert
Closed

gkorland wants to merge 6 commits into
1.2from
arrinsert

Conversation

@gkorland

@gkorland gkorland commented Mar 23, 2021 •

Copy link
Copy Markdown
Contributor

fix #286

@gkorland gkorland linked an issue Mar 31, 2021 that may be closed by this pull request
@gkorland
gkorland requested a review from oshadmi April 25, 2021 14:54
@rafie rafie changed the title allow insert to last index [1.2] allow insert to last index Apr 25, 2021
Comment thread src/lib.rs

let mut new_value = curr.to_owned();
new_value.splice(index..index, items.into_iter());
if new_value.len() == index {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The value of index after calling normalize in line 503, is between 0 and curr.len() - 1 (inclusive),
so we can detect before calling normailze, whether to append at the end, or to insert,
For example:

let should_append = index >= len;
let index = index.normalize(len);
...
if should_append {
    new_value.append(...);

So for doc test:
json.set test $ '["a", "b", "c"]'
We can insert before "c"
by
JSON.ARRINSERT test $ 2 '"d"'
and get ["a", "b", "d", "c"]
Or we can append after "c"
by
JSON.ARRINSERT test $ 3 '"d"'
and get ["a", "b", "c", "d"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems we should also adjust the documentation accordingly.
Currently it says:
The index must be in the array's range.

@gkorland gkorland closed this May 4, 2021
@gkorland
gkorland deleted the arrinsert branch May 4, 2021 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON.ARRINSERT regression?

2 participants